Action接口和AcitonSupport基类

Action:

为了让用户开发的Action类更加规范,Struts2提供了一个Action接口,这个接口定义了Struts2的Action处理类应该实现的规范。下面是标准Action接口的代码:

public interface Action {  
  
    //定义Action接口里包含的一些结果字符串  
    public static final String ERROR = "error";  
    public static final String INPUT = "input";  
    public static final String LOGIN = "login";  
    public static final String NONE = "none";  
    public static final String SUCCESS = "success";  
      
    //定义处理用户请求的execute()方法  
    public String execute() throws Exception;  
}

上面的Action接口里只定义了一个execute()方法,该接口规范规定了Action类应该包含一个execute()方法,该方法返回一个字符串,此外,该接口还定义了5个字符串常量,他的作用是统一execute()方法的返回值。

        例如,当Action类处理用户处理成功后,有人喜欢返回welcome字符串,有人喜欢返回success字符串,如此不利于项目的统一管 理,Struts2的Action接口定义加上了如上的5个字符串常量:ERROR,NONE,INPUT,LOGIN,SUCCESS等,分别代表了特 定的含义。当然,如果开发者依然希望使用特定的字符串作为逻辑视图名,开发者依然可以返回自己的视图名。

ActionSupport:

package com.opensymphony.xwork2;  
import com.opensymphony.xwork2.inject.Container;  
import com.opensymphony.xwork2.inject.Inject;  
import com.opensymphony.xwork2.util.ValueStack;  
import com.opensymphony.xwork2.util.logging.Logger;  
import com.opensymphony.xwork2.util.logging.LoggerFactory;  
import java.io.Serializable;  
import java.util.Collection;  
import java.util.List;  
import java.util.Locale;  
import java.util.Map;  
import java.util.ResourceBundle;  
  
/** 
* Provides a default implementation for the most common actions. 
* See the documentation for all the interfaces this class implements for more detailed information. 
*/  
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {  
protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class);  
private final ValidationAwareSupport validationAware = new ValidationAwareSupport();  
private transient TextProvider textProvider;  
private Container container;  
  
