org.apache.click
Class ActionResult

java.lang.Object
  extended by org.apache.click.ActionResult

public class ActionResult
extends Object

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 ActionResult contentType 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

Ajax requests are handled by adding an 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;
         }
     });
 } 

Page Action

A Page Action is a method on a Page that can be invoked directly from the browser. The Page Action method returns an ActionResult instance that is rendered to the browser, thus bypassing the rendering of the Page template.
 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;
 } 

Content Type

The 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

TEXT

public static final String TEXT
The plain text content type constant: text/plain.

See Also:
Constant Field Values

HTML

public static final String HTML
The html content type constant: text/html.

See Also:
Constant Field Values

XHTML

public static final String XHTML
The The xhtml content type constant: application/xhtml+xml.

See Also:
Constant Field Values

JSON

public static final String JSON
The json content type constant: text/json.

See Also:
Constant Field Values

JAVASCRIPT

public static final String JAVASCRIPT
The javascript content type constant: text/javascript.

See Also:
Constant Field Values

XML

public static final String XML
The xml content type constant: text/xml.

See Also:
Constant Field Values
Constructor Detail

ActionResult

public ActionResult(String template,
                    Map<String,Object> model,
                    String contentType)
Construct the ActionResult for the given template and model.

When the ActionResult is rendered the template and model will be merged and the result will be streamed back to the client.

For example:

 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;
             }
         }
     }
 } 

Parameters:
template - the template to render and stream back to the client
model - the template data model
contentType - the response content type

ActionResult

public ActionResult(Reader reader,
                    String contentType)
Construct the ActionResult for the given reader and content type.

Parameters:
reader - the reader which characters must be streamed back to the client
contentType - the response content type

ActionResult

public ActionResult(InputStream inputStream,
                    String contentType)
Construct the ActionResult for the given inputStream and content type.

Parameters:
inputStream - the input stream to stream back to the client
contentType - the response content type

ActionResult

public ActionResult(String content,
                    String contentType)
Construct the ActionResult for the given String content and content type.

Parameters:
content - the String content to stream back to the client
contentType - the response content type

ActionResult

public ActionResult(byte[] bytes,
                    String contentType)
Construct the ActionResult for the given byte array and content type.

Parameters:
bytes - the byte array to stream back to the client
contentType - the response content type

ActionResult

public ActionResult(String content)
Construct the ActionResult for the given content. The response content type will default to TEXT, unless overridden.

Parameters:
content - the content to stream back to the client

ActionResult

public ActionResult()
Construct a new empty ActionResult. The response content type will default to TEXT, unless overridden.

Method Detail

setCacheActionResult

public void setCacheActionResult(boolean cacheActionResult)
Set whether the action result should be cached by the client browser or not.

If false, Click will set the following headers to prevent browsers from caching the result:

 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()); 

Parameters:
cacheActionResult - indicates whether the action result should be cached by the client browser or not

isCacheActionRestul

public boolean isCacheActionRestul()
Return true if the action result should be cached by the client browser, defaults to false. It is highly unlikely that you would turn action result caching on.

Returns:
true if the action result should be cached by the client browser, false otherwise

getCharacterEncoding

public String getCharacterEncoding()
Return the action result character encoding. If no character encoding is specified the request character encoding will be used.

Returns:
the action result character encoding

setCharacterEncoding

public void setCharacterEncoding(String characterEncoding)
Set the action result character encoding. If no character encoding is set the request character encoding will be used.

Parameters:
characterEncoding - the action result character encoding

setContentType

public void setContentType(String contentType)
Set the action result response content type. If no content type is set it will default to "text/plain".

Parameters:
contentType - the action result response content type

getContentType

public String getContentType()
Return the action result content type, default is "text/plain".

Returns:
the response content type

setContent

public void setContent(String content)
Set the content String to stream back to the client.

Parameters:
content - the content String to stream back to the client

getContent

public String getContent()
Return the content String to stream back to the client.

Returns:
the content String to stream back to the client

setBytes

public void setBytes(byte[] bytes,
                     String contentType)
Set the byte array to stream back to the client.

Parameters:
bytes - the byte array to stream back to the client

getBytes

public byte[] getBytes()
Return the byte array to stream back to the client.

Returns:
the byte array to stream back to the client

setInputStream

public void setInputStream(InputStream inputStream)
Set the content to stream back to the client.

Parameters:
inputStream - the inputStream to stream back to the client

getInputStream

public InputStream getInputStream()
Return the inputStream to stream back to the client.

Returns:
the inputStream to stream back to the client

setReader

public void setReader(Reader reader)
Set the reader which characters are streamed back to the client.

Parameters:
reader - the reader which characters are streamed back to the client.

getReader

public Reader getReader()
Return the reader which characters are streamed back to the client.

Returns:
the reader which characters are streamed back to the client.

getModel

public Map<String,Object> getModel()
Return the data model for the ActionResult template.

Returns:
the data model for the ActionResult template

setModel

public void setModel(Map<String,Object> model)
Set the model of the ActionResult template to render.

If the template property is set, the template and model will be merged and the result will be streamed back to the client.

Parameters:
model - the model of the template to render

getTemplate

public String getTemplate()
Return the template to render for this ActionResult.

Returns:
the template to render for this ActionResult

setTemplate

public void setTemplate(String template)
Set the template to render for this ActionResult.

Parameters:
template - the template to render for this ActionResult

render

public final void render(Context context)
Render the ActionResult to the client.

Parameters:
context - the request context

renderActionResult

protected void renderActionResult(Context context)
Render the ActionResult to the client. This method can be overridden by subclasses if custom rendering or direct access to the HttpServletResponse is required.

Parameters:
context - the request context