aspose file tools*
The moose likes JSF and the fly likes How do we pass the list of objects selected from one screen  to another screen.? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How do we pass the list of objects selected from one screen  to another screen.? " Watch "How do we pass the list of objects selected from one screen  to another screen.? " New topic
Author

How do we pass the list of objects selected from one screen to another screen.?

prasad kakani
Ranch Hand

Joined: Jul 15, 2008
Posts: 59

How do we pass the list of objects selected from one screen to another screen on submit button of first screen.? what i meant is, how the data transfer can be possible from one screen to another screen?
Can anyone please give any hint?
Dinesh Bang
Ranch Hand

Joined: Jun 15, 2006
Posts: 52
In the mapped bean to the page , on action you can pass the object from one bean to another bean (tagged to another page)


SCJP 1.4,SCBCD 1.3, SCWCD 1.5, ???
prasad kakani
Ranch Hand

Joined: Jul 15, 2008
Posts: 59
Hi Dinesh,

<h:commandButton action="bean.passListOfObjects" value="submit form"/>

SubmitFormBackingBean.java (name="bean") action method
----------------------------------------------------

List myList ;

setter and getter methods for myList;

public String passListOfObjects(){

myList= new ArrayList();
myList.add("a");
myList.add("b");
myList.add("c");
//From here dont know whether this is correct way or not.
FacesContext facesContext = FacesContext.getCurrentInstance();
Application app = facesContext.getApplication();
ExpressionFactory ef = app.getExpressionFactory();
ValueExpression ve = ef.createValueExpression(el, "#{bean.myList}", List.class);


return "gotoanotherpagefordisplay"

}
navigation rule:
------------------

<navigation-rule>
<display-name>jsf/Form.xhtml</display-name>
<from-view-id>/jsf/Form.xhtml</from-view-id>
<navigation-case>
<from-outcome>gotoanotherpagefordisplay</from-outcome>
<to-view-id>/jsf/Result.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

ResultBackingBean.java
-----------------------------

List myList;
public List getAllMyListValues(){


// Here how do we get the list from SubmitFormBackingBean.java.
myList = getListfromFirstBean();
return myList;
}
Now i need to use myList object in Result.xhtml..

Just i want to know how do i get the list in the second bean.. Help would be grealy appriciated.
Thanks
Dinesh Bang
Ranch Hand

Joined: Jun 15, 2006
Posts: 52
A simple way would be :

FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("key", object) and retrieve from map in second bean.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16058
    
  21

Any time you access the FacesContext, the first thing you should ask yourself is "Am I doing this wrong?"

JSF was designed to use as little JSF code as possible in order to promote code reuse and better unit testing.

In the case of data transfer, the preferable way to get data from one place to another is to use Inversion of Control techniques such as injection. That way you can do everything you need with POJOs and not tie the code to the framework. Instead you let the framework (JSF) do the tying for you.

Even worse than gratuitous use of the javax.faces classes and methods, however, is to handle the data transfer in the View. Doing so will almost always increase the amount of data being transferred between client and server, plus it potentially exposes security leveraging. Meaning that anything that can goes to the client may come back hacked, so ideally you want to minimize what comes back, not add more to it.

Plus, coding stuff like that on the View blurs or even outright violates the Separation of Concerns between Model, View and Controller, which makes things a lot more expensive to debug and maintain,


Customer surveys are for companies who didn't pay proper attention to begin with.
prasad kakani
Ranch Hand

Joined: Jul 15, 2008
Posts: 59
Many thanks to Tim and Dinesh,

Hello Tim,

Can you please explain through a piece of code with my example?


Thanks
prasad
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16058
    
  21

I don't know about using your code specifically, but I'll give it in abstract terms, and hopefully that will help. Incidentally, http://www.coderanch.com/how-to/java/UseCodeTags and your sample code and XML will be a lot easier to read.

Let's start with the basics. You have (presumably) 2 Managed Beans. One is the "source" bean and one is the "target" bean. The source bean is the one that is providing the model for your selection list and you are looking to make it possible for the target bean to determine what was selected. Note: for best results, try this first using all session-scope objects. Once you understand the process you can shorten scopes as needed.

If you want to make the selection in one web page (View) and use the selections on another page, you actually have 2 options. One is to set the "value" of the selection on the source page to directly reference a property in the target bean, the other is to set the value to be in the source bean and have the target bean retrieve it from there. Which option you choose is up to you. Partly it depends on how you want to manage backing bean scopes, since directly referencing the target bean means that its scope has to include the first page as well as the second page. Partly it depends on how generic you want your code to be.

If you address the targer bean directly, there's really nothing to code at all other than the target property and its set/get methods. It's not quite as simple when the value is set on the source bean, but here's one way to do it:

1. Inject the target bean into the source bean as a ManagedProperty either via faces-config.xml or (JSF2 only) @ManagedProperty annotation.
2. Add logic to the source bean's action method that navigates to the target bean:

prasad kakani
Ranch Hand

Joined: Jul 15, 2008
Posts: 59
Many Many thanks for your exceptional sugestions.. Yout explanation is also very good..

Thanks
Prasad

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How do we pass the list of objects selected from one screen to another screen.?