我有以下代码:
<h:outputText value="#{lecture.lectureName}" />
<c:forEach items="#{criterionController.getCriteriaForLecture(lecture)}" var="criterion">
<h:outputText value="#{criterion.criterionName}" />
<h:commandLink value="Edit"/>
<h:commandLink value="Delete"/>
</c:forEach>
输出文本部分工作正常并显示它应显示的内容,这样就证明lecture
对象已设置。但是,每个标记都会给出一个空指针异常。当我调试代码时,我看到在调用方法getCriteriaForLecture()
时,讲座对象被视为null。
这种行为如何解释?
答案 0 :(得分:2)
如果lecturer
变量又由JSF迭代组件设置,例如<h:dataTable>
,<ui:repeat>
等,或者可能是<p:tabView>
,则可能会发生这种情况。你的previous question。
可以在此处找到有关此行为的更详细说明:JSTL in JSF2 Facelets... makes sense?到目前为止,JSTL标记在构建视图期间运行,而不是在渲染视图期间运行。 lecturer
变量在您的特定情况下仅在呈现视图期间可用,因此在构建视图期间,当JSTL运行时,null
始终为<ui:repeat>
。
要解决此问题,请使用普通的JSF组件,例如<ui:repeat value="#{criterionController.getCriteriaForLecture(lecture)}" var="criterion">
<h:outputText value="#{criterion.criterionName}" />
<h:commandLink value="Edit"/>
<h:commandLink value="Delete"/>
</ui:repeat>
。
List<Criterion>
更好的是不在getter中做商业行为。只需将Lecture
设为<ui:repeat value="#{lecture.criterions}" var="criterion">
<h:outputText value="#{criterion.criterionName}" />
<h:commandLink value="Edit"/>
<h:commandLink value="Delete"/>
</ui:repeat>
的属性即可。
{{1}}