|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.ActionResult
public class ActionResult
Provides an ActionResult that is returned by Page Actions and AjaxBehaviors. ActionResults are often used to return a partial response to the browser instead of the full page content.
An ActionResult can consist of a String (HTML, JSON, XML, plain text) or a byte array (jpg, gif, png, pdf or excel documents). The ActionResultcontentType
must be set appropriately in order for the browser to recognize the action result.
ActionResults are returned by Ajax Behaviors
and Page Action methods.
Ajax Behavior
to a control. The AjaxBehavior onAction
method will handle the request and return a ActionResult instance that contains
the response, thus bypassing the rendering of the Page template. For example:
private ActionLink link = new ActionLink("link"); public void onInit() { addControl(link); link.addBehavior(new AjaxBehavior() { // The onAction method must return a ActionResult public ActionResult onAction(Control source) { // Create a new action result containing an HTML snippet and HTML content type ActionResult actionResult = new ActionResult("<span>Hello World</span>", ActionResult.HTML); return actionResult; } }); }
private ActionLink link = new ActionLink("link"); public void onInit() { link.addControl(link); // A "pageAction" is set as a parameter on the link. The "pageAction" // value is set to the Page method: "renderHelloWorld" link.setParameter(PAGE_ACTION, "renderHelloWorld"); } /** * This is a "pageAction" method that will render an HTML response. * * Note the signature of the pageAction: a public, no-argument method * returning a ActionResult instance. */ public ActionResult renderHelloWorld() { ActionResult actionResult = new ActionResult("<span>Hello World</span>", ActionResult.HTML); return actionResult; }
contentType
of the ActionResult must be set to the appropriate type
in order for the client to recognize the response. ActionResult provides constants
for some of the common content types, including: text/xml
,
text/html
, application/json
, text/plain
.
For example:
ActionResult actionResult = new ActionResult("alert('hello world');", ActionResult.JAVASCRIPT); ... // content type can also be set through the setContentType method actionResult.setContentType(ActionResult.JAVASCRIPT); ...More content types can be retrieved through
ClickUtils.getMimeType(java.lang.String)
:
// lookup content type for PNG String contentType = ClickUtils.getMimeType("png"); actionResult.setContentType(contentType);
Field Summary | |
---|---|
static String |
HTML
The html content type constant: text/html. |
static String |
JAVASCRIPT
The javascript content type constant: text/javascript. |
static String |
JSON
The json content type constant: text/json. |
static String |
TEXT
The plain text content type constant: text/plain. |
static String |
XHTML
The The xhtml content type constant: application/xhtml+xml. |
static String |
XML
The xml content type constant: text/xml. |
Constructor Summary | |
---|---|
ActionResult()
Construct a new empty ActionResult. |
|
ActionResult(byte[] bytes,
String contentType)
Construct the ActionResult for the given byte array and content type. |
|
ActionResult(InputStream inputStream,
String contentType)
Construct the ActionResult for the given inputStream and content type. |
|
ActionResult(Reader reader,
String contentType)
Construct the ActionResult for the given reader and content type. |
|
ActionResult(String content)
Construct the ActionResult for the given content. |
|
ActionResult(String template,
Map<String,Object> model,
String contentType)
Construct the ActionResult for the given template and model. |
|
ActionResult(String content,
String contentType)
Construct the ActionResult for the given String content and content type. |
Method Summary | |
---|---|
byte[] |
getBytes()
Return the byte array to stream back to the client. |
String |
getCharacterEncoding()
Return the action result character encoding. |
String |
getContent()
Return the content String to stream back to the client. |
String |
getContentType()
Return the action result content type, default is "text/plain". |
InputStream |
getInputStream()
Return the inputStream to stream back to the client. |
Map<String,Object> |
getModel()
Return the data model for the ActionResult template . |
Reader |
getReader()
Return the reader which characters are streamed back to the client. |
String |
getTemplate()
Return the template to render for this ActionResult. |
boolean |
isCacheActionRestul()
Return true if the action result should be cached by the client browser, defaults to false. |
void |
render(Context context)
Render the ActionResult to the client. |
protected void |
renderActionResult(Context context)
Render the ActionResult to the client. |
void |
setBytes(byte[] bytes,
String contentType)
Set the byte array to stream back to the client. |
void |
setCacheActionResult(boolean cacheActionResult)
Set whether the action result should be cached by the client browser or not. |
void |
setCharacterEncoding(String characterEncoding)
Set the action result character encoding. |
void |
setContent(String content)
Set the content String to stream back to the client. |
void |
setContentType(String contentType)
Set the action result response content type. |
void |
setInputStream(InputStream inputStream)
Set the content to stream back to the client. |
void |
setModel(Map<String,Object> model)
Set the model of the ActionResult template to render. |
void |
setReader(Reader reader)
Set the reader which characters are streamed back to the client. |
void |
setTemplate(String template)
Set the template to render for this ActionResult. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String TEXT
public static final String HTML
public static final String XHTML
public static final String JSON
public static final String JAVASCRIPT
public static final String XML
Constructor Detail |
---|
public ActionResult(String template, Map<String,Object> model, String contentType)
public class MyPage extends Page { public void onInit() { Behavior behavior = new DefaultAjaxBehavior() { public ActionResult onAction() { Map model = new HashMap(); model.put("id", "link"); // Note: we set XML as the content type ActionResult actionResult = new ActionResult("/js/actionResult.xml", model, ActionResult.XML); return actionResult; } } } }
template
- the template to render and stream back to the clientmodel
- the template data modelcontentType
- the response content typepublic ActionResult(Reader reader, String contentType)
reader
- the reader which characters must be streamed back to the
clientcontentType
- the response content typepublic ActionResult(InputStream inputStream, String contentType)
inputStream
- the input stream to stream back to the clientcontentType
- the response content typepublic ActionResult(String content, String contentType)
content
- the String content to stream back to the clientcontentType
- the response content typepublic ActionResult(byte[] bytes, String contentType)
bytes
- the byte array to stream back to the clientcontentType
- the response content typepublic ActionResult(String content)
response content type
will default to TEXT
, unless overridden.
content
- the content to stream back to the clientpublic ActionResult()
response content type
will default to TEXT
, unless overridden.
Method Detail |
---|
public void setCacheActionResult(boolean cacheActionResult)
response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); response.setDateHeader("Expires", new Date(1L).getTime());
cacheActionResult
- indicates whether the action result should be cached
by the client browser or notpublic boolean isCacheActionRestul()
public String getCharacterEncoding()
public void setCharacterEncoding(String characterEncoding)
characterEncoding
- the action result character encodingpublic void setContentType(String contentType)
contentType
- the action result response content typepublic String getContentType()
public void setContent(String content)
content
- the content String to stream back to the clientpublic String getContent()
public void setBytes(byte[] bytes, String contentType)
bytes
- the byte array to stream back to the clientpublic byte[] getBytes()
public void setInputStream(InputStream inputStream)
inputStream
- the inputStream to stream back to the clientpublic InputStream getInputStream()
public void setReader(Reader reader)
reader
- the reader which characters are streamed back to the client.public Reader getReader()
public Map<String,Object> getModel()
template
.
public void setModel(Map<String,Object> model)
template
property is set, the template and model
will be merged and the result will be streamed back to the client.
model
- the model of the template to renderpublic String getTemplate()
public void setTemplate(String template)
template
- the template to render for this ActionResultpublic final void render(Context context)
context
- the request contextprotected void renderActionResult(Context context)
context
- the request context
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |