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}" ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.