JsfGetRequests

From MPDLMediaWiki
Jump to: navigation, search

The following steps have to be taken to make a JSFJavaServer Faces page basically RESTful, that is

  • calling the page via a HTTPHyperText Transfer Protocol GET request is possible
  • modifying the parameters in the URLUniform Resource Locator is possible
  • bookmarking this page is possible

What this procedure does not change is the behaviour of the JSFJavaServer Faces 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

   // 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

This can look something like this:

   public class MyPage extends AbstractPageBean
   {
       
       //...
       
       public final static String URLUniform Resource Locator_PARAM_FOO = "foo";
       public final static String URLUniform Resource Locator_PARAM_BARBeratender Ausschuss für EDV-Anlagen in der MPG = "bar";
       
       //...
       
   }


Catch Parameters in target page bean init method , add to session/request bean and execute action method

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.URLUniform Resource Locator_PARAM_FOO);
       boolean bar = ("true".equals(request.getParameter(targetPage.URLUniform Resource Locator_PARAM_BARBeratender Ausschuss für EDV-Anlagen in der MPG)));
       
       // fill backing bean
       mySessionBean.setFoo(foo);
       mySessionBean.setBar(bar);
       
       //...
       
       // call action method
       this.processForm();
       
   }