2009-10-28 10 views
10

Próbuję przekazać zmienną java z niestandardowego znacznika JSP (Im przy użyciu tutaj struts2, aby pobrać zmienną z klasy Javy). Oto błąd, który dostaję.Przekazywanie wartości obiektu Java w niestandardowym znaczniku JSP

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

Oto moja strona JSP (część)

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

nie jestem w stanie poprawnie przekazać parametr parentId. Udało mi się poprawnie przekazać parametr parentType, ponieważ obejmował on tylko przekazanie ciągu znaków

Oto plik 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> 

I klasa java niestandardowego tagu.

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; 
} 

} 

Czy ktoś może dać mi znać, jak przekazać zmienną java za pomocą niestandardowego tagu JSP.

Dzięki Aditya

Odpowiedz

13

Element w TDU <rtexpvalue> powinny być <rtexprvalue> i musi być ustawiony na true:

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

Pozwala wyrażenia wykonywalne mają być dostarczone jako wartość atrybutu. Pozostaję w tajemnicy, kto w zespole projektowym JSP uznał, że dobrym pomysłem jest ustawienie tego na false.

+0

Dzięki za odpowiedź, ale nawet po ustawieniu atrybutu „true” Dostaję ten sam komunikat o błędzie. Czy niestandardowy tag JSP z atrybutem jest poprawny? –

+4

Wystąpił błąd literowy (występujący również w poście OP), powinien to być "rtexprwartość" zamiast "rtexpwartości". Zaktualizowałem odpowiedź. – BalusC

+0

To był dokładnie mój problem - wielkie dzięki! –

-2

Spróbuj zawijania wartość parentId w $ {}

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