This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSF and the fly likes Am I misunderstanding the basics of JSF 2.0? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » JSF
Bookmark "Am I misunderstanding the basics of JSF 2.0?" Watch "Am I misunderstanding the basics of JSF 2.0?" New topic

Am I misunderstanding the basics of JSF 2.0?

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
I have a problem (or misunderstanding) with JSF 2.0...
I have 2 JSPs, one to enter firstname & surname and the other to display then 'upload' (not implemented yet) to a DB.
Now moving from page1 to page 2 works and the labels display the values OK, but at the point of submitting on page2 the values of
firstname & surname are 'NULLED',
I presumed the values were still active as they are displayed in the labels.
Isn't the sort of the point of the JSF framework or as I say, am I missing the point?

Thanks in advance.

Here is the basic coding...

Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Hi Keith,

just two short hints (I don't have more time at the moment): JSF 2.0 uses XHTML/Facelets as its preferred templating technology. Not JSPs! And you should check if the FacesServlet is setup correctly in your web.xml descriptor. I think you will find enough sources for more information with these hints ;-)

Donald Jackson

Joined: Mar 09, 2009
Posts: 19
Try using:

<h:inputText label="First Name" value="#{flash.firstname}"/>
<h:inputText label="Last Name" value="#{flash.surname}"/>

In your next page use:

<h: outputLabel value="#{flash.firstname} #{flash.surname}" />

Then you can access the value in your bean with:

Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
setFirstname ((String) flash.get("firstname"));
setSurname ((String) flash.get("surname"));

You may have to use #{flash.keep.firstname} in your EL expression in your page tag though. I'm not sure. Using the "keep" function in the flash tag attribute allows the saved data to stay through another iteration of the view lifefcycle. It will take the data from one page view to the next only unless you use 'keep' to maintain it. You'll have to look into it, but I think this points you in the right direction to do what you want. Atleast this is one way.

Good luck,

Tom Fulton
Ranch Hand

Joined: Mar 30, 2006
Posts: 96
It shouldn't actually be necessary (although it might be nice) to use flash scope to make this work correctly. The managed bean is session-scoped, so the values should be retained across multiple pages, regardless of whether they are forwards or redirects.

However, one thing I noticed about your code was the signature of the create( ) method. You specified this when you said . The action attribute expects a method that returns a String, and takes no parameters...yours is returning void. Honestly, I'm not sure what that would do...and in JSF 1.2, that would have failed deployment.

If you want to stay on the same page, you can use an actionListener, which takes an ActionEvent parameter (which you don't have to do anything with) and returns void. If you want to transition to a new page, use action, and have the method return the name of the page you wish to transition to.

I'm not sure that's the reason your fields are nulled, but it sure won't work correctly until you change it in one form or another.
Aman Goel

Joined: Jan 14, 2010
Posts: 15
It will work fine. Just try to do one thing that in page2.jsp try to get only one name
Aman Goel

Joined: Jan 14, 2010
Posts: 15
Worst Solution.... Do not misguide please
I agree. Here's the link:
subject: Am I misunderstanding the basics of JSF 2.0?
jQuery in Action, 3rd edition