zoukankan      html  css  js  c++  java
  • Struts2实现接口和继承ActionSupport基类(转)

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

    [java] view plain copy
     
    1. package ppp;  
    2.   
    3. public interface Action {  
    4.   
    5.     //定义Action接口里包含的一些结果字符串  
    6.     public static final String ERROR = "error";  
    7.     public static final String INPUT = "input";  
    8.     public static final String LOGIN = "login";  
    9.     public static final String NONE = "none";  
    10.     public static final String SUCCESS = "success";  
    11.       
    12.     //定义处理用户请求的execute()方法  
    13.     public String execute() throws Exception;  
    14. }  


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

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

            另外,Struts2还为Action接口提供了一个实现类:ActionSupport,下面是该实现类的代码:

    [java] view plain copy
     
    1. package com.opensymphony.xwork2;  
    2. import com.opensymphony.xwork2.inject.Container;  
    3. import com.opensymphony.xwork2.inject.Inject;  
    4. import com.opensymphony.xwork2.util.ValueStack;  
    5. import com.opensymphony.xwork2.util.logging.Logger;  
    6. import com.opensymphony.xwork2.util.logging.LoggerFactory;  
    7. import java.io.Serializable;  
    8. import java.util.Collection;  
    9. import java.util.List;  
    10. import java.util.Locale;  
    11. import java.util.Map;  
    12. import java.util.ResourceBundle;  
    13.   
    14. /** 
    15. * Provides a default implementation for the most common actions. 
    16. * See the documentation for all the interfaces this class implements for more detailed information. 
    17. */  
    18. public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {  
    19. protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class);  
    20. private final ValidationAwareSupport validationAware = new ValidationAwareSupport();  
    21. private transient TextProvider textProvider;  
    22. private Container container;  
    23.   
    24. public void setActionErrors(Collection<String> errorMessages) {  
    25. validationAware.setActionErrors(errorMessages);  
    26. }  
    27. public Collection<String> getActionErrors() {  
    28. return validationAware.getActionErrors();  
    29. }  
    30. public void setActionMessages(Collection<String> messages) {  
    31. validationAware.setActionMessages(messages);  
    32. }  
    33. public Collection<String> getActionMessages() {  
    34. return validationAware.getActionMessages();  
    35. }  
    36. /** 
    37. * @deprecated Use {@link #getActionErrors()}. 
    38. */  
    39. @Deprecated  
    40. public Collection<String> getErrorMessages() {  
    41. return getActionErrors();  
    42. }  
    43. /** 
    44. * @deprecated Use {@link #getFieldErrors()}. 
    45. */  
    46. @Deprecated  
    47. public Map<String, List<String>> getErrors() {  
    48. return getFieldErrors();  
    49. }  
    50. //设置表单域校验错误信息  
    51. public void setFieldErrors(Map<String, List<String>> errorMap) {  
    52. validationAware.setFieldErrors(errorMap);  
    53. }  
    54. //返回表单域错误校验信息  
    55. public Map<String, List<String>> getFieldErrors() {  
    56. return validationAware.getFieldErrors();  
    57. }  
    58. //控制locale的相关信息  
    59. public Locale getLocale() {  
    60. ActionContext ctx = ActionContext.getContext();  
    61. if (ctx != null) {  
    62. return ctx.getLocale();  
    63. else {  
    64. LOG.debug("Action context not initialized");  
    65. return null;  
    66. }  
    67. }  
    68. public boolean hasKey(String key) {  
    69. return getTextProvider().hasKey(key);  
    70. }  
    71. public String getText(String aTextName) {  
    72. return getTextProvider().getText(aTextName);  
    73. }  
    74. //返回国际化信息的方法  
    75. public String getText(String aTextName, String defaultValue) {  
    76. return getTextProvider().getText(aTextName, defaultValue);  
    77. }  
    78. public String getText(String aTextName, String defaultValue, String obj) {  
    79. return getTextProvider().getText(aTextName, defaultValue, obj);  
    80. }  
    81. public String getText(String aTextName, List<Object> args) {  
    82. return getTextProvider().getText(aTextName, args);  
    83. }  
    84. public String getText(String key, String[] args) {  
    85. return getTextProvider().getText(key, args);  
    86. }  
    87. public String getText(String aTextName, String defaultValue, List<Object> args) {  
    88. return getTextProvider().getText(aTextName, defaultValue, args);  
    89. }  
    90. public String getText(String key, String defaultValue, String[] args) {  
    91. return getTextProvider().getText(key, defaultValue, args);  
    92. }  
    93. public String getText(String key, String defaultValue, List<Object> args, ValueStack stack) {  
    94. return getTextProvider().getText(key, defaultValue, args, stack);  
    95. }  
    96. public String getText(String key, String defaultValue, String[] args, ValueStack stack) {  
    97. return getTextProvider().getText(key, defaultValue, args, stack);  
    98. }  
    99. //用于访问国际化资源包的方法  
    100. public ResourceBundle getTexts() {  
    101. return getTextProvider().getTexts();  
    102. }  
    103. public ResourceBundle getTexts(String aBundleName) {  
    104. return getTextProvider().getTexts(aBundleName);  
    105. }  
    106. //添加错误信息  
    107. public void addActionError(String anErrorMessage) {  
    108. validationAware.addActionError(anErrorMessage);  
    109. }  
    110. public void addActionMessage(String aMessage) {  
    111. validationAware.addActionMessage(aMessage);  
    112. }  
    113. 添加字段校验的错误信息  
    114. public void addFieldError(String fieldName, String errorMessage) {  
    115. validationAware.addFieldError(fieldName, errorMessage);  
    116. }  
    117. //默认Input方法,直接访问input字符串  
    118. public String input() throws Exception {  
    119. return INPUT;  
    120. }  
    121. public String doDefault() throws Exception {  
    122. return SUCCESS;  
    123. }  
    124. /** 
    125. * A default implementation that does nothing an returns "success". 
    126. * <p/> 
    127. * Subclasses should override this method to provide their business logic. 
    128. * <p/> 
    129. * See also {@link com.opensymphony.xwork2.Action#execute()}. 
    130. * @return returns {@link #SUCCESS} 
    131. * @throws Exception can be thrown by subclasses. 
    132. */  
    133. //默认处理用户请求的方法,直接返回SUCCESS字符串  
    134. public String execute() throws Exception {  
    135. return SUCCESS;  
    136. }  
    137. public boolean hasActionErrors() {  
    138. return validationAware.hasActionErrors();  
    139. }  
    140. public boolean hasActionMessages() {  
    141. return validationAware.hasActionMessages();  
    142. }  
    143. public boolean hasErrors() {  
    144. return validationAware.hasErrors();  
    145. }  
    146. public boolean hasFieldErrors() {  
    147. return validationAware.hasFieldErrors();  
    148. }  
    149. /** 
    150. * Clears field errors. Useful for Continuations and other situations 
    151. * where you might want to clear parts of the state on the same action. 
    152. */  
    153. public void clearFieldErrors() {  
    154. validationAware.clearFieldErrors();  
    155. }  
    156. /** 
    157. * Clears action errors. Useful for Continuations and other situations 
    158. * where you might want to clear parts of the state on the same action. 
    159. */  
    160. public void clearActionErrors() {  
    161. validationAware.clearActionErrors();  
    162. }  
    163. /** 
    164. * Clears messages. Useful for Continuations and other situations 
    165. * where you might want to clear parts of the state on the same action. 
    166. */  
    167. public void clearMessages() {  
    168. validationAware.clearMessages();  
    169. }  
    170. /** 
    171. * Clears all errors. Useful for Continuations and other situations 
    172. * where you might want to clear parts of the state on the same action. 
    173. */  
    174. public void clearErrors() {  
    175. validationAware.clearErrors();  
    176. }  
    177. /** 
    178. * Clears all errors and messages. Useful for Continuations and other situations 
    179. * where you might want to clear parts of the state on the same action. 
    180. */  
    181. //清理错误信息的方法  
    182. public void clearErrorsAndMessages() {  
    183. validationAware.clearErrorsAndMessages();  
    184. }  
    185. /** 
    186. * A default implementation that validates nothing. 
    187. * Subclasses should override this method to provide validations. 
    188. */  
    189. //包含空校验的方法  
    190. public void validate() {  
    191. }  
    192. @Override  
    193. public Object clone() throws CloneNotSupportedException {  
    194. return super.clone();  
    195. }  
    196. /** 
    197. * <!-- START SNIPPET: pause-method --> 
    198. * Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return 
    199. * the specified result, such as {@link #SUCCESS}, {@link #INPUT}, etc. 
    200. * <p/> 
    201. * <p/> 
    202. * The next time this action is invoked (and using the same continuation ID), the method will resume immediately 
    203. * after where this method was called, with the entire call stack in the execute method restored. 
    204. * <p/> 
    205. * <p/> 
    206. * Note: this method can <b>only</b> be called within the {@link #execute()} method. 
    207. * <!-- END SNIPPET: pause-method --> 
    208. * @param result the result to return - the same type of return value in the {@link #execute()} method. 
    209. */  
    210. public void pause(String result) {  
    211. }  
    212. /** 
    213. * If called first time it will create {@link com.opensymphony.xwork2.TextProviderFactory}, 
    214. * inject dependency (if {@link com.opensymphony.xwork2.inject.Container} is accesible) into in, 
    215. * then will create new {@link com.opensymphony.xwork2.TextProvider} and store it in a field 
    216. * for future references and at the returns reference to that field 
    217. * @return reference to field with TextProvider 
    218. */  
    219. private TextProvider getTextProvider() {  
    220. if (textProvider == null) {  
    221. TextProviderFactory tpf = new TextProviderFactory();  
    222. if (container != null) {  
    223. container.inject(tpf);  
    224. }  
    225. textProvider = tpf.createInstance(getClass(), this);  
    226. }  
    227. return textProvider;  
    228. }  
    229. @Inject  
    230. public void setContainer(Container container) {  
    231. this.container = container;  
    232. }  
    233. }  

            正如上面代码中的,ActionSupport是一个默认的Action实现类,该类里已经提供了许多默认方法,这些方法包括获取国际化信息的方法、数据校验的方法、默认的处理用户请求的方法等,实际上,ActionSupport是Struts2的默认的Action处理类,如果让开发者的Action类继承该ActionSupport类,则会大大简化Action的开发。

  • 相关阅读:
    几种常见的基于Lucene的开源搜索解决方案对比
    【例子】Bobobrowse:lucene分组统计扩展组件
    Solr 相似页面MoreLikeThis
    eclipse安装、基本使用、常用设置
    iPhone开发入门守则:ObjectiveC编码规范系列教程
    ios开发学习按钮(Button)效果源码分享
    ios开发学习音频声效(Audio)效果源码分享系列教程
    ios开发学习动画(Animation)效果源码分享系列教程1
    ios开发学习图表(Chart)效果源码分享系列教程
    局网满猿关不住,一波码农出墙来。
  • 原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6542713.html
Copyright © 2011-2022 走看看