根据提供的属性初始化复合组件

Xtr*_*ica 11 jsf composite-component jsf-2

我正在用Mojarra JSF编写自定义表复合组件.我也试图将该组合绑定到支持组件.目的是能够指定表在复合属性中具有的元素数量,稍后绑定的后备组件将在呈现视图之前自动生成元素本身.我有这个示例代码:

主页:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:comp="http://java.sun.com/jsf/composite/comp">
<h:head />
<body>
    <h:form>
        <comp:myTable itemNumber="2" />
    </h:form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

myTable.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:h="http://java.sun.com/jsf/html">

<h:body>
    <composite:interface componentType="components.myTable">
        <composite:attribute name="itemNumber" 
            type="java.lang.Integer" required="true" />
    </composite:interface>

    <composite:implementation>
        <h:dataTable value="#{cc.values}" var="value">
            <h:column headerText="column">
                #{value}
                <h:commandButton value="Action" action="#{cc.action}" />
            </h:column>
        </h:dataTable>
    </composite:implementation>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

MyTable.java:

@FacesComponent("components.myTable")
public class MyTable extends UINamingContainer {

    private List<String> values = new ArrayList<String>();

    public void action() {
        System.out.println("Called");
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        // Initialize the list according to the element number
        Integer num = (Integer) getAttributes().get("itemNumber");
        for (int i = 0; i < num; i++) {
            values.add("item" + i);
        }
        super.encodeBegin(context);
    }

    public List<String> getValues() {
        return values;
    }

}
Run Code Online (Sandbox Code Playgroud)

问题是表格被正确渲染(在这种情况下有两个项目),但按下线条上的按钮时不会调用action方法.

如果我按照wiki页面查看复合组件,我可以通过这种方式使它工作,但是List每次都必须初始化getValues(),将逻辑引入getter方法:-(.

有什么想法吗?这似乎是一个与重写encodeBegin方法有关的麻烦.我也试过初始化它markInitialState,但那里的属性还没有...


测试了Mojarra 2.1.27 + Tomcat 6-7和Mojarra 2.2.5 + Tomcat 7

Bal*_*usC 19

至于原因,UIComponent实例本身就是请求作用域.回发有效地创建了一个全新的实例,其属性如values重新初始化为默认值.在您的实现中,它仅在期间填充encodeXxx(),在decode()其中行动事件需要排队并因此太晚之后很久就会被调用.

您最好在组件初始化期间填充它.如果你想@PostConstructUIComponent实例提供类似的钩子,那么postAddToView事件是一个很好的候选者.将组件实例添加到组件树后,将直接调用此方法.

<cc:implementation>
    <f:event type="postAddToView" listener="#{cc.init}" />
    ...
</cc:implementation>
Run Code Online (Sandbox Code Playgroud)

private List<String> values;

public void init() {
    values = new ArrayList<String>();
    Integer num = (Integer) getAttributes().get("value");

    for (int i = 0; i < num; i++) {
        values.add("item" + i);
    }
}
Run Code Online (Sandbox Code Playgroud)

(encodeBegin()如果它没有做任何有用的东西,则删除该方法)

另一种方法是在getValues()方法中进行延迟初始化.