|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.Context
public class Context
Provides the HTTP request context information for pages and controls.
A new Context object is created for each Page request. The request Context
object can be obtained from the thread local variable via the
getThreadLocalContext()
method.
Field Summary | |
---|---|
protected ServletConfig |
config
The servlet config. |
protected ServletContext |
context
The servlet context. |
protected boolean |
isPost
The HTTP method is POST flag. |
static String |
LOCALE
The user's session Locale key: locale. |
protected HttpServletResponse |
response
The servlet response. |
Constructor Summary | |
---|---|
Context(ServletContext context,
ServletConfig config,
HttpServletRequest request,
HttpServletResponse response,
boolean isPost,
ClickServlet clickServlet)
Create a new request context. |
Method Summary | ||
---|---|---|
Map<String,String> |
createMessagesMap(Class<?> baseClass,
String globalResource)
Return a new messages map for the given baseClass (a page or control) and the given global resource bundle name. |
|
|
createPage(Class<T> pageClass)
Return a new Page instance for the given class. |
|
|
createPage(String path)
Return a new Page instance for the given path. |
|
String |
getApplicationMode()
Return the Click application mode value: ["production", "profile", "development", "debug", "trace"]. |
|
String |
getCharset()
Return the Click application charset or ISO-8859-1 if not is defined. |
|
Cookie |
getCookie(String name)
Return the cookie for the given name or null if not found. |
|
String |
getCookieValue(String name)
Return the cookie value for the given name or null if not found. |
|
FileItem |
getFileItem(String name)
Returns the value of a request parameter as a FileItem, for "multipart" POST requests (file uploads), or null if the parameter is not found. |
|
Map<String,FileItem[]> |
getFileItemMap()
Returns a map of FileItem arrays keyed on request parameter name for "multipart" POST requests (file uploads). |
|
Locale |
getLocale()
Return the users Locale. |
|
Class<? extends Page> |
getPageClass(String path)
Return the page Class for the given path. |
|
String |
getPagePath(Class<? extends Page> pageClass)
Return the path for the given page Class. |
|
HttpServletRequest |
getRequest()
Returns the servlet request. |
|
Object |
getRequestAttribute(String name)
Return the named request attribute, or null if not defined. |
|
String |
getRequestParameter(String name)
Return the named request parameter. |
|
String[] |
getRequestParameterValues(String name)
Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist. |
|
String |
getResourcePath()
Return the page resource path from the request. |
|
HttpServletResponse |
getResponse()
Returns the servlet response. |
|
ServletConfig |
getServletConfig()
Returns the servlet config. |
|
ServletContext |
getServletContext()
Returns the servlet context. |
|
HttpSession |
getSession()
Return the user's HttpSession, creating one if necessary. |
|
Object |
getSessionAttribute(String name)
Return the named session attribute, or null if not defined. |
|
static Context |
getThreadLocalContext()
Return the thread local request context instance. |
|
boolean |
hasRequestAttribute(String name)
Return true if the request contains the named attribute. |
|
boolean |
hasRequestParameter(String name)
Return true if the request contains the named parameter. |
|
boolean |
hasSession()
Return true if a HttpSession exists, or false otherwise. |
|
boolean |
hasSessionAttribute(String name)
Return true if there is a session and it contains the named attribute. |
|
static boolean |
hasThreadLocalContext()
Returns true if a Context instance is available on the current thread, false otherwise. |
|
void |
invalidateCookie(String name)
Invalidate the specified cookie and delete it from the response object. |
|
boolean |
isAjaxRequest()
Return true is this is an Ajax request, false otherwise. |
|
boolean |
isForward()
Return true if the request has been forwarded. |
|
boolean |
isGet()
Return true if the HTTP request method is "GET". |
|
boolean |
isMultipartRequest()
Return true if the request is a multi-part content type POST request. |
|
boolean |
isPost()
Return true if the HTTP request method is "POST". |
|
void |
removeSessionAttribute(String name)
Remove the named attribute from the session. |
|
String |
renderTemplate(Class templateClass,
Map<String,?> model)
Return a rendered Velocity template and model for the given class and model data. |
|
String |
renderTemplate(String templatePath,
Map<String,?> model)
Return a rendered Velocity template and model data. |
|
Cookie |
setCookie(String name,
String value,
int maxAge)
Sets the given cookie value in the servlet response with the path "/". |
|
void |
setFlashAttribute(String name,
Object value)
This method will set the named object as a flash HttpSession object. |
|
void |
setLocale(Locale locale)
This method stores the given Locale in the users session. |
|
void |
setRequestAttribute(String name,
Object value)
This method will set the named object in the HTTP request. |
|
void |
setSessionAttribute(String name,
Object value)
This method will set the named object in the HttpSession. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String LOCALE
protected final ServletContext context
protected final ServletConfig config
protected final boolean isPost
protected final HttpServletResponse response
Constructor Detail |
---|
public Context(ServletContext context, ServletConfig config, HttpServletRequest request, HttpServletResponse response, boolean isPost, ClickServlet clickServlet)
context
- the servlet contextconfig
- the servlet configrequest
- the servlet requestresponse
- the servlet responseisPost
- the servlet request is a POSTclickServlet
- the click servlet instanceMethod Detail |
---|
public static Context getThreadLocalContext()
RuntimeException
- if a Context is not available on the thread.public static boolean hasThreadLocalContext()
getThreadLocalContext()
this method
can safely be used and will not throw an exception if Context is not
available on the current thread.
This method is very useful inside a Control
constructor which
might need access to the Context. As Controls could potentially be
instantiated during Click startup (in order to deploy their resources),
this check can be used to determine whether Context is available or not.
For example:
public MyControl extends AbstractControl { public MyControl(String name) { if (Context.hasThreadLocalContext()) { // Context is available, meaning a user initiated a web // request Context context = getContext(); String state = (String) context.getSessionAttribute(name); setValue(state); } else { // No Context is available, meaning this is most probably // the application startup and deployment phase. } } }
public HttpServletRequest getRequest()
public HttpServletResponse getResponse()
public ServletConfig getServletConfig()
public ServletContext getServletContext()
public HttpSession getSession()
public String getResourcePath()
http://www.mycorp.com/banking/secure/login.htm -> /secure/login.htm
public boolean isForward()
ClickServlet.CLICK_FORWARD
request attribute.
public boolean isPost()
public boolean isGet()
public boolean isAjaxRequest()
public boolean hasRequestAttribute(String name)
name
- the name of the request attribute
public Object getRequestAttribute(String name)
name
- the name of the request attribute
public void setRequestAttribute(String name, Object value)
name
- the storage name for the object in the requestvalue
- the object to store in the requestpublic boolean hasRequestParameter(String name)
name
- the name of the request parameter
public String getRequestParameter(String name)
name
- the name of the request parameter
Form.onProcess()
,
isMultipartRequest()
,
getFileItemMap()
public String[] getRequestParameterValues(String name)
name
- a String containing the name of the parameter whose
value is requested
public Object getSessionAttribute(String name)
FlashAttribute
which when accessed are then
removed from the session.
name
- the name of the session attribute
public void setSessionAttribute(String name, Object value)
name
- the storage name for the object in the sessionvalue
- the object to store in the sessionpublic void removeSessionAttribute(String name)
name
- of the attribute to remove from the sessionpublic boolean hasSessionAttribute(String name)
name
- the name of the attribute
public boolean hasSession()
public void setFlashAttribute(String name, Object value)
name
- the storage name for the object in the sessionvalue
- the object to store in the sessionpublic Cookie getCookie(String name)
name
- the name of the cookie
public String getCookieValue(String name)
name
- the name of the cookie
public Cookie setCookie(String name, String value, int maxAge)
name
- the cookie namevalue
- the cookie valuemaxAge
- the maximum age of the cookie in seconds. A negative
value will expire the cookie at the end of the session, while 0 will delete
the cookie.
ClickUtils.setCookie(HttpServletRequest, HttpServletResponse, String, String, int, String)
public void invalidateCookie(String name)
name
- the name of the cookie you want to delete.ClickUtils.invalidateCookie(HttpServletRequest, HttpServletResponse, String)
public <T extends Page> T createPage(String path)
Page.setForward(Page)
, for example:
UserEdit userEdit = (UserEdit) getContext().createPage("/user-edit.htm");
userEdit.setUser(user);
setForward(userEdit);
The given page path must start with a '/'.
path
- the Page path as configured in the click.xml file
IllegalArgumentException
- if the Page is not foundpublic <T extends Page> T createPage(Class<T> pageClass)
Page.setForward(Page)
, for example:
UserEdit userEdit = (UserEdit) getContext().createPage(UserEdit.class);
userEdit.setUser(user);
setForward(userEdit);
pageClass
- the Page class as configured in the click.xml file
IllegalArgumentException
- if the Page is not found, or is not
configured with a unique pathpublic String getPagePath(Class<? extends Page> pageClass)
pageClass
- the class of the Page to lookup the path for
IllegalArgumentException
- if the Page Class is not configured
with a unique pathpublic Class<? extends Page> getPageClass(String path)
path
- the page path
IllegalArgumentException
- if the Page Class for the path is not
foundpublic String getApplicationMode()
public String getCharset()
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <click-app charset="UTF-8"> .. </click-app>
public Map<String,String> createMessagesMap(Class<?> baseClass, String globalResource)
baseClass
- the target classglobalResource
- the global resource bundle name
public Map<String,FileItem[]> getFileItemMap()
public FileItem getFileItem(String name)
name
- the name of the parameter of the fileItem to retrieve
public Locale getLocale()
setLocale(Locale)
method.
Pages and Controls obtain the users Locale using this method.
public void setLocale(Locale locale)
LOCALE
key.
locale
- the Locale to store in the users session using the key
"locale"public boolean isMultipartRequest()
public String renderTemplate(Class templateClass, Map<String,?> model)
// Full class name com.mycorp.control.CustomTextField // Template path name /com/mycorp/control/CustomTextField.htmExample method usage:
public String toString() { Map model = getModel(); return getContext().renderTemplate(getClass(), model); }
templateClass
- the class to resolve the template formodel
- the model data to merge with the template
RuntimeException
- if an error occurspublic String renderTemplate(String templatePath, Map<String,?> model)
public String toString() { Map model = getModel(); return getContext().renderTemplate("/custom-table.htm", model); }
templatePath
- the path of the Velocity template to rendermodel
- the model data to merge with the template
RuntimeException
- if an error occurs
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |