【问题标题】:Passing a Java object value in Custom JSP tag在自定义 JSP 标记中传递 Java 对象值
【发布时间】:2010-12-10 20:57:28
【问题描述】:

我正在尝试从自定义 jsp 标记传递一个 java 变量(我在这里使用 struts2 从 java 类中获取变量)。这是我遇到的错误。

javax.servlet.ServletException: /pages/editBidForm.jsp(51,8) According to TLD or attribute directive in tag file, attribute parentId does not accept any expressions
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
    ....

这是我的jsp页面(部分)

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="/WEB-INF/taglib.tld" prefix="custom" %>
...
...
<table>
           <tr>
           <%

         String bidformoid=null;
         bidFormOid=request.getParameter("bidFormOid");
         %>

            <td> <custom:zorancustomtag  parentType = "BIDFORM" parentId = "<%= pageContext.getAttribute("bidFormOid") %>" /></td>   


           </tr>
        </table>

我无法正确传递 parentId 参数。我能够正确传递 parentType 参数,因为它只涉及传递字符串

这里是 taglib 文件。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag 
Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
      <tlibversion>1.0</tlibversion>
      <jspversion>1.1</jspversion>
      <shortname>custom</shortname>
  <tag>
      <name>zorancustomtag</name>
      <tagclass>com.zoran.action.CustomizedTag</tagclass>
      <bodycontent>JSP</bodycontent>
      <info>Tag having a body and attributes</info>
      <attribute>
         <name>name</name>
         <required>false</required>
         <rtexpvalue>false</rtexpvalue>
      </attribute>

       <attribute>
         <name>parentType</name>
         <required>true</required>
         <rtexpvalue>true</rtexpvalue>
      </attribute>

       <attribute>
         <name>parentId</name>
         <required>true</required>
         <rtexpvalue>false</rtexpvalue>
      </attribute>



   </tag>

</taglib>

还有自定义标签的java类。

public class CustomizedTag implements Tag {
   private PageContext pageContext;
   private Tag parent;
   private String name;
   private int parentId;
   private String parentType;
   List list = null;




   public String getName() {
    return name;
   }

   public void setName(String name) {
    this.name = name;
   }

 /*  public CustomizedTag() {
      super();
   }
*/
   public int doStartTag() throws JspException {
       Session session = SessionUtil.getSession();
        session.beginTransaction();


      try {
          JspWriter out = pageContext.getOut();
          String parId = getParentId()+"";
        //  out.println(getParent()+"&nbsp;");
          String quer = "from ContentBase cb where cb.parentType=? AND cb.parentId=? ";//+parId;
          Query query = session.createQuery(quer);

            query.setParameter(0, getParentType());
            query.setParameter(1, getParentId());

            list = query.list();
            ContentBase cb = new ContentBase();
            if (null != list && !list.isEmpty()) {
                 cb = (ContentBase) list.get(0);
            }

        // pageContext.getOut().print("Sri "+getName());

          out.println(cb.getDescription());


      } catch (IOException ioe) {
         throw new JspException("Error:"+ioe.getMessage());
      }
      return SKIP_BODY;
   }

   public int doEndTag() throws JspException {
      return SKIP_PAGE;
   }
   public void release() {
   }



   public void setPageContext(PageContext pageContext) {
      this.pageContext = pageContext;
   }

   public void setParent(Tag parent) {
      this.parent = parent;
   }

   public Tag getParent() {
      return parent;
   }

public int getParentId() {
    return parentId;
}

public void setParentId(int parentId) {
    this.parentId = parentId;
}

public String getParentType() {
    return parentType;
}

public void setParentType(String parentType) {
    this.parentType = parentType;
}

}

谁能告诉我如何通过自定义jsp标签传递java变量。

谢谢, 阿迪亚

【问题讨论】:

    标签: java jsp jsp-tags


    【解决方案1】:

    您的 TLD 中的 &lt;rtexpvalue&gt; 元素应为 &lt;rtexprvalue&gt;,并且需要设置为 true

      <attribute>
         <name>parentId</name>
         <required>true</required>
         <rtexprvalue>true</rtexprvalue>
      </attribute>
    

    这允许将运行时表达式作为属性值提供。对于 JSP 设计团队中的哪些人认为允许将其设置为 false 是个好主意,我仍然感到困惑。

    【讨论】:

    • 感谢您的回答,但即使将属性设置为“true”,我也会收到相同的错误消息。带有属性的自定义jsp标签是否正确?
    • 有一个错字(也出现在 OP 的帖子中),应该是 rtexprvalue 而不是 rtexpvalue。我更新了答案。
    • 我还没有弄清楚是否可以在这里发送 pm,但感谢@BalusC,您一直是救生员!我从你这里的很多答案中得到了很多帮助。非常感谢!
    【解决方案2】:

    尝试将 parentId 值包装在 ${}

    <custom:zorancustomtag  parentType = "BIDFORM" parentId = "${<%= pageContext.getAttribute("bidFormOid") %>}" />
    

    【讨论】:

      猜你喜欢
      • 2011-09-12
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 2016-03-12
      • 2018-07-10
      • 1970-01-01
      • 2015-06-29
      相关资源
      最近更新 更多