Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jakarta.ee-community] Proposal - Unit JSF & JAX-RS...?

Hi,

I would like to make a suggestion how to bring JSF and JAX-RS closer together.

I am working with JSF for the early binning. And so I went through all the browser problems caused by the POSTBACK mechanism of JSF. Of course, since version 2.0 we have a solution (or workaround) with the <h:link> and with the  query param "faces-redirect=true". This works well, so this here is no criticism.

But as a result of the Post/Redirect/Get pattern I see in my own application now often a construction like this:

1) The Action Method
A Action-Method of a CDI bean returns an navigation outcome like this:

....
 public String myActionMethod() {
   ...

   String actionResult="ticket.xhtml?id=112233&faces-redirect=true";
   return actionResult;
 }
....

 
This method adds the faces-redirect for the JSF POSTBACK.


2) The JSF Page
My jsf page on the other hand has to deal with this custom query param 'id'. So for example my ticket.xhtml page has an header with something like this:

....
    <h:head>
        <!-- support deep link for ticket id -->
        <f:metadata>
            <f:viewParam name="id" value="#{myController.ticketId}" />
        </f:metadata>
    </h:head>
....



3) The Controller
And finally my CDI Controller has the corresponding setter method to load the data from the backend:

....
    public void setTicketId(String id) {
      this.id=id;
      if (id != null && !id.isEmpty()) {
        myTicket=myServiceEJB.load(id);
        // finally we destroy the param to avoid a reload on the next postback
        id = null; // !
      }
    }
....



As a result users can now bookmark links like the following:

   http://..../ticket.xhtml?id=111222333


This works fro bookmarking and also for the jsf action methods. But it is after all a little bit clumsy.....

In MVC 1.0 we see how JAX-RS can leverage the navigation of a web framework. So what I am now thinking about is the following:

Is it possible to combine JAX-RS with JSF in a similar way as it is used by MVC 1.0?
So for the scenario above, I imagine a CDI bean which looks like this:


@JSFController
@Path("ticket")
public class TicketController {
  ...
  @GET
  @Path("/{id}")
  public String loadTicket(@PathParam("id") String ticketID) {
    myTicket=myServiceEJB.load(
ticketID);
    return "ticket.xhtml";
  }
  ...
}


The imaginary annotation '@JSFController' does nothing but instruct the JSF rendering engine to load the page ticket.xhtml as an outcome.
And as a result users can load any JSF page with a pure JAX-RS resource.

    /ticket/112233

Also action methods can use the JAX-RS URL pattern as well. From my example in the beginning the action method would now simply look like this:

....
 public String myActionMethod() {
   ...
   return "/ticket/112233";
 }
....


So my questions are:

- Is it worth discussing this proposal?
- Is it possible to redirect from a JAX-RS Resource Method to the JSF rendering engine?
- Or do I overlook some of the complexity of JSF here?

I think that JSF would be much more natural to use and the POSTBACK would be completely hidden from the user. Maybe this could be a big step forward for JSF in general?

Please let me know what you think and if here is the right place to discuss it?

Best regards
Ralph



Back to the top