I have a web-application where the users can be sent directly to some specific pages (such as a page where he can view or edit an item). To achieve that, we provide a specif
Set the GET query parameters as managed properties in faces-config.xml so that you don't need to gather them manually:
forward
com.example.ForwardBean
request
action
#{param.action}
actionParam
#{param.actionParam}
This way the request forward.jsf?action=outcome1&actionParam=123 will let JSF set the action and actionParam parameters as action and actionParam properties of the ForwardBean.
Create a small view forward.xhtml (so small that it fits in default response buffer (often 2KB) so that it can be resetted by the navigationhandler, otherwise you've to increase the response buffer in the servletcontainer's configuration), which invokes a bean method on beforePhase of the f:view:
The ForwardBean can look like this:
public class ForwardBean {
private String action;
private String actionParam;
public void navigate(PhaseEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String outcome = action; // Do your thing?
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
}
// Add/generate the usual boilerplate.
}
The navigation-rule speaks for itself (note the entries which would do ExternalContext#redirect() instead of ExternalContext#dispatch() under the covers):
outcome1
/outcome1.xhtml
outcome2
/outcome2.xhtml
An alternative is to use forward.xhtml as
#{forward}
and update the navigate() method to be invoked on @PostConstruct (which will be invoked after bean's construction and all managed property setting):
@PostConstruct
public void navigate() {
// ...
}
It has the same effect, however the view side is not really self-documenting. All it basically does is printing ForwardBean#toString() (and hereby implicitly constructing the bean if not present yet).
Note for the JSF2 users, there is a cleaner way of passing parameters with and more robust way of handling the redirect/navigation by . See also among others: