在自定义 JSP 标记中传递 Java 对象值

发布于 2024-08-08 18:21:09 字数 4357 浏览 2 评论 0原文

我正在尝试从自定义 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 变量。

谢谢, 阿迪亚

I'm trying to pass a java variable from a custom jsp tag(Im using struts2 here to get the variable from the java class). Here is the error I'm getting.

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)
    ....

Here is my jsp page(part)

<%@ 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>

I'm not able to pass the parentId parameter correctly. I was able to pass the parentType parameter correctly since it involved only passing the string

Here is the taglib file.

<?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>

And the java class of the custom tag.

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()+" ");
          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;
}

}

Can anyone please let me know how to pass a java variable through custom jsp tag.

Thanks,
Aditya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个俗人 2024-08-15 18:21:09

TLD 中的 元素应为 并且需要设置为 true

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

这允许运行时表达式作为属性值提供。我仍然很困惑 JSP 设计团队中的谁认为允许将其设置为 false 是个好主意。

The <rtexpvalue> element in your TLD should be <rtexprvalue> and needs to be set to true:

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

This allows runtime expressions to be supplied as the attribute value. I remain mystified as to who on the JSP design team thought it was a good idea to allow this to be set to false.

暮年 2024-08-15 18:21:09

尝试将parentId值包装在${}中

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

Try wrapping the parentId value in ${}

<custom:zorancustomtag  parentType = "BIDFORM" parentId = "${<%= pageContext.getAttribute("bidFormOid") %>}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文