public void setActionErrors(Collection<String> errorMessages) {  
validationAware.setActionErrors(errorMessages);  
}  
public Collection<String> getActionErrors() {  
return validationAware.getActionErrors();  
}  
public void setActionMessages(Collection<String> messages) {  
validationAware.setActionMessages(messages);  
}  
public Collection<String> getActionMessages() {  
return validationAware.getActionMessages();  
}  
/** 
* @deprecated Use {@link #getActionErrors()}. 
*/  
@Deprecated  
public Collection<String> getErrorMessages() {  
return getActionErrors();  
}  
/** 
* @deprecated Use {@link #getFieldErrors()}. 
*/  
@Deprecated  
public Map<String, List<String>> getErrors() {  
return getFieldErrors();  
}  
//设置表单域校验错误信息  
public void setFieldErrors(Map<String, List<String>> errorMap) {  
validationAware.setFieldErrors(errorMap);  
}  
//返回表单域错误校验信息  
public Map<String, List<String>> getFieldErrors() {  
return validationAware.getFieldErrors();  
}  
//控制locale的相关信息  
public Locale getLocale() {  
ActionContext ctx = ActionContext.getContext();  
if (ctx != null) {  
return ctx.getLocale();  
} else {  
LOG.debug("Action context not initialized");  
return null;  
}  
}  
public boolean hasKey(String key) {  
return getTextProvider().hasKey(key);  
}  
public String getText(String aTextName) {  
return getTextProvider().getText(aTextName);  
}  
//返回国际化信息的方法  
public String getText(String aTextName, String defaultValue) {  
return getTextProvider().getText(aTextName, defaultValue);  
}  
public String getText(String aTextName, String defaultValue, String obj) {  
return getTextProvider().getText(aTextName, defaultValue, obj);  
}  
public String getText(String aTextName, List<Object> args) {  
return getTextProvider().getText(aTextName, args);  
}  
public String getText(String key, String[] args) {  
return getTextProvider().getText(key, args);  
}  
public String getText(String aTextName, String defaultValue, List<Object> args) {  
return getTextProvider().getText(aTextName, defaultValue, args);  
}  
public String getText(String key, String defaultValue, String[] args) {  
return getTextProvider().getText(key, defaultValue, args);  
}  
public String getText(String key, String defaultValue, List<Object> args, ValueStack stack) {  
return getTextProvider().getText(key, defaultValue, args, stack);  
}  
public String getText(String key, String defaultValue, String[] args, ValueStack stack) {  
return getTextProvider().getText(key, defaultValue, args, stack);  
}  
//用于访问国际化资源包的方法  
public ResourceBundle getTexts() {  
return getTextProvider().getTexts();  
}  
public ResourceBundle getTexts(String aBundleName) {  
return getTextProvider().getTexts(aBundleName);  
}  
//添加错误信息  
public void addActionError(String anErrorMessage) {  
validationAware.addActionError(anErrorMessage);  
}  
public void addActionMessage(String aMessage) {  
validationAware.addActionMessage(aMessage);  
}  
添加字段校验的错误信息  
public void addFieldError(String fieldName, String errorMessage) {  
validationAware.addFieldError(fieldName, errorMessage);  
}  
//默认Input方法,直接访问input字符串  
public String input() throws Exception {  
return INPUT;  
}  
public String doDefault() throws Exception {  
return SUCCESS;  
}  
/** 
* A default implementation that does nothing an returns "success". 
* <p/> 
* Subclasses should override this method to provide their business logic. 
* <p/> 
* See also {@link com.opensymphony.xwork2.Action#execute()}. 
* 
* @return returns {@link #SUCCESS} 
* @throws Exception can be thrown by subclasses. 
*/  
//默认处理用户请求的方法,直接返回SUCCESS字符串  
public String execute() throws Exception {  
return SUCCESS;  
}  
public boolean hasActionErrors() {  
return validationAware.hasActionErrors();  
}  
public boolean hasActionMessages() {  
return validationAware.hasActionMessages();  
}  
public boolean hasErrors() {  
return validationAware.hasErrors();  
}  
public boolean hasFieldErrors() {  
return validationAware.hasFieldErrors();  
}  
/** 
* Clears field errors. Useful for Continuations and other situations 
* where you might want to clear parts of the state on the same action. 
*/  
public void clearFieldErrors() {  
validationAware.clearFieldErrors();  
}  
/** 
* Clears action errors. Useful for Continuations and other situations 
* where you might want to clear parts of the state on the same action. 
*/  
public void clearActionErrors() {  
validationAware.clearActionErrors();  
}  
/** 
* Clears messages. Useful for Continuations and other situations 
* where you might want to clear parts of the state on the same action. 
*/  
public void clearMessages() {  
validationAware.clearMessages();  
}  
/** 
* Clears all errors. Useful for Continuations and other situations 
* where you might want to clear parts of the state on the same action. 
*/  
public void clearErrors() {  
validationAware.clearErrors();  
}  
/** 
* Clears all errors and messages. Useful for Continuations and other situations 
* where you might want to clear parts of the state on the same action. 
*/  
//清理错误信息的方法  
public void clearErrorsAndMessages() {  
validationAware.clearErrorsAndMessages();  
}  
/** 
* A default implementation that validates nothing. 
* Subclasses should override this method to provide validations. 
*/  
//包含空校验的方法  
public void validate() {  
}  
@Override  
public Object clone() throws CloneNotSupportedException {  
return super.clone();  
}  
/** 
* <!-- START SNIPPET: pause-method --> 
* Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return 
* the specified result, such as {@link #SUCCESS}, {@link #INPUT}, etc. 
* <p/> 
* <p/> 
* The next time this action is invoked (and using the same continuation ID), the method will resume immediately 
* after where this method was called, with the entire call stack in the execute method restored. 
* <p/> 
* <p/> 
* Note: this method can <b>only</b> be called within the {@link #execute()} method. 
* <!-- END SNIPPET: pause-method --> 
* 
* @param result the result to return - the same type of return value in the {@link #execute()} method. 
*/  
public void pause(String result) {  
}  
/** 
* If called first time it will create {@link com.opensymphony.xwork2.TextProviderFactory}, 
* inject dependency (if {@link com.opensymphony.xwork2.inject.Container} is accesible) into in, 
* then will create new {@link com.opensymphony.xwork2.TextProvider} and store it in a field 
* for future references and at the returns reference to that field 
* 
* @return reference to field with TextProvider 
*/  
private TextProvider getTextProvider() {  
if (textProvider == null) {  
TextProviderFactory tpf = new TextProviderFactory();  
if (container != null) {  
container.inject(tpf);  
}  
textProvider = tpf.createInstance(getClass(), this);  
}  
return textProvider;  
}  
@Inject  
public void setContainer(Container container) {  
this.container = container;  
}  
}


转载于:https://my.oschina.net/u/1538782/blog/383105

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值