Struts2(五):ActionSupport
我们在上一章节中的一个列子中使用到了一个标识跳转到登录页面的例子:
示例是这样写的:
index.jsp:
<br/> <a href="gotoLoginPage">login page</a>
这里表示要调转到登录login.jsp页面,不过这个跳转是一个action。
在struts.xml中对应注册:
1 <!-- default class="com.opensymphony.xwork2.ActionSupport" method="execute" --> 2 <action name="gotoLoginPage"> 3 <!-- name default value:"success" --> 4 <result>/login.jsp</result> 5 </action>
不错,这里就是这么写的。在index.jsp页面中点login page连接确实跳转到了登录页面login.jsp。
- 为什么在struts.xml中缺省的class是“com.opensymphony.xwork2.ActionSupport”,而缺省的method又是“execute”呢?
这和struts.xml中的package有关系:
<package name="default" namespace="/" extends="struts-default"> <!-- default class="com.opensymphony.xwork2.ActionSupport" method="execute" --> <action name="gotoLoginPage"> <!-- name default value:"success" --> <result>/login.jsp</result> </action> </package>
我们看到这里边<package>节点属性中的extends继承了struts-default包,而这个struts-default package是在哪里定义的呢?
在struts2-core-2.3.28.jar包的根目录下,包含了一个struts-default.xml的配置文件,在该文件中声明的有一个package叫struts-default。
我们将struts-default package定义节点拷贝出来分析下:
1 <package name="struts-default" abstract="true"> 2 <result-types> 3 <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> 4 <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> 5 <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> 6 <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> 7 <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> 8 <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> 9 <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> 10 <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> 11 <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> 12 <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> 13 <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /> 14 </result-types> 15 16 <interceptors> 17 <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/> 18 <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/> 19 <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/> 20 <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/> 21 <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/> 22 <interceptor name="cookieProvider" class="org.apache.struts2.interceptor.CookieProviderInterceptor"/> 23 <interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" /> 24 <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" /> 25 <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" /> 26 <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/> 27 <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/> 28 <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/> 29 <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/> 30 <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/> 31 <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/> 32 <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/> 33 <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/> 34 <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/> 35 <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/> 36 <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/> 37 <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/> 38 <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/> 39 <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/> 40 <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/> 41 <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/> 42 <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/> 43 <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/> 44 <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" /> 45 <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" /> 46 <interceptor name="datetime" class="org.apache.struts2.interceptor.DateTextFieldInterceptor" /> 47 <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" /> 48 <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" /> 49 <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" /> 50 <interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" /> 51 <interceptor name="deprecation" class="org.apache.struts2.interceptor.DeprecationInterceptor" /> 52 53 <!-- Basic stack --> 54 <interceptor-stack name="basicStack"> 55 <interceptor-ref name="exception"/> 56 <interceptor-ref name="servletConfig"/> 57 <interceptor-ref name="prepare"/> 58 <interceptor-ref name="checkbox"/> 59 <interceptor-ref name="datetime"/> 60 <interceptor-ref name="multiselect"/> 61 <interceptor-ref name="actionMappingParams"/> 62 <interceptor-ref name="params"/> 63 <interceptor-ref name="conversionError"/> 64 <interceptor-ref name="deprecation"/> 65 </interceptor-stack> 66 67 <!-- Sample validation and workflow stack --> 68 <interceptor-stack name="validationWorkflowStack"> 69 <interceptor-ref name="basicStack"/> 70 <interceptor-ref name="validation"/> 71 <interceptor-ref name="workflow"/> 72 </interceptor-stack> 73 74 <!-- Sample file upload stack --> 75 <interceptor-stack name="fileUploadStack"> 76 <interceptor-ref name="fileUpload"/> 77 <interceptor-ref name="basicStack"/> 78 </interceptor-stack> 79 80 <!-- Sample model-driven stack --> 81 <interceptor-stack name="modelDrivenStack"> 82 <interceptor-ref name="modelDriven"/> 83 <interceptor-ref name="basicStack"/> 84 </interceptor-stack> 85 86 <!-- Sample action chaining stack --> 87 <interceptor-stack name="chainStack"> 88 <interceptor-ref name="chain"/> 89 <interceptor-ref name="basicStack"/> 90 </interceptor-stack> 91 92 <!-- Sample i18n stack --> 93 <interceptor-stack name="i18nStack"> 94 <interceptor-ref name="i18n"/> 95 <interceptor-ref name="basicStack"/> 96 </interceptor-stack> 97 98 <!-- An example of the paramsPrepareParams trick. This stack 99 is exactly the same as the defaultStack, except that it 100 includes one extra interceptor before the prepare interceptor: 101 the params interceptor. 102 103 This is useful for when you wish to apply parameters directly 104 to an object that you wish to load externally (such as a DAO 105 or database or service layer), but can't load that object 106 until at least the ID parameter has been loaded. By loading 107 the parameters twice, you can retrieve the object in the 108 prepare() method, allowing the second params interceptor to 109 apply the values on the object. --> 110 <interceptor-stack name="paramsPrepareParamsStack"> 111 <interceptor-ref name="exception"/> 112 <interceptor-ref name="alias"/> 113 <interceptor-ref name="i18n"/> 114 <interceptor-ref name="checkbox"/> 115 <interceptor-ref name="datetime"/> 116 <interceptor-ref name="multiselect"/> 117 <interceptor-ref name="params"/> 118 <interceptor-ref name="servletConfig"/> 119 <interceptor-ref name="prepare"/> 120 <interceptor-ref name="chain"/> 121 <interceptor-ref name="modelDriven"/> 122 <interceptor-ref name="fileUpload"/> 123 <interceptor-ref name="staticParams"/> 124 <interceptor-ref name="actionMappingParams"/> 125 <interceptor-ref name="params"/> 126 <interceptor-ref name="conversionError"/> 127 <interceptor-ref name="validation"> 128 <param name="excludeMethods">input,back,cancel,browse</param> 129 </interceptor-ref> 130 <interceptor-ref name="workflow"> 131 <param name="excludeMethods">input,back,cancel,browse</param> 132 </interceptor-ref> 133 </interceptor-stack> 134 135 <!-- A complete stack with all the common interceptors in place. 136 Generally, this stack should be the one you use, though it 137 may do more than you need. Also, the ordering can be 138 switched around (ex: if you wish to have your servlet-related 139 objects applied before prepare() is called, you'd need to move 140 servletConfig interceptor up. 141 142 This stack also excludes from the normal validation and workflow 143 the method names input, back, and cancel. These typically are 144 associated with requests that should not be validated. 145 --> 146 <interceptor-stack name="defaultStack"> 147 <interceptor-ref name="exception"/> 148 <interceptor-ref name="alias"/> 149 <interceptor-ref name="servletConfig"/> 150 <interceptor-ref name="i18n"/> 151 <interceptor-ref name="prepare"/> 152 <interceptor-ref name="chain"/> 153 <interceptor-ref name="scopedModelDriven"/> 154 <interceptor-ref name="modelDriven"/> 155 <interceptor-ref name="fileUpload"/> 156 <interceptor-ref name="checkbox"/> 157 <interceptor-ref name="datetime"/> 158 <interceptor-ref name="multiselect"/> 159 <interceptor-ref name="staticParams"/> 160 <interceptor-ref name="actionMappingParams"/> 161 <interceptor-ref name="params"/> 162 <interceptor-ref name="conversionError"/> 163 <interceptor-ref name="validation"> 164 <param name="excludeMethods">input,back,cancel,browse</param> 165 </interceptor-ref> 166 <interceptor-ref name="workflow"> 167 <param name="excludeMethods">input,back,cancel,browse</param> 168 </interceptor-ref> 169 <interceptor-ref name="debugging"/> 170 <interceptor-ref name="deprecation"/> 171 </interceptor-stack> 172 173 <!-- The completeStack is here for backwards compatibility for 174 applications that still refer to the defaultStack by the 175 old name --> 176 <interceptor-stack name="completeStack"> 177 <interceptor-ref name="defaultStack"/> 178 </interceptor-stack> 179 180 <!-- Sample execute and wait stack. 181 Note: execAndWait should always be the *last* interceptor. --> 182 <interceptor-stack name="executeAndWaitStack"> 183 <interceptor-ref name="execAndWait"> 184 <param name="excludeMethods">input,back,cancel</param> 185 </interceptor-ref> 186 <interceptor-ref name="defaultStack"/> 187 <interceptor-ref name="execAndWait"> 188 <param name="excludeMethods">input,back,cancel</param> 189 </interceptor-ref> 190 </interceptor-stack> 191 192 </interceptors> 193 194 <default-interceptor-ref name="defaultStack"/> 195 196 <default-class-ref class="com.opensymphony.xwork2.ActionSupport" /> 197 </package>
这里边都包含了:返回类型,拦截器,拦截器栈,默认的拦截器栈,Action默认的class。
不错这是这里的“<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />”制定了action的默认class,而method的默认方法是ActionSupport中的execute方法。
分析ActionSupport类:
public class com.opensymphony.xwork2.ActionSupport implements com.opensymphony.xwork2.Action, com.opensymphony.xwork2.Validateable, com.opensymphony.xwork2.ValidationAware, com.opensymphony.xwork2.TextProvider, com.opensymphony.xwork2.LocaleProvider, java.io.Serializable {}
ActionSupport定义实现了Action接口、Validateable抽象类、ValidateAware接口、TextProvider接口、LocalProvider接口、Serializeable接口。
1、Action抽象接口中定义了几个返回值常量SUCCESS,INPUT,ERROR,NONE,LOGIN,及一个抽象方法execute接口;
2、Validateable抽象接口类中定义了一个validate抽象方法;
3、ValidateAware抽象接口类中定义
hasActionErrors,hasActionMessages,hasErrors,hasFieldErrors,
addActionError,addActionMessage,addFieldError,
getFieldErrors,getFieldErrors,
setActionMessages,getActionMessages,
setActionErrors,getActionErrors
4、TextProvider,LocalProvider抽象接口类是实现国际化使用的。
从上边ActionSupport定义,得知如果我们要想使用验证,或者国际化等最好是在定义Action时继承ActionSupport类。
基础才是编程人员应该深入研究的问题,比如:
1)List/Set/Map内部组成原理|区别
2)mysql索引存储结构&如何调优/b-tree特点、计算复杂度及影响复杂度的因素。。。
3)JVM运行组成与原理及调优
4)Java类加载器运行原理
5)Java中GC过程原理|使用的回收算法原理
6)Redis中hash一致性实现及与hash其他区别
7)Java多线程、线程池开发、管理Lock与Synchroined区别
8)Spring IOC/AOP 原理;加载过程的。。。
【+加关注】。