2.3. Request Parameter Auto Binding

Click will automatically bind any request parameter values to public Page variable with the same name. You can also use the Bindable annotation to bind private and protected Page variables. When binding these values Click will also attempt to convert them to the correct type.

The best way to understand this is to walk through an example. Our application receives a GET request:

http://localhost:8080/mycorp/customer-details.htm?customerId=7203

This request is automatically handled by our CustomerDetails page:

package com.mycorp.page;

public class CustomerDetails extends Page {

    @Bindable protected Integer customerId;
}

After the CustomerDetails page has been created the "customerId" request parameter value "7023" will be converted into an Integer and assigned to the public page variable customerId.

Another feature of Click is that any public Page variables are automatically added to the page's model before it is rendered. This will make these values available in the page template for display. In our example the public customerId variable will be added to the Page model and will be available for rendering in the page template.

Our customer-details.htm page template contains:

<html>
<body>

  Customer ID: $customerId

</body>
</html>

After processing the request our page would be rendered as:

Customer ID: 7203

2.3.1. Customizing Auto Binding

Auto binding supports the conversion of request string parameters into the Java classes: Integer, Double, Boolean, Byte, Character, Short, Long, Float, BigInteger, BigDecimal, String and the various Date classes.

By default type conversion is performed by the RequestTypeConverter class which is used by the ClickServlet method getTypeConverter().

If you need to add support for additional types, you would write your own type converter class and specify it as a ClickServlet init parameter.

For example if you wanted to automatically load a Customer object from the database when a customer id request parameter is specified, you could write your own type converter:

public class CustomTypeConverter extends RequestTypeConverter {

    private CustomerService customerService = new CustomerService();

    /**
     * @see RequestTypeConverter#convertValue(Object, Class)
     */
    protected Object convertValue(Object value, Class toType) {
        if (toType == Customer.class) {
            return customerService.getCustomerForId(value);

        } else {
            return super.convertValue(value, toType);
        }
    }
}

This type converter would handle the following request:

http://localhost:8080/mycorp/customer-details.htm?customer=7203

This request will load the customer object from the database using "7203" as the customer id value. The ClickServlet would then assign this customer object to the matching page variable:

package com.mycorp.page;

public class CustomerDetails extends Page {

    @Bindable protected Customer customer;

}

To make your custom type converter available you will need to add an init parameter to ClickServlet in web.xml. For example:

<web-app>
  ...
  <servlet>
    <servlet-name>ClickServlet</servlet-name>
    <servlet-class>org.apache.click.ClickServlet</servlet-class>
    <init-param>
       <param-name>type-converter-class</param-name>
       <param-value>com.mycorp.util.CustomTypeConverter</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>ClickServlet</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  ...
</web-app>