Action"

From Documentation
m (correct highlight (via JWB))
 
Line 3: Line 3:
 
__TOC__
 
__TOC__
  
An action is POJO. It does ''not'' have to implement any interface. Of course, it has to implement the method specified in <tt>WEB-INF/zest</tt>. For example,
+
An action is POJO. It does ''not'' have to implement any interface. Of course, it has to implement the method specified in <code>WEB-INF/zest</code>. For example,
  
<source lang="java" high="6">
+
<source lang="java" highlight="6">
 
package foo;
 
package foo;
 
public class HelloAction {
 
public class HelloAction {
Line 33: Line 33:
 
# <code>public String ''execute''(ActionContext ac);</code>
 
# <code>public String ''execute''(ActionContext ac);</code>
  
The method's name could be anything you prefer, as long as it is the same as the one you specified in <tt>WEB-INF/zest.xml</tt>.
+
The method's name could be anything you prefer, as long as it is the same as the one you specified in <code>WEB-INF/zest.xml</code>.
  
 
The signature with <javadoc type="interface" directory="zest">org.zkoss.zest.ActionContext</javadoc> has higher priority if both method are specified (and the signature with no arg will be ignored). It is used if you need the information of the request, such as <code>HttpServletRequest</code>.
 
The signature with <javadoc type="interface" directory="zest">org.zkoss.zest.ActionContext</javadoc> has higher priority if both method are specified (and the signature with no arg will be ignored). It is used if you need the information of the request, such as <code>HttpServletRequest</code>.
Line 49: Line 49:
  
 
== Handling Conversion Errors ==
 
== Handling Conversion Errors ==
If ZEST fails to coerce a parameter to the setter method's argument, an exception will be thrown. By default, the exception will be ignored (i.e., no error, no log). If you'd like to handle it, you could implement an error handle, <javadoc type="interface" directory="zest">org.zkoss.zest.sys.ErrorHandler</javadoc>, and specifies it in [[ZEST Essentials/Configuration/zest.xml/error-handler-class|the error-handler-class element]] in <tt>WEB-INF/zest.xml</tt>.
+
If ZEST fails to coerce a parameter to the setter method's argument, an exception will be thrown. By default, the exception will be ignored (i.e., no error, no log). If you'd like to handle it, you could implement an error handle, <javadoc type="interface" directory="zest">org.zkoss.zest.sys.ErrorHandler</javadoc>, and specifies it in [[ZEST Essentials/Configuration/zest.xml/error-handler-class|the error-handler-class element]] in <code>WEB-INF/zest.xml</code>.
  
 
== Disabling Parameter Conversion ==
 
== Disabling Parameter Conversion ==

Latest revision as of 02:57, 18 January 2022


An action is POJO. It does not have to implement any interface. Of course, it has to implement the method specified in WEB-INF/zest. For example,

package foo;
public class HelloAction {
	private String _message = "Welcome";

	/** The execute method specified in WEB-INF/zest.xml. */
	public String execute() {
		return "success";
	}

	/** Sets the message. */
	public void setMessage(String message) {
		_message = message;
	}
	/** Returns the message. */
	public String getMessage() {
		return _message;
	}
}

Signature of the execute Method

There are two kinds of signatures are allowed for the execute method of an action.

  1. public String execute();
  2. public String execute(ActionContext ac);

The method's name could be anything you prefer, as long as it is the same as the one you specified in WEB-INF/zest.xml.

The signature with ActionContext has higher priority if both method are specified (and the signature with no arg will be ignored). It is used if you need the information of the request, such as HttpServletRequest.

public class FooAction {
    public String execute(ActionContext ac) {
        HttpServletRequest request = ac.getServletRequest();
        ....

Parameters of Request

If a parameter of the request (ServletRequest.getParameterMap()) matches a setter method of the action, the method will be invoked to set the parameter's value. For example, assume the action has a method called void setIndex(int index) and the request contains a parameter, index=5 (such as http://foo.com/foo?index=5), then setIndex(5) will be invoked. Thus, you could retrieve the value directly from the action, such as int getIndex().

Handling Conversion Errors

If ZEST fails to coerce a parameter to the setter method's argument, an exception will be thrown. By default, the exception will be ignored (i.e., no error, no log). If you'd like to handle it, you could implement an error handle, ErrorHandler, and specifies it in the error-handler-class element in WEB-INF/zest.xml.

Disabling Parameter Conversion

If you prefer not to convert the parameters to the action, you could implement ParameterIgnored in the action. For example,

public class FooAction implements ParameterIgnored {
...

Version History

Last Update : 2022/01/18


Version Date Content
     



Last Update : 2022/01/18

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.