Scopes problem
amitev Jul 10, 2007 4:49 AMHi all i have the following scenario - page for creating a new project that has a few input fields for project info and data table with components related to that project. Here is the page code:
<h:form> <table> <tr> <td colspan="3"> <h:messages layout="table" globalOnly="true" styleClass="message" id="globalMessages" /> </td> </tr> <tr> <td> #{projMsg.name}: </td> <td> <h:inputText id="prj_name" value="#{project.name}" required="true"> </h:inputText> </td> </tr> <tr> <td colspan="2" align="center"> <h:message styleClass="errorMessage" for="prj_name" /> </td> </tr> <tr> <td> #{projMsg.url}: </td> <td> <h:inputText id="prj_url" value="#{project.url}"> </h:inputText> </td> </tr> <tr> <td> #{projMsg.description} </td> <td> <h:inputTextarea rows="4" cols="20" id="prj_desc" value="#{project.description}" /> </td> <td> <h:message for="prj_desc" /> </td> </tr> <tr> <td> </td> <td> <h:commandButton value="#{otherMsg.create}" action="#{projectAction.createProject}" /> </td> <td> </td> </tr> <tr> <td colspan="2"> <rich:dataTable value="#{project.componentCollection}" var="comp"> <f:facet name="header"> <rich:columnGroup> <rich:column colspan="2"> #{projMsg.components} </rich:column> </rich:columnGroup> </f:facet> <rich:column> #{comp.name} </rich:column> <rich:column> #{fn:length(comp.name)} </rich:column> <f:facet name="footer"> <rich:columnGroup> <rich:column> <h:inputText value="#{component.name}"></h:inputText> </rich:column> <rich:column> <h:commandButton action="#{projectAction.addComponent}" value="Add" /> </rich:column> </rich:columnGroup> </f:facet> </rich:dataTable> </td> </tr> </table> </h:form>
Here is the code of the backing bean:
@Stateful @Name("projectAction") public class ProjectActionBean implements com.amitev.bts.ejb.ProjectActionLocal { @PersistenceContext EntityManager em; @In private User user; @In @Out private Project project; @In @Out private Component component; @In private FacesMessages facesMessages; @Restrict("#{identity.loggedIn}") public String createProject() { List results = em.createQuery("from Project pr where pr.name = #{projectAction.project.name}").getResultList(); if ( results.size()==0 ) { project.setOwnerId(user); em.persist(project); } else { facesMessages.addToControlFromResourceBundle("prj_name","project_exists"); } return null; } @Restrict("#{identity.loggedIn}") public void addComponent() { String componentName = component.getName().trim().toLowerCase(); boolean contains = false; System.out.println(project.getComponentCollection().size()); for (Component comp: project.getComponentCollection()) { System.out.println(comp.getName()); if (componentName.equalsIgnoreCase(comp.getName())) { contains = true; } } if (!contains) { project.getComponentCollection().add(component); component = new Component(); } } @Destroy @Remove public void destroy() {} }
When i add a component the page is redisplayed and the component is shown in the data table. But when i try to add a new component and press the back button i see that the component collection is empty (probably new Project instance is injected). What's the best way to solve this problem with seam?