wood burning stoves 2.0*
The moose likes JSF and the fly likes JSF and REST urls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF and REST urls" Watch "JSF and REST urls" New topic
Author

JSF and REST urls

Jean-Philippe Toupin
Greenhorn

Joined: Apr 22, 2006
Posts: 6
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:

http://myserver.com/myapp/clients/view.jsf?id=46

or better:

http://myserver.com/myapp/clients/view/46

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.

This is useful for pasting urls in emails.

Thanks for your help.
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
You can check the solution on my post here
http://www.coderanch.com/t/211444/JSF/java/method-JSF
[ April 23, 2006: Message edited by: Gerardo Tasistro ]
Jean-Philippe Toupin
Greenhorn

Joined: Apr 22, 2006
Posts: 6
Thanks for your reply.

I found this solution which looks cleaner:



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 ?
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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?
Jean-Philippe Toupin
Greenhorn

Joined: Apr 22, 2006
Posts: 6
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:

/clients/view.jsf?id=46

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 ?
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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.
Jean-Philippe Toupin
Greenhorn

Joined: Apr 22, 2006
Posts: 6
Gerardo,
thanka for your very helpful reply.

I will try your method and see how things work.

Thanks again.
[ April 25, 2006: Message edited by: Jean-Philippe Toupin ]
Lincoln Baxter
Greenhorn

Joined: Aug 25, 2008
Posts: 24
This post is a bit old, but try this JSF extension. It was released recently:

http://ocpsoft.com/prettyfaces


Lincoln Baxter, III | [url]http://ocpsoft.com/[/url] | Creator of: PrettyFaces - Bookmarks, Navigation, and Dynamic Parameters for JSF
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF and REST urls
 
Similar Threads
Error 400: Invalid path /main was requested
alternative to query string
.htaccess
question
poulate a form from the reset (good practice)?