Hi, I have been looking for an active Java community for a while. Looks like I just found the right place. I'm developping my first JSF application. I understand the basics of JSF with the navigation map and everything as POST. However, sometimes you need to use GET for usability's sake.
I would like to be able to have URLs like the following in some cases:
How can I achieve this with JSF ? I tried creating a request scoped bean that gets the GET parameter in the constructor but it looks like the bean is recreated everytime the page is posted back as the constructor gets called everytime.
However, when a button is clicked and an action is triggered on the backing bean, the page is posted back to the url without the query string and I get a null pointer exception. I thought a request scoped bean would persist between postbacks ?
Joined: Feb 08, 2005
It might be passing a null value to your setter method and your logic might not be checking for it. Have you tried checking for null in the setter of ID and not update the value if ID=null?
Joined: Apr 22, 2006
Gerardo, thank you very much for your help.
It is indeed passing the NULL value to my bean and this is normal. The problem is that I access my jsf page with the following url:
Then, I have a button which triggers an action on the bean. The action method gets called on the bean and the browser is redirected to:
/clients/view.jsf (without the id query string)
Hence why the NULL value. Is there anyway to make jsf post back with the same query string the page got originally called ?
Joined: Feb 08, 2005
Jean, there are two scenarios I usually come about in these cases.
One is the session scoped bean and the other is the request scoped bean. In the case of session scoped beans I use the setProperty call to init the bean. In that sense I do a "No no" of beans which is include some sort of business logic in the setId call. Thus I use the existence of a GET variable as a means to signal and initialize the page or application. It isn't orthodox, but it works. Since the setProperty will not be called if the GET variable isn't set the application is only initialized once.
For example in your scenario I would use the setProperty to call setId only when ?id=46 exists on the URL. The setId method accesses the database and loads cliend 46 and displays it. All work will be done on client 46 since the bean is session scoped and the ID need not be set again.
The other case is the request scope. In such a case the ID will be forgotten between requests (well actually the whole bean). So I use hidden variables. I call the use Bean and setProperty early in the JSP. After that a managed bean exists of name clientBean (to put a name on it). Within the form it is valid now to do ...
Your ID value will now be propagated through succesive posts.