Saving @ViewScoped property states in composite components

This question is related to my previous version of component initialization . If I have a built-in composite component and a @ViewScopedbean, I want certain properties of the managed bean to be reflected in the component. The transition of the property value to the component is performed using the composite attribute .

This is the code that I have right now, consisting of @ManagedBean, that determines the random line number that will be displayed in the component. The component generates h:dataTablewith h:commandButtonin each line by clicking this button, it simply calls the method in the component support code, and performs a postback in the same form .

Managed bean:

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private Integer rowNum;

    public Integer getRowNum() {
        return rowNum;
    }

    /**
     * Initializes the rowNum property with a value between 1 and 9
     */
    public void init() {
        if (!FacesContext.getCurrentInstance().isPostback()) {
            rowNum = new Random().nextInt(10) + 1;
            System.out.println("Bean initialized");
        }
    }

    public void setRowNum(Integer rowNum) {
        this.rowNum = rowNum;
    }

}

index.xhtml:

<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>
    <f:event type="preRenderView" listener="#{bean.init}" />
    <h:form>
        <comp:myTable itemNumber="#{bean.rowNum}" />
    </h:form>
</body>
</html>

MyTable.java:

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

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

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

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

    /**
     * Initializes the 'values' List based in 'itemNumber' attribute
     */
    public void init() {
        System.out.println("Component initialized");
        Integer num = (Integer) getAttributes().get("itemNumber");
        for (int i = 0; i < num; i++) {
            values.add("item" + i);
        }
    }

}

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"
    xmlns:f="http://java.sun.com/jsf/core">

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

    <composite:implementation>
        <f:event type="preRenderView" listener="#{cc.init}" />
        <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>

, , Bean, , ,

@ManagedBean, init(), preRenderView. @ManagedBean , , . , , , , .

h:commandButton List . , init(). , GET.

getValues(), init() , preRenderView, , , .

postAddToView preRenderView , @ManagedBean.


2.1.27 - 2.2.5 + Tomcat 7

+3

All Articles