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 {@link #contentType} of the ActionResult must be set to the appropriate typein order for the client to recognize the response. ActionResult provides constants for some of the common
content types, including: {@link #XML text/xml}, {@link #HTML text/html}, {@link #JSON application/json}, {@link #TEXT 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 {@link org.apache.click.util.ClickUtils#getMimeType(java.lang.String)}:
// lookup content type for PNG String contentType = ClickUtils.getMimeType("png"); actionResult.setContentType(contentType);