wood burning stoves 2.0*
The moose likes JSF and the fly likes Data Population in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Data Population in JSF" Watch "Data Population in JSF" New topic
Author

Data Population in JSF

Sandip Chaudhuri
Greenhorn

Joined: Dec 27, 2004
Posts: 26
Hi,
I am pretty new to JSF and very confused about its working as well. This may be cause i am still thinking in the struts mode which is causing the problem.

What i have is a simple senario. (using MyFaces)

the User clicks on a link or submits a page. That link has a nivigation

<navigation-rule>
<from-view-id>/helloWorld.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/Profile.jsp</to-view-id>
</navigation-case>
</navigation-rule>

I want to pre populate some values in the bean used in Profile.jsp. How do i do it?
IN struts we had the action class which would allow me to prepopulate. Thats is if i extended LookupDispatchAction then calling the right method would allow me to populate the bean. How do i do this ?
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
If you are using ibmjsf.jar , make use of the post-renderer attribute of <hx:scriptcollector. the post-renderer method will get executed after the page is rendered and you can populate the components with some values when the page gets displayed first.

other option is to go with binding for each components. Try to find some examples on the net, its pretty simple.

Thanks,


SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
Sandip Chaudhuri
Greenhorn

Joined: Dec 27, 2004
Posts: 26
Could you please give me some links or something for the examples?
Also i am using MyFaces and my beans are all always in request scope.
Andrew Och
Ranch Hand

Joined: Mar 19, 2004
Posts: 32
In order to populate Profile.jsp you set the value of each input field with the method in your backing bean.

I give an example from my project where we want to modify a device values such as name, msisdn, location etc.

<af:inputText id="location" required="true" value="#{ModifyDeviceManager.location}" shortDesc="#{msg['global.device.location.desc']}" />

ModifyDeviceManager is my backing bean and it has an attribute called: location and corresponding getLocation( ) and setLocation( String s) methods. MyFaces uses Java reflection to first get values and on submit set values. If the submit method has no return String ie void modifyDevice() then the same jsp page will be displayed again, with any messages you have added to the context and the getter methods run again showing you that the input fields have been updated with the latest entry.

public String getLocation() {
return this.device.getLocation();
}

public void setLocation(String deviceLocation) {
this.device.setLocation(deviceLocation);
}

So in your case I am guessing you want the getter methods for a user profile to access the database and display the results to the user and if the user updates and submits, then the new values are stored in the database using the setter methods.
Sandip Chaudhuri
Greenhorn

Joined: Dec 27, 2004
Posts: 26
Oh no i am not using the getters and setters that way. That would be impracticle won't it? All i am supposed to do is populate the Value Object and send it to the the interface to deal with.

The retriving and saving of data is done by methods in the backing bean whih pass the Value Object to the Interface(Spring). What i am looking for is how do i fill up the backing bean of the second jsp from the first in such way that the server populates its view.
Andrew Och
Ranch Hand

Joined: Mar 19, 2004
Posts: 32
I have to admit that I am a little confused. However if I was to take values in from a user and then display results based on the user's input one way would be to wrap it in a DataModel object (if getters and setters are not practical).

DataModel results = new ListDataModel();
results.setWrappedData(ArrayList of objects you want to display);

<afutputText value="#{item.realName}" />

If you want values/ objects to be stored in a session to be able to retrieve between different backing beans you could do the following:

public static void storeOnSession(String key, Object object) {
FacesContext ctx = FacesContext.getCurrentInstance();
Map sessionState = ctx.getExternalContext().getSessionMap();
sessionState.put(key, object);
}

public static Object getFromSession(String key) {
FacesContext ctx = FacesContext.getCurrentInstance();
Map sessionState = ctx.getExternalContext().getSessionMap();
return sessionState.get(key);
}

As for database persistance we are using hibernate and I have no knowledge of Spring. I am sorry if my answers are not very helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Population in JSF
 
Similar Threads
commandLink param problem
Navigation Problem - URL not updated
know about request scope in JSF(how to pass parameter from one page to other)
My navigation problem
JSF with Facelets - Using "h:commandLink" inside header of the layout