tags:

views:

147

answers:

1

Hi!

I got a problem. I have a bean CreateProjectBean which is RequestScope bean. I want to use Prime Faces component called Collector so i can dynamicly change in view createProject table groupRoleAdapters which is a field of CreateProjectBean. Unforuntely every time i click "add" or "remove" in collector, there is a new request being sent to bean, which means, that GroupRoleAdapters is being created once more - of course empty.

My collector code:

<p:panel header="Add group">
    <h:panelGrid columns="2">
        <h:outputLabel value="Group name: *" for="txt_title"></h:outputLabel>

        <h:selectOneMenu id="groupMenu"
            value="#{createProjectBean.groupRoleAdapter.groupName}">
            <f:selectItems value="#{createProjectBean.groupNames}"
                var="group" itemValue="#{group}" itemLabel="#{group}" />
        </h:selectOneMenu>


        <f:verbatim>
            <br />
        </f:verbatim>

        <h:message styleClass="errorMessage" for="creationForm:groupMenu" />

        <h:outputLabel value="Role name: *" for="txt_title"></h:outputLabel>

        <h:selectOneMenu id="roleMenu"
            value="#{createProjectBean.groupRoleAdapter.roleName}">
            <f:selectItems value="#{createProjectBean.roleNames}" var="role"
                itemValue="#{role}" itemLabel="#{role}" />
        </h:selectOneMenu>

        <f:verbatim>
            <br />
        </f:verbatim>


        <h:message styleClass="errorMessage" for="creationForm:roleMenu" />

        <f:verbatim>
            <br />
        </f:verbatim>


        <p:commandButton value="Add" update="creationForm:out"
            action="#{createProjectBean.reinit}">
            <p:collector value="#{createProjectBean.groupRoleAdapter}"
                addTo="#{createProjectBean.selectedGroupRoleAdapters}"/>
        </p:commandButton>
    </h:panelGrid>
</p:panel>

<f:verbatim>
    <br />
</f:verbatim>

<p:outputPanel id="out">
    <p:dataTable value="#{createProjectBean.selectedGroupRoleAdapters}"
        var="groupRoleAdapter">


        <p:column>
            <f:facet name="header">
                <h:outputText value="Name" />
            </f:facet>
            <h:outputText value="#{groupRoleAdapter.groupName}" />
        </p:column>

        <p:column>
            <f:facet name="header">
                <h:outputText value="Role" />
            </f:facet>
            <h:outputText value="#{groupRoleAdapter.roleName}" />

        </p:column>

        <p:column>
            <f:facet name="header">
                <h:outputText value="Operation" />
            </f:facet>
            <p:commandLink value="Remove" update="creationForm:out">
                <p:collector value="#{groupRoleAdapter}" removeFrom="#{createProjectBean.selectedGroupRoleAdapters}" />
            </p:commandLink>
        </p:column>

    </p:dataTable>
</p:outputPanel>

I'd like to use the same instance of bean while adding and removing groupRoleAdapters from list selectedGroupRoleAdapters (represented by collector table), but create new instance of bean every time i try to create new project, so changing scope to sessionScope is not what i can accept.

Thanks in advance for every help.

