ConversationScoped example
ben_utzer Oct 31, 2011 8:09 AMI wonder why there is no example contained in the Weld distribution that makes use of the conversation scope. Is it by default unusable?
I find the following to be the basic use case for conversation scope:
@Named
@ConversationScoped
public class PersonView implements Serializable {
@Inject
private PersonService personService;
@Inject
private Conversation conversation;
private Person person;
@Produces
@Named
public Person getPerson() {
return person;
}
public void setPerson(Person person){
this.person = person;
}
public String save() {
this.personService.save(person);
this.conversation.end();
return "success";
}
@PostConstruct
public void loadPerson() {
if(this.conversation.isTransient()) {
this.conversation.begin();
}
this.person=personService.ladeAzubi(3L);
}
}
@Stateless
@Named
public class PersonService implements Serializable {
@PersistenceContext
private EntityManager em;
public Person save(Person person) {
return em.merge(person);
}
public Person loadPerson(long id) {
return em.find(Person.class, id);
}
}page1.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:form>
<h:inputText value="#{person.vorname}" />
<h:commandLink action="page2" value="Continue" />
</h:form>
</html>page2.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:form>
<h:inputText value="#{person.lastname}" />
<h:commandLink action="#{personView.save}" value="save" />
</h:form>
</html>success.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">Sauber. Azubi
#{person.lastname}, #{person.firstname} saved successfully.
</html>This is actually not working as expected. The @PostConstructed loadPerson-method is invoked twice so two conversations are started. Once on first get request and the second time on the first postback after I hit Continue
.
Moreover, I found that redirect doesn't seem to be supported. While this wizard works with forwarding when I change the commandlink action on page1 and add ?faces-redirect=true then the conversation id gets added twice and a new conversation is started. Quite sad.
Another thing that seems broken to me is how a page refresh is handled when the conversation has ended: a org.jboss.weld.context.NonexistentConversationException occurs.
Has there been any real world use of the conversation scope or is this just some marketing joke? I also heard about problems handling concurrent ajax requests.