每个类的Arraylist的JSTL

时间:2015-04-21 21:04:15

标签: jsp cq5

将Adobe CQ 5.5和JSP用于组件对象。在JSTL中返回结果时遇到一些麻烦。

我有一个自定义类的数组。

private static class Asset {

   private String displayname;

   public Asset(){
   }

   public String getDisplayName() {
      return displayname;
   }

   public void setDisplayName(String displayname) {
       this.displayname = displayname;
   }

}

此片段有效,但我想使用JSTL

  for (int i = 0; i < assets.size(); i++) {
      Asset c = assets.get(i);                        
      out.println(c.displayname + "<BR>");    
  }

这就是我想要做的事情:

 request.setAttribute("assetList",assets);
%>

<c:forEach items="${assetList}" var="item" varStatus="status">
   <p>Title:</p> 
   ${item.displayname}
</c:forEach>

JSTL会返回4个项目,例如工作示例,但是只要我添加${item.displayname},我就会收到错误。

Caused by: javax.el.PropertyNotFoundException: Property 'displayname' not found on type org.apache.jsp.apps.pnc_002dideas.components.homepage.slider.slider_jsp$Asset
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:193)
    at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:170)
    at javax.el.BeanELResolver.property(BeanELResolver.java:279)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:60)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:97)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at org.apache.sling.scripting.jsp.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:975)
    at org.apache.jsp.apps.dideas.components.homepage.slider.slider_jsp._jspx_meth_c_005fforEach_005f0(slider_jsp.java:576)
    at org.apache.jsp.apps.pnc_002dideas.components.homepage.slider.slider_jsp._jspService(slider_jsp.java:372)
    at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:497)

1 个答案:

答案 0 :(得分:5)

使用JSTL时,将根据getter方法名称而非私有字段名称解析属性名称。

因此,当您使用${item.displayname}时,它会搜索方法getDisplayname()而不是getDisplayName()

如下所示更改您的JSTL

<c:forEach items="${assetList}" var="item" varStatus="status">
   <p>Title:</p> 
   ${item.displayName}
</c:forEach>

或更改bean中的getter方法,如下所示,以使其正常工作。

   public String getDisplayname() {
      return displayname;
   }

请不要改变它们。