我在将 p:commandButton
绑定(bind)到辅助 bean 中的属性时遇到问题。我试图简化我的代码以展示总体思路。
ExampleBean 是一个辅助 bean
public class ExampleBean {
public String title;
List<ExampleWrapper> list;
// Getters and setters
}
ExampleWrapper 是一个 POJO
public class Wrapper {
public String name;
public String description;
public CommandButton button;
// Listener which changes button state
// Getters and setters
}
index.xhtml 是一个主页面:
<h:form>
<h:outputText value="Title" />
<p:inpurText value="#{exampleBean.title}"
<ui:include src="list.xhtml">
<ui:param name="bean" value="#{exampleBean}">
</ui:include>
</h:form>
list.xhtml 是我想在几个地方重复使用的片段:
<ui:composition ...>
<ui:repeat id="list" var="exampleWrapper" value="#{bean.list}">
<h:outputText value="#{exampleWrapper.name}"/>
<h:outputTextarea value="#{exampleWrapper.description}"/>
<p:commandButton id="button" binding="#{exampleWrapper.button}"
value="Button" />
</ui:composition>
所以,我得到异常: javax.el.PropertyNotFoundException:/list.xhtml ... binding="#{exampleWrapper.button}": 目标无法到达,标识符 'exampleWrapper' 解析为 null
没有 binding
属性,一切正常,显示正常
您能解释一下为什么以及如何将按钮绑定(bind)到这个 POJO 属性吗?任何帮助将不胜感激
我正在使用 JSF 2.0.2 和 Primefaces 3.0.1
最佳答案
binding
(和 id
)JSF UI 组件的属性在 View 构建期间解析。 #{exampleWrapper}
实例在 View 构建期间不可用。 View 构建时间是 XHTML 文件被解析为 JSF 组件树的那一刻。 #{exampleWrapper}
仅在 View 渲染期间可用。 View 呈现时间是 JSF 组件树生成 HTML 输出的时刻。
基本上,只有一个 <p:commandButton>
在组件树中,它生成的 HTML 输出是 <ui:repeat>
的多倍迭代。您需要将其绑定(bind)到 #{bean}
相反,或者使用 JSTL <c:forEach>
而不是 <ui:repeat>
. JSTL 标记即在 View 构建时间和 <c:forEach>
期间运行因此将在物理上产生多个 JSF UI 组件。但是,在 JSF 2.x 中,通常不需要将组件绑定(bind)到支持 bean。无论您心中有什么样的功能需求,您认为这是解决方案,它肯定可以以更好的方式解决。
关于java - 如何将 JSF 组件绑定(bind)到支持 bean 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9723488/