Difference between revisions of "JsfGetRequests"
Jump to navigation
Jump to search
Line 15: | Line 15: | ||
=== Modify source bean action method to evaluate parameters and redirect to target page === | === Modify source bean action method to evaluate parameters and redirect to target page === | ||
// action method invoked by action link | |||
public String doSomething() | public String doSomething() | ||
{ | { | ||
// some parameters | |||
String foo = this.fooInput.getSubmittedValue(); | String foo = this.fooInput.getSubmittedValue(); | ||
boolean bar = "true".equals(this.barInput.getSubmittedValue()); | boolean bar = "true".equals(this.barInput.getSubmittedValue()); | ||
Line 57: | Line 59: | ||
//... | //... | ||
// get http request | |||
HttpServletRequest request = (HttpServletRequest)FacesContext | HttpServletRequest request = (HttpServletRequest)FacesContext | ||
.getCurrentInstance().getExternalContext().getRequest(); | .getCurrentInstance().getExternalContext().getRequest(); | ||
// get parameters | |||
String foo = request.getParameter(targetPage.URL_PARAM_FOO); | String foo = request.getParameter(targetPage.URL_PARAM_FOO); | ||
boolean bar = ("true".equals(request.getParameter(targetPage.URL_PARAM_BAR))); | boolean bar = ("true".equals(request.getParameter(targetPage.URL_PARAM_BAR))); | ||
// fill backing bean | |||
mySessionBean.setFoo(foo); | mySessionBean.setFoo(foo); | ||
mySessionBean.setBar(bar); | mySessionBean.setBar(bar); | ||
Line 68: | Line 73: | ||
//... | //... | ||
// call action method | |||
this.processForm(); | this.processForm(); | ||
} | } |
Revision as of 13:41, 24 August 2007
The following steps have to be taken to make a JSF page basically RESTful, that is
- calling the page via a HTTP GET request is possible
- modifying the parameters in the URL is possible
- bookmarking this page is possible
What this procedure does not change is the behaviour of the JSF links. This means that a "Open link in a new window" is still not possible.
Usually, no changes in the JSPs/JSPFs are needed. Assuming you have the following
- a source page with a form
- a java bean for the source page containing an action method
- a target page
- a java bean for the target page containing an action method
- a session- or request-scoped backing bean holding the parameters
Modify source bean action method to evaluate parameters and redirect to target page[edit]
// action method invoked by action link public String doSomething() { // some parameters String foo = this.fooInput.getSubmittedValue(); boolean bar = "true".equals(this.barInput.getSubmittedValue()); // change from // return this.getTargetBean().doMore(); // to FacesContext .getCurrentInstance() .getExternalContext() .redirect("myTargetPage.jsp?foo=" + urlEscape(foo) + "&bar=" + bar); return ""; }
Define parameters in target page bean[edit]
This can look something like this:
public class MyPage extends AbstractPageBean { //... public final static String URL_PARAM_FOO = "foo"; public final static String URL_PARAM_BAR = "bar"; //... }
Catch Parameters in target page bean init method , add to session/request bean and execute action method[edit]
Like this:
public void init() { // Perform initializations inherited from our superclass super.init(); //... // get http request HttpServletRequest request = (HttpServletRequest)FacesContext .getCurrentInstance().getExternalContext().getRequest(); // get parameters String foo = request.getParameter(targetPage.URL_PARAM_FOO); boolean bar = ("true".equals(request.getParameter(targetPage.URL_PARAM_BAR))); // fill backing bean mySessionBean.setFoo(foo); mySessionBean.setBar(bar); //... // call action method this.processForm(); }