JsfGetRequests

From MPDLMediaWiki
Revision as of 12:57, 14 September 2007 by Natasab (talk | contribs)
Jump to navigation Jump to search

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();
       
   }