Chapter 2. Pages

2.1. Classes
2.2. Execution
2.3. Request Parameter Auto Binding
2.3.1. Customizing Auto Binding
2.4. Security
2.4.1. Application Authentication
2.4.2. Container Authentication
2.4.3. Container Access Control
2.4.4. Logging Out
2.5. Page Navigation
2.5.1. Forward
2.5.1.1. Forward Parameter Passing
2.5.1.2. Page Forwarding
2.5.2. Template Path
2.5.3. Redirect
2.5.3.1. Redirect Parameter Passing
2.5.3.2. Post Redirect
2.6. Page Templating
2.7. Page Actions
2.7.1. Page Action Execution
2.7.2. ActionResult
2.7.3. Page Action Example
2.7.4. Accessing Request Parameters
2.7.5. Set response headers and status code
2.8. Direct Rendering
2.9. Stateful Pages
2.9.1. Page Creation
2.9.2. Page Execution
2.9.3. Page Destruction
2.10. Error Handling
2.11. Page Not Found
2.12. Page Message Properties
2.13. Page HEAD Elements

Pages are the heart of web applications. In Apache Click, Pages encapsulate the processing of HTML requests and the rendering of HTML responses. This chapter discusses Apache Click pages in detail.

In Click, a logical page is composed of a Java class and a Velocity template, with these components being defined in page elements of the click.xml file:

<page path="search.htm" classname="com.mycorp.page.Search"/>

The path attribute specifies the location of the page Velocity template, and the classname attribute specifies the page Java class name. If you use the Freemarker template engine instead of Velocity, the setup is the same.

The template path should have an .htm extension which is specified in web.xml to route *.htm requests to the ClickServlet.

Please note if you want Click to process templates with a different extension e.g. .xml, you need to implement the method isTemplate(String path) and specify the extra extensions. The simplest way is to subclass XmlConfigService and override the default implementation as described here. Also remember to map the new extensions in web.xml.

If you use JSP pages for rendering, the .jsp extension must be used. For example:

<page path="search.jsp" classname="com.mycorp.page.Search"/>

Please note, Click does not handle JSP requests directly, instead it forwards JSP requests to the servlet container. Do not map the ClickServlet to handle *.jsp requests in web.xml. Instead .jsp templates are accessed with a .htm extension. At runtime Click will convert the page path from .jsp to .htm and back.

2.1. Classes

All custom Click pages must subclass the Page base class. The Page class and its associated companion classes, Context and Control, are depicted in the figure below.

Page Class Diagram

Figure 2.1. Page Class Diagram


The Page class provides a model attribute which is used to hold all the objects that are rendered in the page Velocity template. The model may also contain Control objects, which provide user interface controls on the Page.

Pages also provides access to the Context object which references all the javax.servlet objects associated with the request. When programming in Click you use the Context object to access HttpServletRequest attributes, parameters and the HttpSession object.