Multiple DataModels scoped to PAGE context, only 1 Factory called when returning to page
csherstan Apr 3, 2008 7:51 PMI have two h:dataTable components on the same page (home.xhtml). The associated lists (list1 & list2) are marked with @DataModel(scope=ScopeType.PAGE) in PageTestBean. Each list has its own @Factory. Objects are displayed in the first or second table depending if the number in member variable tableNum is 1 or 2.
Clicking on an item in one of the tables takes navigates to toggle.xhtml, where I toggle the tableNum value between 1 and 2. Toggling takes us back to home.xhtml. When home.xhtml is loaded this second time only the factory for the first dataTable is called. So what ends up happening is that the items list in the first dataTable are updated while those in the second are not.
I've played around with this a fair bit and I'm thinking this might be a bug. My understanding is that the factories of both lists should be getting called when the page is reloaded. But I'm new to Seam so maybe someone else can see my problem.
//Table1.java package com.cs.PageTest.entity; ... @Entity @Table(name = "table1") public class Table1 implements java.io.Serializable { private int id; private int tableNum; public Table1() { } @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false) @NotNull public int getId() { return this.id; } public void setId(int id) { this.id = id; } @Column(name = "table_num", unique = false, nullable = false) @NotNull public int getTableNum() { return this.tableNum; } public void setTableNum(int tableNum) { this.tableNum = tableNum; } }
//PageTestBean.java package com.cs.PageTest; ... @Stateful @Name("pageTest") public class PageTestBean implements PageTest { @In(create=true) private EntityManager entityManager; @Out(required=false) Table1 table1; @DataModel(scope=ScopeType.PAGE) private List<Table1> list1; @DataModel(scope=ScopeType.PAGE) private List<Table1> list2; @Factory("list1") public void populateList1() { list1 = entityManager.createQuery( "SELECT t " + "FROM Table1 AS t " + "WHERE t.tableNum = 1") .getResultList(); } @Factory("list2") public void populateList2() { list2 = entityManager.createQuery( "SELECT t " + "FROM Table1 AS t " + "WHERE t.tableNum = 2") .getResultList(); } public String viewEntryButton(Table1 table1) { this.table1 = table1; return "togglePage"; } @Remove public void destroy(){ } }
//PageTest.java package com.cs.PageTest; ... public interface PageTest { public String viewEntryButton(Table1 table1); public void populateList1(); public void populateList2(); public void destroy(); }
//Toggle.java package com.cs.PageTest; ... public interface Toggle { public String toggleButton(); public Table1 getTable1(); public void create(); public void remove(); }
//ToggleBean.java package com.cs.PageTest; ... @Name("toggle") @Stateful public class ToggleBean implements Toggle{ @In Table1 table1; @In EntityManager entityManager; @Create @Begin(join=true) public void create() { } public Table1 getTable1() { return this.table1; } public String toggleButton() { table1 = entityManager.merge(table1); table1.setTableNum(table1.getTableNum() == 1 ? 2 : 1); return "home"; } @Remove public void remove() { } }
<!--home.xhtml--> <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:s="http://jboss.com/products/seam/taglib" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <body> <h1>Test</h1> <div id="messages"> <h:messages globalOnly="true" styleClass="message" id="globalMessages"/> </div> <h:form> <h:dataTable var="table11" value="#{list1}" > <f:facet name="header"> List 1 </f:facet> <h:column> <f:facet name="header"> <h4></h4> </f:facet> <h:commandLink action="#{pageTest.viewEntryButton(table11)}"> #{table11.id} </h:commandLink> </h:column> </h:dataTable> </h:form> <h:form> <h:dataTable var="table12" value="#{list2}" > <f:facet name="header"> List 2 </f:facet> <h:column> <f:facet name="header"> <h4></h4> </f:facet> <h:commandLink action="#{pageTest.viewEntryButton(table12)}"> #{table12.id} </h:commandLink> </h:column> </h:dataTable> </h:form> <h:form> <s:link propagation="false" action="home.xhtml" value="refresh"/> </h:form> </body> </ui:composition>
<!--toggle.xhtml--> <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:s="http://jboss.com/products/seam/taglib" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <body> <h1>Toggle</h1> <div id="messages"> <h:messages globalOnly="true" styleClass="message" id="globalMessages"/> </div> <h:form> <h:panelGrid> <h:outputText value="id: #{toggle.table1.id}"/> <h:outputText value="table_num: #{toggle.table1.tableNum}"/> <h:commandButton action="#{toggle.toggleButton}" value="Toggle"/> </h:panelGrid> </h:form> </body> </ui:composition>
<!--pages.xml--> <?xml version="1.0" encoding="UTF-8"?> <pages xmlns="http://jboss.com/products/seam/pages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.0.xsd" no-conversation-view-id="/home.xhtml" login-view-id="/login.xhtml"> <page view-id="*"> <navigation> <rule if-outcome="home"> <redirect view-id="/home.xhtml"/> </rule> </navigation> </page> <page view-id="/home.xhtml"> <navigation> <rule if-outcome="togglePage"> <redirect view-id="/toggle.xhtml"/> </rule> </navigation> </page> <exception class="org.jboss.seam.framework.EntityNotFoundException"> <redirect view-id="/error.xhtml"> <message>Not found</message> </redirect> </exception> <exception class="javax.persistence.EntityNotFoundException"> <redirect view-id="/error.xhtml"> <message>Not found</message> </redirect> </exception> <exception class="javax.persistence.OptimisticLockException"> <end-conversation/> <redirect view-id="/error.xhtml"> <message>Another user changed the same data, please try again</message> </redirect> </exception> <exception class="org.jboss.seam.security.AuthorizationException"> <redirect view-id="/error.xhtml"> <message>You don't have permission to do this</message> </redirect> </exception> <exception class="org.jboss.seam.security.NotLoggedInException"> <redirect view-id="/login.xhtml"> <message>Please log in first</message> </redirect> </exception> <exception class="javax.faces.application.ViewExpiredException"> <redirect view-id="/error.xhtml"> <message>Your session has timed out, please try again</message> </redirect> </exception> <exception> <redirect view-id="/error.xhtml"> <message>Unexpected error, please try again</message> </redirect> </exception> </pages>