O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Processing JSF Input and Generating a Non-JSF Response

If user input must be processed before the response is rendered using some other type of resource than a JSF view, you can do so in a JSF action method or an action event listener. When the input is processed, render the response any way you like, and just tell JSF that a response has already been sent to prevent it from trying to generate a response from the view.

Let’s say that you have a JSF view containing links to external resources, such as referral links to Amazon.com. When the user clicks a link, you want to log this fact before you redirect to the book details page on Amazon.com—maybe to make sure their click-count is accurate. The link may be represented by a <h:commandLink> action element, like this:

<h:form>
  <h:commandLink action="#{logger.logJSFBookClick}">
    <h:outputText value="JavaServer Faces, Hans Bergsten (O'Reilly)" />
  </h:commandLink>
</h:form>

The <h:commandLink> action element must be nested within an <h:form> element, and it generates an HTML link element with a JavaScript event handler that submits the form when the link is clicked. What’s most important in this example is that it’s bound to an action method that may look like this:

public String logJSFBookClick( ) { // Whatever is needed to log the event ... FacesContext context = FacesContext.getCurrentInstance( ); ExternalContext ec = context.getExternalContext( ); try { ec.redirect("http://www.amazon.com/exec/obidos/ASIN/0596005393"); } catch (IOException ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required