Action接口和AcitonSupport基类

2023-11-19

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Action接口和AcitonSupport基类 的相关文章

  • 如何在 JPA 中使用枚举

    我有一个电影租赁系统的现有数据库 每部电影都有一个评级属性 在 SQL 中 他们使用约束来限制该属性的允许值 CONSTRAINT film rating check CHECK rating text text OR rating tex
  • 不同的 JDK 更新会产生不同的 Java 字节码吗?

    假设场景 我有一个项目 其源合规性级别指定为 1 5 现在 我使用两种不同的 JDK 编译此项目 首先使用 JDK 6 Update 7 然后使用 JDK 6 Update 20 这两个不同的 JDK 是否会生成不同的 Java 字节代码
  • 在 MongoDB Java 驱动程序中如何使用 $filter

    我有一个适用于 MQL 的查询 我需要将其翻译成Java MQL 中的查询如下所示 db
  • 按下按钮时清除编辑文本焦点并隐藏键盘

    我正在制作一个带有编辑文本和按钮的应用程序 当我在 edittext 中输入内容然后单击按钮时 我希望键盘和焦点在 edittext 上消失 但我似乎无法做到这一点 我在 XML 中插入了这两行代码 android focusable tr
  • 如何将列表转换为地图?

    最近我和一位同事讨论了转换的最佳方式是什么List to Map在 Java 中 这样做是否有任何具体的好处 我想知道最佳的转换方法 如果有人可以指导我 我将非常感激 这是个好方法吗 List
  • 使用 xuggle 将 mp3 转换为 wav 出现异常

    我正在尝试将 mp3 转换为 wav 代码在这里 String mp3 F work pic2talk38512 mp3 String wav F work pic2talk38512 wav TranscodeAudioAndVideo
  • Java,将 null 分配给对象和仅声明之间有什么区别

    之间有什么区别 Object o null and Object o 仅声明 有人可以回答我吗 这取决于您声明变量的范围 例如 局部变量没有default values在这种情况下你将不得不分配null手动 在这种情况下实例变量分配 nul
  • Eclipse 自动完成更改变量名称

    只是一个愚蠢的问题 但很难搜索 因为有很多关于 Eclipse 自动完成的主题 而且很难找到与我的问题匹配的内容 所以问题是 如果我写 MyClass MyVarName 然后按空格键 添加 new MyClass Eclipse 自动添加
  • Java 9 中可以使用提前编译吗?

    As per JEP 295 http openjdk java net jeps 295 任何 JDK 模块 类或用户代码的 AOT 编译都是实验性的 JDK 9 中不支持 要使用 AOT 化的 java base 模块 用户必须编译该模
  • 适用于 Solaris 的 Java 8 中缺少 javaws

    看起来 Oracle 从 Java 8 for Solaris 中删除了 Java Web Start javaws 在 Java 8u51 中不再可用 来自兼容性指南 http www oracle com technetwork jav
  • JSON 对象数组转 Java POJO

    将此 JSON 对象转换为 java 中的类 您的 POJO 类中的映射将如何 ownerName Robert pets name Kitty name Rex name Jake This kind of question is ver
  • 在 JavaFX 中拖动未装饰的舞台

    我希望将舞台设置为 未装饰 使其可拖动且可最小化 问题是我找不到这样做的方法 因为我遇到的示例是通过插入到主方法中的方法来实现的 我想通过控制器类中声明的方法来完成此操作 就像我如何使用下面的 WindowClose 方法来完成此操作 这是
  • 驱动程序信息:driver.version:未知,使用 ChromeDriver v78.0.3904.70 和 Chrome 浏览器 v78.0.3904.97

    我使用的是java 1 8和chrome浏览器版本78 0 3904 97 我正在尝试使用 chrome 驱动程序版本执行我的 selenium 脚本代码78 0 3904 70 但在执行时我面临以下问题并且 chrome 立即崩溃 Pic
  • Java中无参数的for循环

    我在看别人的代码 发现了这段代码 for 我不是 Java 专家 这行代码在做什么 起初 我认为这会创建一个无限循环 但在该程序员使用的同一个类中 while true 其中 如果我错了 请纠正我 是一个无限循环 这两个相同吗 为什么有人会
  • java Runtime.getRunTime().exec 和通配符?

    我正在尝试使用删除垃圾文件 Process p Runtime getRuntime exec 只要我不使用通配符 它 就可以正常工作 即 Process p Runtime getRuntime exec bin rm f specifi
  • 设置 JAVA_HOME 变量时出现问题

    所以我刚刚下载了 Android Studio 并尝试设置 JAVA HOME 变量以便我可以运行它 我使用的是 Windows 8 并按照我找到的所有说明进行操作 但无济于事 转到高级系统设置 gt 环境变量 然后使用包含我的 jre7
  • 处理照片上传的最佳方式是什么?

    我正在为一个家庭成员的婚礼制作一个网站 他们要求的一个功能是一个照片部分 所有客人都可以在婚礼结束后前往并上传他们的照片 我说这是一个很棒的想法 然后我就去实现它 那么只有一个问题 物流 上传速度很慢 现代相机拍摄的照片很大 2 5 兆 我
  • 我可以关闭并重新打开套接字吗?

    我学习了一个使用套接字的例子 在此示例中 客户端向服务器发送请求以打开套接字 然后服务器 侦听特定端口 打开套接字 一切都很好 套接字从双方 客户端和服务器 打开 但我仍然不清楚这个东西有多灵活 例如 客户端是否可以关闭一个打开的 从两端
  • Java 相当于 Python 的 urllib.urlencode(基于 HashMap 的 UrlEncode)

    From https stackoverflow com questions 2018026 should i use urllib or urllib2 2018103 2018103 Java 中 Python 的 urllib url
  • spring data jpa 过滤 @OneToMany 中的子项

    我有一个员工测试实体是父实体并且FunGroup信息子实体 这两个实体都是通过employeeId映射 我需要一种方法来过滤掉与搜索条件匹配的子实体 以便结果仅包含父实体和子实体 满足要求 员工测试类 Entity name Employe

随机推荐