Java 如何将 JSF 组件绑定到支持 bean 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9723488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:44:38  来源:igfitidea点击:

How to bind JSF component to backing bean property

javajsfjakarta-eebindingprimefaces

提问by nikagra

I have a problem with a binding p:commandButtonto a property in a backing bean. I've tried to simplify my code to show general idea.

我在绑定p:commandButton到支持 bean 中的属性时遇到问题。我试图简化我的代码以展示总体思路。

ExampleBean is a backing bean

ExampleBean 是一个支持 bean

public class ExampleBean {

    public String title;        
    List<ExampleWrapper> list;

    // Getters and setters

}

ExampleWrapper is a POJO

ExampleWrapper 是一个 POJO

public class Wrapper {

    public String name;
    public String description;

    public CommandButton button;

    // Listener which changes button state

    // Getters and setters
}

index.xhtml is a main page:

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 is a fragment I want to be reused in a few places:

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>

So, I get exception: javax.el.PropertyNotFoundException: /list.xhtml ... binding="#{exampleWrapper.button}": Target Unreachable, identifier 'exampleWrapper' resolved to null

所以,我得到异常: javax.el.PropertyNotFoundException: /list.xhtml ... binding="#{exampleWrapper.button}": Target Unreachable, identifier 'exampleWrapper' 解析为 null

Without bindingattribute everything works and displays fine

没有binding属性,一切正常,显示正常

Could you explain why and how can I bind button to this POJO property? Any help will be appreciated

你能解释一下为什么以及如何将按钮绑定到这个 POJO 属性吗?任何帮助将不胜感激

I'm using JSF 2.0.2 with Primefaces 3.0.1

我将 JSF 2.0.2 与 Primefaces 3.0.1 一起使用

采纳答案by BalusC

The binding(and id) attribute of a JSF UI component is resolved during view build time. The #{exampleWrapper}instance is notavailable during the view build time. The view build time is that moment when the XHTML file is parsed into a JSF component tree. The #{exampleWrapper}is only available during the view render time. The view render time is the moment when the JSF component tree generates HTML output.

JSF UI 组件的binding(and id) 属性在视图构建期间解析。该#{exampleWrapper}实例在视图构建期间不可用。视图构建时间是将 XHTML 文件解析为 JSF 组件树的那一刻。该#{exampleWrapper}视图渲染时间时才可用。视图渲染时间是 JSF 组件树生成 HTML 输出的时刻。

Basically, there's only one<p:commandButton>in the component tree which generates its HTML output multiple times as many as the <ui:repeat>iterates. You need to bind it to the #{bean}instead, or to use JSTL <c:forEach>instead of <ui:repeat>. The JSTL tags runs namely during view build time and the <c:forEach>will thus produce physically multiple JSF UI components. But, more than often, binding components to backing beans is unnecessary in JSF 2.x. Whatever functional requirement you've had in mind for which you thought that this is the solution, it can definitely be solved in a better way.

基本上,组件树中只有一个<p:commandButton>生成其 HTML 输出的次数是<ui:repeat>迭代次数的多倍。您需要将其绑定到#{bean},或者使用 JSTL<c:forEach>而不是<ui:repeat>. JSTL 标记即在视图构建期间运行,因此<c:forEach>将在物理上生成多个 JSF UI 组件。但是,在 JSF 2.x 中,通常不需要将组件绑定到支持 bean。无论您有什么功能需求,您认为这是解决方案,它绝对可以以更好的方式解决。