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

A Renderer for Encoding and Decoding

To customize the way the value for an input component is entered, we must develop a custom renderer that implements the decoding behavior in addition to the encoding behavior. An example is a custom renderer for the UIInput component that lets the user pick a date by selecting the year, month, and day from three selection lists, as shown in Figure 13-2.

Date selection lists rendered by a custom renderer for an input component
Figure 13-2. Date selection lists rendered by a custom renderer for an input component

Because the date value is rendered as three selection lists, it’s sent to the server as three separate parameters. The custom renderer reads all three and creates a single java.util.Date value that the input component can handle.

Before we dig into the code, let’s see how the custom renderer can be used. Example 13-2 shows the JSP page using the custom action element that ties together the custom renderer and the input component.

Example 13-2. Using the date picker (custom/datePicker.jsp)
<%@ page contentType="text/html" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://mycompany.com/jsftaglib" prefix="my" %>

<jsp:useBean id="now" scope="request" class="java.util.Date" />
<f:view>
  <html>
    <body>
      <h:form>
        <my:inputDatePicker value="#{myDate}"
          startYear="#{now.year + 1900 - 2}" years="10" /> <h:outputText value="#{myDate}" ...

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