|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.click.Page
Provides the Page request event handler class.
The Page class plays a central role in Click applications defining how the application's pages are processed and rendered. All application pages must extend the base Page class, and provide a no arguments constructor.format
property is set
headers
property is set
path
property is set
onSecurityCheck()
method called to check whether the page should
be processed. This method should return true if the Page should continue
to be processed, or false otherwise.
onInit()
method called to complete the initialization of the page
after all the dependencies have been set. This is where you should put
any "dynamic" page initialization code which depends upon the request or any
other dependencies.
Form and field controls must be fully initialized by the time this method
has completed.
controls
calling their Control.onProcess()
method. If any of these
controls return false, continued control and page processing will be aborted.
onGet()
method called for any additional GET related processing.
Form and field controls should NOT be created or initialized at this
point as the control processing stage has already been completed.
onRender()
method called for any pre-render processing. This
method is often use to perform database queries to load information for
rendering tables.
Form and field controls should NOT be created or initialized at this
point as the control processing stage has already been completed.
model
with the
Velocity template defined by the getTemplate()
property.
onDestroy()
method called to clean up any resources. This method
is guaranteed to be called, even if an exception occurs. You can use
this method to close resources like database connections or Hibernate
sessions.
onPost()
method is called instead of onGet()
. The POST
request page execution sequence is illustrated below:
A good way to see the page event execution order is to view the log when
the application mode is set to trace:
[Click] [debug] GET http://localhost:8080/quickstart/home.htm [Click] [trace] invoked: HomePage.<<init>> [Click] [trace] invoked: HomePage.onSecurityCheck() : true [Click] [trace] invoked: HomePage.onInit() [Click] [trace] invoked: HomePage.onGet() [Click] [trace] invoked: HomePage.onRender() [Click] [info ] renderTemplate: /home.htm - 6 ms [Click] [trace] invoked: HomePage.onDestroy() [Click] [info ] handleRequest: /home.htm - 24 ms
getTemplate()
to find the Velocity template.model
to populate the Velocity Contextformat
to add to the Velocity ContextgetContentType()
to set as the HttpServletResponse content typeheaders
to set as the HttpServletResponse headers
Field Summary | |
protected List |
controls
The list of page controls. |
protected Format |
format
The Velocity template formatter object. |
protected String |
forward
The forward path. |
protected Map |
headers
The HTTP response headers. |
protected boolean |
headersEdited
The headers have been edited flag, to support copy on write. |
protected MessagesMap |
messages
The map of localized page resource messages. |
protected Map |
model
The page model. |
static String |
PAGE_MESSAGES
The global page messages bundle name: click-page. |
protected PageImports |
pageImports
The Page header imports. |
protected String |
path
The path of the page template to render. |
protected String |
redirect
The redirect path. |
protected boolean |
stateful
The page is stateful and should be save to the users HttpSession between requests. |
protected String |
template
The path of the page border template to render. |
Constructor Summary | |
Page()
|
Method Summary | |
void |
addControl(Control control)
Add the control to the page. |
void |
addModel(String name,
Object value)
Add the named object value to the Pages model map. |
String |
getContentType()
Return the HTTP response content type. |
Context |
getContext()
Return the request context of the page. |
List |
getControls()
Return the list of page Controls. |
Format |
getFormat()
Return the Velocity template formatter object. |
String |
getForward()
Return the path to forward the request to. |
Map |
getHeaders()
Return the map of HTTP header to be set in the HttpServletResponse. |
String |
getHtmlImports()
Return the HTML import string to include in the page, by default this method returns null. |
String |
getMessage(String key)
Return the localized Page resource message for the given resource property key. |
String |
getMessage(String name,
Object arg)
Return the formatted page message for the given resource name and message format argument and for the context request locale. |
String |
getMessage(String name,
Object[] args)
Return the formatted page message for the given resource name and message format arguments and for the context request locale. |
Map |
getMessages()
Return a Map of localized messages for the Page. |
Map |
getModel()
Return the Page's model map. |
PageImports |
getPageImports()
Return the Page header imports. |
String |
getPath()
Return the path of the Template or JSP to render. |
String |
getRedirect()
Return the path to redirect the request to. |
String |
getTemplate()
Return the path of the page border template to render, by default this method returns getPath() . |
boolean |
hasControls()
Return true if the page has any controls defined. |
boolean |
isStateful()
Return true if the page is stateful and should be saved in the users HttpSession between requests. |
void |
onDestroy()
The on Destroy request event handler. |
void |
onGet()
The on Get request event handler. |
void |
onInit()
The on Initialization event handler. |
void |
onPost()
The on Post request event handler. |
void |
onRender()
The on render event handler. |
boolean |
onSecurityCheck()
The on Security Check event handler. |
void |
removeControl(Control control)
Remove the control from the page. |
void |
setFormat(Format value)
Set the Velocity template formatter object. |
void |
setForward(Class pageClass)
Set the request to forward to the given page class. |
void |
setForward(Page page)
The Page instance to forward the request to. |
void |
setForward(String value)
Set the path to forward the request to. |
void |
setHeader(String name,
Object value)
Set the named header with the given value. |
void |
setHeaders(Map value)
Set the map of HTTP header to be set in the HttpServletResponse. |
void |
setPageImports(PageImports pageImports)
Set the Page header imports. |
void |
setPath(String value)
Set the path of the Template or JSP to render. |
void |
setRedirect(Class pageClass)
Set the request to redirect to the give page class. |
void |
setRedirect(String location)
Set the location to redirect the request to. |
void |
setStateful(boolean stateful)
Set whether the page is stateful and should be saved in the users HttpSession between requests. |
void |
setTemplate(String template)
Set the page border template path. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final String PAGE_MESSAGES
protected List controls
protected Format format
protected String forward
protected Map headers
protected boolean headersEdited
protected transient MessagesMap messages
protected Map model
protected transient PageImports pageImports
protected String path
protected String redirect
protected boolean stateful
protected String template
Constructor Detail |
public Page()
Method Detail |
public boolean onSecurityCheck()
public void onInit()
onInit()
method has been called.
Subclasses should place any initialization code which has dependencies
on the context or other properties in this method. Generally light
weight initialization code should be placed in the Pages constructor.
Time consuming operations such as fetching the results of a database
query should not be placed in this method. These operations should be
performed in the onRender()
, onGet()
or
onPost()
methods so that other event handlers may take
alternative execution paths without performing these expensive operations.
Please Note however the qualifier for the previous statement is
that all form and field controls must be fully initialized before they
are processed, which is after the onInit() method has
completed. After this point their onProcess() methods will be
invoked by the ClickServlet.
Select controls in particular must have their option list values populated
before the form is processed otherwise field validations cannot be performed.
For initializing page controls the best practice is to place all the
control creation code in the pages constructor, and only place any
initialization code in the onInit() method which has an external
dependency to the context or some other object. By following this practice
it is easy to see what code is "design time" initialization code and what
is "runtime initialization code".
When subclassing pages which also use the onInit() method is
is critical you call the super.onInit() method first, for
example:
public void onInit() { super.onInit(); // Initialization code .. }
public void onGet()
onSecurityCheck()
has been
called and all the Page controls
have been processed. If either
the security check or one of the controls cancels continued event
processing the onGet() method will not be invoked.
public void onPost()
onSecurityCheck()
has been
called and all the Page controls
have been processed. If either
the security check or one of the controls cancels continued event
processing the onPost() method will not be invoked.
public void onRender()
public void onDestroy()
public void addControl(Control control)
control
- the control to add
IllegalArgumentException
- if the control is null, or if the name
of the control is not definedpublic void removeControl(Control control)
control
- the control to remove
IllegalArgumentException
- if the control is null, or if the name
of the control is not definedpublic List getControls()
public boolean hasControls()
public Context getContext()
public String getContentType()
ServletRequest.getCharacterEncoding()
then this method will return "text/html; charset=encoding".
The ClickServlet uses the pages content type for setting the
HttpServletResponse content type.
public Format getFormat()
public void setFormat(Format value)
value
- the Velocity template formatter object.public String getForward()
forward
property is not null it will be used to forward
the request to in preference to rendering the template defined by the
path
property. The request is forwarded using the
RequestDispatcher.
See also getPath()
, getRedirect()
public void setForward(String value)
forward
property is not null it will be used to forward
the request to in preference to rendering the template defined by the
path
property. The request is forwarded using the Servlet
RequestDispatcher.
If forward paths start with a "/"
character the forward path is
relative to web applications root context, otherwise the path is
relative to the requests current location.
For example given a web application deployed to context mycorp
with the pages:
/index.htm /customer/search.htm /customer/details.htm /customer/management/add-customer.htmTo forward to the customer search.htm page from the web app root you could set forward as setFoward( ) or setFoward( ). If a user was currently viewing the add-customer.htm to forward to customer details.htm you could set forward as setFoward( ) or setFoward( ). See also
setPath(String)
, setRedirect(String)
value
- the path to forward the request topublic void setForward(Page page)
path
defined, as the path
specifies
the location to forward to.
page
- the Page object to forward the request to.setForward(java.lang.String)
public void setForward(Class pageClass)
pageClass
- the class of the Page to forward the request to
IllegalArgumentException
- if the Page Class is not configured
with a unique pathsetForward(java.lang.String)
public Map getHeaders()
setHeader(String, Object)
as
headers Map is initially unmodifiable.
public void setHeader(String name, Object value)
name
- the name of the headervalue
- the value of the headerpublic void setHeaders(Map value)
value
- the map of HTTP header to be set in the HttpServletResponsepublic String getHtmlImports()
public MyPage extends Page { // Define a constant for the Page Javascript import. protected static final String JAVASCRIPT_IMPORT = "<script type='text/javascript' src='{0}/click/my-page.js'></script>\n"; // Define a constant for the Page CSS import protected static final String CSS_IMPORT = "<link type='text/css' rel='stylesheet' href='text/css' src='{0}/click/my-page.css'/>\n"; ... // Override getHtmlImports and return the Javascript and CSS imports. public String getHtmlImports() { Context context = getContext(); // Concatenate Javascript and CSS imports return ClickUtils.createHtmlImport(MyPage.JAVASCRIPT_IMPORT, ClickUtils.getResourceVersionIndicator(), context) + ClickUtils.createHtmlImport(MyPage.CSS_IMPORT, ClickUtils.getResourceVersionIndicator(), context); } }Note multiple import lines should be separated by a '\n' char, as the
PageImports
will
parse multiple import lines on the '\n' char and ensure that
imports are not included twice.
The order in which JS and CSS files are include will be preserved in the
page.
If you need to customize the HTML imports included in your page override
the method getPageImports()
.
public String getMessage(String key)
package com.mycorp.pages; public class Login extends Page { ..The page class property filenames and their path:
/com/mycorp/pages/Login.properties /com/mycorp/pages/Login_en.properties /com/mycorp/pages/Login_fr.propertiesPage messages can also be defined in the optional global messages bundle:
/click-page.propertiesTo define global page messages simply add click-page.properties file to your application's class path. Message defined in this properties file will be available to all of your application pages. Note messages in your page class properties file will override any messages in the global click-page.properties file. Page messages can be accessed directly in the page template using the $messages reference. For examples:
$messages.title
Please see the MessagesMap
adaptor for more details.
key
- the message property key name
public String getMessage(String name, Object arg)
name
- resource name of the messagearg
- the message argument to format
public String getMessage(String name, Object[] args)
name
- resource name of the messageargs
- the message arguments to format
public Map getMessages()
IllegalStateException
- if the context for the Page has not be setgetMessage(String)
public void addModel(String name, Object value)
name
- the key name of the object to addvalue
- the object to add
IllegalArgumentException
- if the name or value parameters are
null, or if there is already a named value in the modelpublic Map getModel()
public PageImports getPageImports()
ClickServlet.createTemplateModel(Page)
- for template pagesClickServlet.setRequestAttributes(Page)
- for JSP pagesClickServlet.createPageImports(net.sf.click.Page)
public void setPageImports(PageImports pageImports)
ClickServlet.createTemplateModel(Page)
- for template pagesClickServlet.setRequestAttributes(Page)
- for JSP pagesgetPageImports()
method and modify the PageImports object
returned.
If you need to create a custom PageImports, override the method
ClickServlet.createPageImports(net.sf.click.Page)
pageImports
- the new pageImports instance to setpublic String getPath()
getForward()
, getRedirect()
public void setPath(String value)
public void onGet() { setPath("/some-other-template.htm"); }And here is an example if you want to change the path to a different JSP.
public void onGet() { setPath("/some-other-jsp.jsp"); }If path is set to null, Click will not perform any rendering. This is useful when you want to stream or write directly to the HttpServletResponse. See also
setForward(String)
, setRedirect(String)
value
- the path of the Template or JSP to renderpublic String getRedirect()
redirect
property is not null it will be used to redirect
the request in preference to forward
or path
properties.
The request is redirected to using the HttpServletResponse.setRedirect()
method.
See also getForward()
, getPath()
public boolean isStateful()
public void setStateful(boolean stateful)
stateful
- the flag indicating whether the page should be saved
between user requestspublic void setRedirect(String location)
redirect
property is not null it will be used to redirect
the request in preference to forward
or path
properties.
The request is redirected to using the HttpServletResponse.setRedirect()
method.
If the redirect location begins with a "/"
character the redirect location will be prefixed with the web applications
context path.
For example if an application is deployed to the context
"mycorp" calling
setRedirect( )
will redirect the request to:
"/mycorp/customer/details.htm"
If the redirect location does not begin with a "/"
character the redirect location will be used as specified. Thus if the
location is http://somehost.com/myapp/customer.jsp,
Click will redirect to that location.
JSP note: when redirecting to a JSP template keep in mind that the
JSP template won't be processed by Click, as ClickServlet is mapped to
*.htm. Instead JSP templates are processed by the Servlet
container JSP engine.
So if you have a situation where a Page Class
( ) is mapped to the JSP
( ) and you want to redirect to
Customer.class, you could either redirect to
( ) or
use the alternative redirect utility setRedirect(java.lang.Class)
.
Please note that Click will url encode the location by invoking
response.encodeRedirectURL(location) before redirecting.
See also setForward(String)
, setPath(String)
location
- the path to redirect the request topublic void setRedirect(Class pageClass)
pageClass
- the class of the Page to redirect the request to
IllegalArgumentException
- if the Page Class is not configured
with a unique pathsetRedirect(java.lang.String)
public String getTemplate()
getPath()
.
Pages can override this method to return an alternative border page
template. This is very useful when implementing an standardized look and
feel for a web site. The example below provides a BorderedPage base Page
which other site templated Pages should extend.
public class BorderedPage extends Page { public String getTemplate() { return "border.htm"; } }The BorderedPage returns the page border template "border.htm":
<html> <head> <title> $title </title> <link rel="stylesheet" type="text/css" href="style.css" title="Style"/> </head> <body> <h1> $title </h1> <hr/> #parse( $path ) </body> </html>Other pages insert their content into this template, via their
path
property using the Velocity
#parse
directive. Note the $path value is automatically
added to the VelocityContext by the ClickServlet.
getPath()
public void setTemplate(String template)
getTemplate()
will default
to getPath()
.
template
- the border template path
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |