basic JSF question
Hi,
This is probably obvious to anyone experienced in JSF, but how do you get JSF to call a method on a page's backing bean before displaying the page? For instance, I have a JSP showing a list, that will be called via GET request from a link on a non-JSF page. I simply want to make database calls to populate the list before the page is rendered, but don't know what hook to use to do this.
Thanks
# 1
I do not have experience with such a thing but I guess that AbstractPageBean could help you, if you extend it.It has a prerender() method. See http://developers.sun.com/docs/jscreator/apis/jsfcl/com/sun/jsfcl/app/AbstractPageBean.html.Jan
honzaa at 2007-7-12 10:19:31 >

# 2
If you don't use the Java Studio Creator components, then you just can make use of the constructor or the initialization block of the backing bean or even the getter of the value to prepopulate the values.
# 3
All of those seem like hacks somehow. I just read another thread saying not to do database access in a constructor. Is there no equivalent of what is the core flow of Struts, i.e. execute code, then render page?
# 4
> All of those seem like hacks somehow. I just read
> another thread saying not to do database access in a
> constructor. Is there no equivalent of what is the
> core flow of Struts, i.e. execute code, then render
> page?
I think that the most appropriate tool for this would be a PhaseListener.
# 5
I found an article that explains somewhat the difficulty of transitioning from Struts to JSF, aka the "paradigm mismatch": http://blog.exadel.com/?p=19
# 6
I've been struggeling with something similar for quite some time too. The most suitable solution for me was to use a datatable to populate a list using database values.
Just read this (http://balusc.xs4all.nl/srv/dev-jep-dat.html ), it will make things more clear. This, out of several solutions that were offered to me, is the only one that works perfectly.
> Hi,
>
> This is probably obvious to anyone experienced in
> JSF, but how do you get JSF to call a method on a
> page's backing bean before displaying the page? For
> instance, I have a JSP showing a list, that will be
> called via GET request from a link on a non-JSF page.
> I simply want to make database calls to populate the
> list before the page is rendered, but don't know what
> hook to use to do this.
>
> Thanks
# 7
> All of those seem like hacks somehow. I just read
> another thread saying not to do database access in a
> constructor. Is there no equivalent of what is the
> core flow of Struts, i.e. execute code, then render
> page?
Hacks? Backing beans aren't strictly DTO's or POJO's. They are the Controller part of the MVC pattern.
Database access ought to be done in the another layer.
# 8
I found this thread most helpful: http://forum.java.sun.com/thread.jspa?forumID=427&threadID=541382
Here is a quote from it that was the most helpful to me out of anything:
"IMHO you should change your approach to this problem.
JSF page unlike real JSP one is responsible only for drawing the view. All data should be prepared somewhere else. Where? It depends of your task.
f.g. how user can come to this page?
If through the navigation rule then you can prepare your bean in the action method or in event listener for commandLink which go to your page.
if user come from where else and bean is not prepared then this bean should determine its unprepared state itself and whether stay prepared or throw exception.
This is the responsibility of your Backing Bean to check its state and know what data to get."
Long and short: you have to start "thinking in JSF", not trying to force JSF to be Struts.