2012-12-17 51 views
1

我试图通过项目的集合迭代:问题JSP的<c:forEach>

<c:forEach items="#{tree.items}" var="item"> 
    <h:commandLink rendered="#{item.type == 'category'}" action="#{item.onNodeClicked}" 

Hovewer,我有一个例外:

javax.el.PropertyNotFoundException:类' java.lang.String'没有'type'属性。

看起来像JSP将树项目识别为字符串对象。我错过了什么?

回答

1

这是行不通的。 #{item}仅在查看生成时间可用,而rendered属性在查看渲染时间期间进行评估。

改为使用<c:if>

<c:forEach items="#{tree.items}" var="item"> 
    <c:if test="#{item.type == 'category'}"> 
     <h:commandLink ... action="#{item.onNodeClicked}" /> 

或者,如果你在JSF2(这超出了3年以上),取代传统的JSP其继任Facelets并使用其<ui:repeat>组件,而不是。它在视图渲染时也被评估。

<ui:repeat value="#{tree.items}" var="item"> 
    <h:commandLink rendered="#{item.type == 'category'}" action="#{item.onNodeClicked}" /> 
+0

我应该注意到,依赖关系发生变化后出现问题。代码没有改变。 –

+0

没错(我们正在使用JSF 2。 –