2009-10-28 81 views
10

我試圖從自定義jsp標記(我在這裏使用struts2從java類獲取變量)傳遞一個java變量。這是我得到的錯誤。在自定義JSP標記中傳遞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變量。

感謝, 阿迪亞

回答

13

在TLD的<rtexpvalue>元素應該是<rtexprvalue>,需要設置爲true

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

這使得運行時表達式爲屬性值提供。對於JSP設計團隊的成員認爲允許將其設置爲false是個好主意,我仍然很困惑。

+0

由於包裹parentId的值。定製jsp標記的屬性是否正確? – 2009-10-29 05:26:46

+4

有一個錯字(也出現在OP的文章中),它應該是'rtexprvalue'而不是'rtexpvalue'。我更新了答案。 – BalusC 2011-03-19 18:48:58

+0

這正是我的問題 - 非常感謝! – 2012-02-16 09:18:17

-2

嘗試的答案,但即使將屬性設置爲「真」我得到了同樣的錯誤消息後,在$ {}

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