File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Putting values in the requestScope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Putting values in the requestScope" Watch "Putting values in the requestScope" New topic

Putting values in the requestScope

Paolo Pinkel
Ranch Hand

Joined: May 04, 2011
Posts: 35
I'm trying to put values in the requestScope from an action method.

This is what the method looks like:

This is a snippet of my form:

(Yep, it's in a dataTable.)

It will put display the value (2nd inputText), but not the key (1st inputText). That's rather strange. (Inside the action method, the key looks fine.)

Do you see anything wrong there?

Thank you!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Paolo Pinkel wrote:I'm trying to put values in the requestScope from an action method.

Do you see anything wrong there?

Yes. You're creating a whole lot of clunky server-specific code to do what JSF was intended to do easily as one of its core functions.

JSF can define Request-scope beans in faces-config and then reference them as managed properties. The managed properties can be injected into other managed beans and/or referenced via EL expressions in View definitions. And you never have to get within 100 kilometers of FacesContext or any other javax.faces objects to do that.

You've made your job a little more difficult by making 2 request objects, each of which is a simple String-class object. Usually it's easier to define a custom class that holds both key and value with property accessor methods for each. You can then inject this object into a managed bean that initializes its key and value properties, which results in a view almost identical to the one you've coded except that instead of the name "requestScope", you'd substitute the name that your custom object was published under.

There is one "gotcha", however. You can't inject a request-scope object into a session-scope bean since the destination object's lifespan has to be shorter or equal than the injected object. This sometimes means that the "obvious" arrangement of objects has to be turned around.

One final warning. JSF uses postbacks that cause the same form to be sent and received multiple times. That doesn't work well with Request scope, since the Request scope objects get destroyed and created from scratch on each request. So Request scope is much less usable in JSF than it is in other J2EE frameworks.

An IDE is no substitute for an Intelligent Developer.
Paolo Pinkel
Ranch Hand

Joined: May 04, 2011
Posts: 35
Thanks for this enlightening comment, Tim! I'm new to JSF, so... thank you! It's working now!
I agree. Here's the link:
subject: Putting values in the requestScope
It's not a secret anymore!