我们在上一章节中的一个列子中使用到了一个标识跳转到登录页面的例子:

示例是这样写的:

index.jsp:

    <br/>
<a href="gotoLoginPage">login page</a>

这里表示要调转到登录login.jsp页面,不过这个跳转是一个action。

在struts.xml中对应注册:

     <!-- default class="com.opensymphony.xwork2.ActionSupport" method="execute" -->
<action name="gotoLoginPage">
<!-- name default value:"success" -->
<result>/login.jsp</result>
</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定义节点拷贝出来分析下:

   <package name="struts-default" abstract="true">
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
<result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
</result-types> <interceptors>
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
<interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
<interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
<interceptor name="cookieProvider" class="org.apache.struts2.interceptor.CookieProviderInterceptor"/>
<interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
<interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
<interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
<interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
<interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
<interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
<interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
<interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
<interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
<interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
<interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
<interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
<interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
<interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
<interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />
<interceptor name="datetime" class="org.apache.struts2.interceptor.DateTextFieldInterceptor" />
<interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
<interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
<interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
<interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />
<interceptor name="deprecation" class="org.apache.struts2.interceptor.DeprecationInterceptor" /> <!-- Basic stack -->
<interceptor-stack name="basicStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="datetime"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="deprecation"/>
</interceptor-stack> <!-- Sample validation and workflow stack -->
<interceptor-stack name="validationWorkflowStack">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack> <!-- Sample file upload stack -->
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack> <!-- Sample model-driven stack -->
<interceptor-stack name="modelDrivenStack">
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack> <!-- Sample action chaining stack -->
<interceptor-stack name="chainStack">
<interceptor-ref name="chain"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack> <!-- Sample i18n stack -->
<interceptor-stack name="i18nStack">
<interceptor-ref name="i18n"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack> <!-- An example of the paramsPrepareParams trick. This stack
is exactly the same as the defaultStack, except that it
includes one extra interceptor before the prepare interceptor:
the params interceptor. This is useful for when you wish to apply parameters directly
to an object that you wish to load externally (such as a DAO
or database or service layer), but can't load that object
until at least the ID parameter has been loaded. By loading
the parameters twice, you can retrieve the object in the
prepare() method, allowing the second params interceptor to
apply the values on the object. -->
<interceptor-stack name="paramsPrepareParamsStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="datetime"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="params"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack> <!-- A complete stack with all the common interceptors in place.
Generally, this stack should be the one you use, though it
may do more than you need. Also, the ordering can be
switched around (ex: if you wish to have your servlet-related
objects applied before prepare() is called, you'd need to move
servletConfig interceptor up. This stack also excludes from the normal validation and workflow
the method names input, back, and cancel. These typically are
associated with requests that should not be validated.
-->
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="datetime"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging"/>
<interceptor-ref name="deprecation"/>
</interceptor-stack> <!-- The completeStack is here for backwards compatibility for
applications that still refer to the defaultStack by the
old name -->
<interceptor-stack name="completeStack">
<interceptor-ref name="defaultStack"/>
</interceptor-stack> <!-- Sample execute and wait stack.
Note: execAndWait should always be the *last* interceptor. -->
<interceptor-stack name="executeAndWaitStack">
<interceptor-ref name="execAndWait">
<param name="excludeMethods">input,back,cancel</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="execAndWait">
<param name="excludeMethods">input,back,cancel</param>
</interceptor-ref>
</interceptor-stack> </interceptors> <default-interceptor-ref name="defaultStack"/> <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
</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类。

Struts2(五):ActionSupport的更多相关文章

  1. struts2 中 Actionsupport 的作用

    struts2 中 Actionsupport 的作用 Action 跟 Actionsupport 的区别     当我们在写action的时候,可以实现Action接口,也可以继承Actionsu ...

  2. Struts2五、Struts1与Struts2的区别

    Struts1和Struts2的区别和对比: Action 类:  • Struts1要求Action类继承一个抽象基类.Struts1的一个普遍问题是使用抽象类编程而不是接口,而struts2的Ac ...

  3. 【JAVA得知】struts2 于 Actionsupport 任务

    尊重原创:http://xumiao900.iteye.com/blog/469760     Action 跟 Actionsupport 的差别      当我们在写action的时候,能够实现A ...

  4. Struts2(五)数据校验

    一.概述 在提交表单数据时,如果数据需要保存到数据库,空输入等可能会引发一些异常,为了避免引起用户的输入引起底层异常,通常在进行业务逻辑操作之前,先执行基本的数据校验. 下面通过两种方式来阐述Stru ...

  5. Struts2(五)——核心拦截器

    Struts框架一共为我们提供了35个拦截器,其中默认的拦截器有18个,框架访问action的异常处理,配置信息处理,转发重定向选择,上传等等等等,都是这18个拦截器中设置的,起着非比寻常的作用.而这 ...

  6. Struts2(五)常量的配置

    Struts2 常量大多在 默认的配置文件中已经配置好,但根据用户的需求不同,开发的要求不同,需要修改这些常量值,修改的方法就是在配置的文件对常量进行重新配置 在struts.xml 文件中使用< ...

  7. struts2(五) s标签和国际化

    坚持就是胜利. --WH 一.s标签 在struts-2.3.15.1/docs/WW/docs/tag-reference.html下,就有着struts2所有标签的参考文献,只能看看其中比较常用的 ...

  8. Struts2(五)Action二配置

    一.method参数 action package com.pb.web.action; public class HourseAction { public String add(){ System ...

  9. Struts2(五.用户注册的实现及整合Action的配置方法)

    一.用户注册功能 register.jsp页面 若是jquery ajax方式提交给action,还要回到jquery,控制权在jquery若是表单方式提交给action,控制权交给action &l ...

随机推荐

  1. swiper初步探索

    最近要做一个效果,初步想到了使用swiper,不过貌似最后并不能完全通过swiper来实现,整整试了一天的时间都没有试出来,真是...压力很大,不过自己选的路,总要坚持走下去了. Swiper(Swi ...

  2. 【noiOj】p8207(233)

    07:和为给定数 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 给出若干个整数,询问其中是否有一对数的和等于给定的数. 输入 共三行:第一行是整数n(0 & ...

  3. SQL Server2008清空日志文件

    USE[master] GO ALTER DATABASE mydbname SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE mydbname S ...

  4. 20145330第七周《Java学习笔记》

    20145330第七周<Java学习笔记> 第十三章 时间与日期 认识时间与日期 时间的度量 GMT(格林威治标准时间):现在不是标准时间 世界时(UT):1972年UTC出来之前,UT等 ...

  5. Thymeleaf学习内容

    Thymeleaf Page Layouts Spring MVC and Thymeleaf: how to access data from templates thymeleaf-layout- ...

  6. Odoo 路线规则实现机制浅析

    事情是这个样子的:项目在实施过程中,碰到A仓库向B仓库供货的情况,心想这还不简单,老老实实地建多个仓库并将B仓库的供货仓库选为A仓库,再设置好产品的再订购规则,万事大吉了.然而,事情并非想象的那么简单 ...

  7. CentOS默认开放的本地端口范围

    系统本地开放端口的范围:(默认30000多到60000多) [root@linux2 ~]# vim /etc/sysctl.conf net.ipv4.ip_local_port_range = 1 ...

  8. iframe自适应高度js

    <iframe src="http://www.fufuok.com/" id="iframepage" name="iframepage&qu ...

  9. 树莓派3b+ 用samba与windows共享文件

    1. 树莓派安装samba sudo apt-get install samba 2. 设置一个公共目录 cd /;sudo mkdir share;sudo chmod 777 sharesudo ...

  10. CSS3初学篇章_3(属性选择符/字体样式/元素样式)

    属性选择符  选择符  说明  E[att]  选择具有att属性的E元素.  E[att="val"]  选择具有att属性且属性值等于val的E元素.  E[att~=&quo ...