Passing data into a new conversation (in JSF)
vesuvius.vesuvius.prime.gmail.com Mar 2, 2010 4:19 PMI was wondering about the best way to pass data into a newly-created long-running conversation without polluting the conversation with unnecessary beans.
A very simplistic scenario would be: I am working with a JSF page (Page1) whose backing bean is @ConversationScoped for some reason (for example, it must be able to survive a post-redirect-get operation). From that page, I wish to navigate to another page (Page2) which must start a new long-running conversation. And I wish to pass data (say, customer name) from the first page into the second page. But I don't want the first page's backing bean to be part of the newly created conversation.
I don't know the best way -- I'm asking about it here. But I can think of two ways to pass data:
One way would be to pass the data REST-style (as request parameters):
page1.xhtml:
<h:inputText value="#{page1.custName}" /><br/> <h:commandButton value="To Page2" action="#{page1.toPage2}"/>
Page1.java:
@ConversationScoped @Named public class Page1 implements Serializable { public String toPage2() { return "page2?faces-redirect=true&custName=" + custName; } ...
page2.xhtml:
<f:metadata> <f:viewParam name="custName" value="#{page2.custName}"/> </f:metadata> ...
Page2.java:
@ConversationScoped @Named public class Page2 implements Serializable { @Inject Conversation conversation; @PostConstruct public void init() { conversation.begin(); } ...
I have no idea if I'm doing it right, but this works.
However, I'm interested in the other way -- passing data via server-side state instead of passing it as request parameters:
Page1.java:
@ConversationScoped @Named public class Page1 implements Serializable { @Inject Page2 page2; public String toPage2() { page2.setCustName(custName); return "page2"; } ...
Here's my real problem: when using this method, and if Page2.java starts a long-running conversation (see the Page2 snippet further above that starts the conversation), then Page1.java will be placed in the long-running conversation too. I don't want it there. I want ONLY Page2.java in the conversation. How can I do that? I understand it's impossible to remove beans from a context, but I don't quite understand why? Why shouldn't I be allowed to remove Page1 from the conversation? I don't need it there. Or... what can I do, so that Page1 doesn't get included into the conversation in the first place?
If you reached this point, thank you for your patience! I just want to learn some 'best practices'.