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