File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes richfaces: prerender method in backing bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "richfaces: prerender method in backing bean" Watch "richfaces: prerender method in backing bean" New topic
Author

richfaces: prerender method in backing bean

Wen Ping Fong
Greenhorn

Joined: Oct 31, 2009
Posts: 4
Hi everyone,

This is my first post in JavaRanch.

Firstly, some background on my java web application development experience. I have previously developed a web application using Visual Web JSF (the IDE is NetBeans 6.5, and GlassFish V2 as the application server) - this is my only experience in java web application development so far.

I'm now beginning to venture into RichFaces. I encounter a problem in some of the backing beans of my RichFaces pages, where I need to retrieve data from database and then use these data to populate the values of the components (mostly output text fields and comboboxes) when the page is rendered for the first time, but the datasource is not available and hence, I am not able to do what I wanted to do.

This is my scenario:
In the first page user selects a command link, in the backing bean of page1 the action of the command link will assign a value to a session variable and then navigates to the second page.
In the second page, I will check if the session variable is not null in page2 backing bean, if true then I will use datasource to connect to database to retrieve data, and then use the data to populate the fields in second page. But I do not have method like prerender( ) where the datasource is already initialised and usable.

Previously in Visual Web JSF Page I can do this in the backing bean's prerender() method, but using RichFaces I don't know where this prerender() method (or something similar) going to come from.

I spent many days trying to search for information in the internet, found Apache Shale framework but it has recently retired, so I decided not to use it first unless I have no other choice.

Other information I found, such as on the JBoss Seam and MyFaces Orchestra, are just too complicated and confusing for my level of experience. I only wanted something that can do init() and prerender() in my backing bean.

Appreciate if anyone can advise me how it can be done (including maybe ask me to forget about Visual Web JSF way of development but please show me the "right" way how a web application employing RichFaces components should be developed), or direct me to a relevant tutorial website.

Thanks in advance.

Akaine Harga
Ranch Hand

Joined: Nov 03, 2009
Posts: 71

Don't understand the problem.

Define a session backing bean for the second page. In the command method get this bean from the FacesContext and fill it with data. Then just render the page having your elements' values set to the same bean. Don't forget to declare keepAlive element with your backing bean as parameter for richfaces ajax operations.


Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
willCodeForFood("Java,PHP,C#,XML,VBS,XHTML,CSS,JavaScript,SQL"); //always looking for job opportunities in AU/NZ/US/CA/Europe :P
Wen Ping Fong
Greenhorn

Joined: Oct 31, 2009
Posts: 4
Hi Akaine,

Thanks for your reply, and sorry for the late, I was away.

I will try the your suggestion. Will post the result or maybe ask further question after I try. I haven't used FacesContext to get a bean, nor use the keepAlive element before, so might post my result a bit later because I need to learn those things first.

Thanks again.


Wen Ping Fong
Greenhorn

Joined: Oct 31, 2009
Posts: 4
Hi Akaine,

I think your suggestion works.

I did some tests by following the information I found in the following post on JSF - Managed Bean to Managed Bean Communication:
http://www.coderanch.com/t/214436/JSF/java/JSF-Managed-Bean-Managed-Bean#1021122

and was eventually led to BalusC's blog on Accessing Another Managed Bean:
http://balusc.blogspot.com/2006/06/communication-in-jsf.html#AccessingAnotherManagedBean

In my small testing web project, I managed to retrieve the data from the database in one of the backing beans, and then use another bean to set the field values.

However, in the JSP I had to change the outputText from binding

to

Otherwise, the outputText component will not be shown. I'm not sure why, maybe it is due to my setup.

Anyway, at least it works in my testing! I will use this in my code to see if it works.

Thanks for your help!

Akaine Harga
Ranch Hand

Joined: Nov 03, 2009
Posts: 71

That's correct. In this approach you should use values directly. Binding is supposed to be used for element structure alteration.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: richfaces: prerender method in backing bean
 
Similar Threads
Session backing beans / STATE_SAVING_METHOD / WAS7
JSF Popup from JSP page
MVC Model and Domain Model
Migration from woodstock to richfaces
Constructor of a Backing Bean of JSF page Invoking Twice on Initial Load