In Seam how do I use a @DataModelSelection with the Conversation scope?
Environment
- JBoss Enterprise Application Platform (EAP) 5
- Seam 2.2
Issue
- How do I build a search form page with temporary conversation where the selected value won't be null?
Resolution
-
Here is a working example of using a @DataModelSelection with an Entity Called Person
-
Person.java
@Entity
public class Person implements Serializable
{
private Long id;
private String name;
@Id @GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
@Length(max = 20)
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
- SomeAction.java
@Name("someAction")
@Scope(ScopeType.CONVERSATION)
public class SomeAction
{
@Logger private Log log;
@In EntityManager entityManager;
@DataModel
private List<Person> persons;
@Out(required = false)
@DataModelSelection
private Person selectedPerson;
@Factory("persons")
public void findSecurityAccounts() {
persons = (List<Person>)entityManager.createQuery("select person from Person person").getResultList();
}
public void someAction()
{
log.info("selected Person is: " + selectedPerson.getName());
}
public void createPersons() {
Person p = new Person();
p.setName("Person 1");
entityManager.persist(p);
p = new Person();
p.setName("Person 2");
entityManager.persist(p);
p = new Person();
p.setName("Person 3");
entityManager.persist(p);
}
}
- someAction.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"
xmlns:rich="http://richfaces.org/rich"
template="layout/template.xhtml">
<ui:define name="body">
<rich:panel>
<f:facet name="header">Selected Person</f:facet>
Selected Person: #{selectedPerson.name}
</rich:panel>
<h:form>
<rich:panel>
<f:facet name="header">Person List</f:facet>
<div class="results">
<h:outputText value="No person exists"
rendered="#{empty persons}"/>
<h:dataTable id="personList" var="p"
value="#{persons}"
rendered="#{not empty persons}">
<h:column>
<f:facet name="header">Id</f:facet>
#{p.id}
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<h:commandButton id="p"
value="#{p.name}"
action="#{someAction.someAction}" />
</h:column>
</h:dataTable>
</div>
<br />
<h:commandButton value="Create Persons" action="#{someAction.createPersons}" />
</rich:panel>
</h:form>
</ui:define>
</ui:composition>
- Full source to reproduce is attached
Attachments
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