I attach full code of that view:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.prime.com.tr/ui"&gt;
<ui:composition template="/templates/template.xhtml">
    <ui:define name="head">
        <title>Create Project</title>
        <link rel="stylesheet" type="text/css"
            href="#{facesContext.externalContext.requestContextPath}/styles/style.css" />
    </ui:define>
    <ui:define name="content">
        <div class="mainTable">
        <center><f:view>
            <h:outputText id="error" rendered="false" />
            <h:message styleClass="errorMessage" for="error" />
            <h:form id="creationForm">
                <h:panelGrid columns="2" width="420">

                    <h:panelGroup width="300">
                        <h:outputLabel styleClass="formLabel" value="Name: "></h:outputLabel>
                    </h:panelGroup>

                    <h:panelGroup>
                        <h:inputText styleClass="formField" id="name"
                            value="#{createProjectBean.project.name}" required="true">
                            <f:validateLength minimum="3" />
                        </h:inputText>
                    </h:panelGroup>

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <h:message styleClass="errorMessage" for="creationForm:name" />

                    <h:panelGroup>
                        <h:outputLabel styleClass="formLabel" value="Short Name: " />
                    </h:panelGroup>

                    <h:panelGroup>
                        <h:inputText styleClass="formField" id="shortname"
                            value="#{createProjectBean.project.shortname}" required="false">
                            <f:validateLength maximum="8" />
                        </h:inputText>
                    </h:panelGroup>

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <h:message styleClass="errorMessage" for="creationForm:shortname" />

                    <h:panelGroup>
                        <h:outputLabel styleClass="formLabel" value="Homepage: " />
                    </h:panelGroup>

                    <h:panelGroup>
                        <h:inputText styleClass="formField" id="homepage"
                            value="#{createProjectBean.project.homepage}" required="false">
                        </h:inputText>
                    </h:panelGroup>

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <h:message styleClass="errorMessage" for="creationForm:hostname" />

                    <h:panelGroup>
                        <h:outputLabel styleClass="formLabel" value="Description: " />
                    </h:panelGroup>

                    <h:panelGroup>
                        <h:inputTextarea styleClass="formField" id="description"
                            value="#{createProjectBean.project.description}" required="false"
                            cols="50" rows="10" />
                    </h:panelGroup>

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <h:message styleClass="errorMessage" for="creationForm:description" />

                    <h:panelGroup>
                        <h:outputLabel styleClass="formLabel" value="Plugins: " />
                    </h:panelGroup>

                    <h:selectManyListbox id="pluginBox"
                        value="#{createProjectBean.selectedPluginNames}">
                        <f:selectItems value="#{createProjectBean.pluginNames}"
                            var="plugin" itemValue="#{plugin}" itemLabel="#{plugin}" />
                    </h:selectManyListbox>

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <h:message styleClass="errorMessage" for="creationForm:pluginBox" />

                    <h:panelGroup>
                        <h:outputLabel styleClass="formLabel" value="Tags: " />
                    </h:panelGroup>

                    <h:selectManyListbox id="tagBox"
                        value="#{createProjectBean.project.tags}">
                        <f:selectItems value="#{createProjectBean.allTags}" var="tag"
                            itemValue="#{tag}" itemLabel="#{tag.name}" />
                    </h:selectManyListbox>

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <h:message styleClass="errorMessage" for="creationForm:tagBox" />

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <p:panel header="Add group">
                        <h:panelGrid columns="2">
                            <h:outputLabel value="Group name: *" for="txt_title"></h:outputLabel>

                            <h:selectOneMenu id="groupMenu"
                                value="#{createProjectBean.groupRoleAdapter.groupName}">
                                <f:selectItems value="#{createProjectBean.groupNames}"
                                    var="group" itemValue="#{group}" itemLabel="#{group}" />
                            </h:selectOneMenu>

                            <f:verbatim>
                                <br />
                            </f:verbatim>

                            <h:message styleClass="errorMessage" for="creationForm:groupMenu" />

                            <h:outputLabel value="Role name: *" for="txt_title"></h:outputLabel>

                            <h:selectOneMenu id="roleMenu"
                                value="#{createProjectBean.groupRoleAdapter.roleName}">
                                <f:selectItems value="#{createProjectBean.roleNames}" var="role"
                                    itemValue="#{role}" itemLabel="#{role}" />
                            </h:selectOneMenu>

                            <f:verbatim>
                                <br />
                            </f:verbatim>

                            <h:message styleClass="errorMessage" for="creationForm:roleMenu" />

                            <f:verbatim>
                                <br />
                            </f:verbatim>


                            <p:commandButton value="Add" update="creationForm:out"
                                action="#{createProjectBean.reinit}">
                                <p:collector value="#{createProjectBean.groupRoleAdapter}"
                                    addTo="#{createProjectBean.selectedGroupRoleAdapters}"/>
                            </p:commandButton>
                        </h:panelGrid>
                    </p:panel>

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <p:outputPanel id="out">
                        <p:dataTable value="#{createProjectBean.selectedGroupRoleAdapters}"
                            var="groupRoleAdapter">


                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Name" />
                                </f:facet>
                                <h:outputText value="#{groupRoleAdapter.groupName}" />
                            </p:column>

                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Role" />
                                </f:facet>
                                <h:outputText value="#{groupRoleAdapter.roleName}" />

                            </p:column>

                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Operation" />
                                </f:facet>
                                <p:commandLink value="Remove" update="creationForm:out">
                                    <p:collector value="#{groupRoleAdapter}"
                                        removeFrom="#{createProjectBean.selectedGroupRoleAdapters}" />
                                </p:commandLink>
                            </p:column>

                        </p:dataTable>
                    </p:outputPanel>

                    <f:verbatim>
                        <br />
                    </f:verbatim>

                    <h:commandButton value="Create" styleClass="formButton"
                        action="#{createProjectBean.create}" />
                </h:panelGrid>
            </h:form>
        </f:view></center>
        </div>
    </ui:define>
</ui:composition>
</html>
A: 

If you're already on JSF 2.0, just put the bean in view scope, by either @ViewScoped annotation or by <managed-bean-scope>view</managed-bean-scope> in faces-config.xml.

If you're still on JSF 1.x (which I'm afraid of since you're using those ugly JSF 1.0/1.1-mandatory <f:verbatim> tags), then you have to either put bean in session scope, eventually in combination with an unique request scoped parameter which is retained in subsequent requests by h:inputHidden or f:param, or to grab a 3rd party library with a component which is able to save the state of an entire request scoped bean for the subsequent request, like Tomahawk's t:saveState.

BalusC
In fact I use JSF 2.0 and <f:verbatim> just survived migration from 1.x. I tried to make bean ViewScoped, but it acts the same as in request scope. I can add only one adapter to collector list and when i try to add another one, the first just disapears. It acts normal in session scope, but i have reasons I cant't use that one. I'll try solutions for older JSF, but i doubt that my supervisor will agree to use some new libraries. Technology is given strictly. Anyway, thanks.
aerion
The view scope fails whenever you bind a component to the bean using `binding` or when you use JSTL `c:if`/`c:forEach` in view. Can you verify this all?
BalusC
Nope, i use neither. I attach full code of my view.
aerion