Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PostRedirectGet w/viewParams loses parameters on failed validation

843844Jul 16 2010 — edited Sep 3 2010
Apologies if this has already been answered, but I could not dig it up. I am attempting to use JSF 2 and viewParams to handle navigation with bookmarkability in a new application. For the most part I have succeeded in doing so and I am very pleased at the simplicity of the design with implicit navigation and the inclusion of view params. Since my application is primarily for display with very little user interaction aside from navigation, it has been relatively easy. However I have run into a snag with this methodology where the user is allowed to search for something in the system. Here is the jsf snippet that is related (omitting all non-pertinent bits and renaming some elements for clarity):
<h:inputText value="#{searchCommand.searchValue}"/>
<h:commandButton type="submit" value="Search" action="#{searchCommand.invoke}"/>
Here is an extract of the searchCommand backing bean action logic (modified for simplicity):
if( hasContent )
{
   return "success?faces-redirect=true&includeViewParams=true&result=" + matchId;
}
else
{
   FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( failMessage ) );
}
The problem is that if I simply add a message to the context, I see the message, but the viewParams for the page are lost. If I instead do something like this for the else block:
else
{
   FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( failMessage ) );
   return "currentPage?faces-redirect=true&includeViewParams=true";
}
then I keep my view parameters but my failure message does not appear in my messages (since it does a redirect with the view params).

I thought to use a validator instead of having the validation logic in the action method, but this loses my viewParams if the value does not pass validation, which seems problematic. I can imagine numerous ways around this (using f:ajax instead of a full form submit or adding a failure code to the viewParams) but I would like to keep this as simple as possible and stick to a single methodology (I am trying to avoid AJAX if I can to make the site usable by any browser and to avoid forcing javascript downloads, and adding the failure message to the viewParams seems hackish at best).

So two questions:
1) is there a standard way to keep faces messages visible when an error occurs in the backing bean while using PRG?
2) is it possible to use validators on form elements with an application that depends on viewParams?

Thanks,
--Zack
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 1 2010
Added on Jul 16 2010
17 comments
2,195 views