01.package com.jadyer.action;
02.
03.import org.apache.struts2.config.Namespace;
04.import org.apache.struts2.config.Result;
05.import org.apache.struts2.config.Results;
06.
07.import com.opensymphony.xwork2.ActionSupport;
08.
09./********************【Zero Configuration】*********************************************************************************/
10.//官方文档中Zero Configuration叫做零配置,翻译到中国叫做注解配置。好处是不用写struts.xml,而将配置信息写在Action中
11.//Spring中也有两种配置方式,一种是在配置文件里面配,还有一种就是通过注解来配
12.//所以现在如果强迫自己弄明白了这里的注解的话,将来在学习Spring的时候,就能够变得简单一些
13.//而且注解这东西,将来用的会越来越广,因为它有很多的好处。Struts2.0.11版本中的注解的功能暂时还不是特别强大
14.//据官方网站上说:在Struts2.2版出来后,注解的功能会非常非常的强大,完全可以抛离struts.xml来写程序,程序员开发时就更方便了
15.//每个人都可以配置好自己的程序,不用集中在struts.xml中配置了,但统一性就会有所缺失,万一某人不按套路胡写一通,维护时很麻烦
16./********************【浅析Action的四大注解】*********************************************************************************/
17.//Struts2的所有注解中与Action相关的注解:ParentPackage、Namespace、Result、Results
18.//在org.apache.struts2.config包中有四个它们的同名类,就是四大注解相关的
19.//这四大注解都是类级别的,也就是说它们需要写在类的前面,接下来一一解释
20.//【@ParentPackage(value="struts-default")】相当于【<package extends="struts-default"/>】
21.//而ParentPackage注解的缺省设置就是继承struts-default包,所以它可以不写
22.//【@Namespace(value="")】相当于【<package namespace=""/>】或者是没有在包中定义命名空间
23.//而Namespace注解的缺省设置就是位于默认的命名空间中,所以它同样可以不写
24.//【@Result(name="success",value="/userSuc.jsp")】相当于【<result name="success">/userSuc.jsp</result>】
25.//而Result注解的name属性缺省设置就是success,但为了便于分析,仍建议显式的指明name属性
26.//在Action中配置好注解之后,还需要在【web.xml】中声明哪些类使用了注解,代码如下所示
27.//<filter>
28.// <filter-name>struts2</filter-name>
29.// <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
30.// <init-param>
31.// <!-- 这里actionPackage是固定格式。然后再指定使用注解的Action所在的包 -->
32.// <param-name>actionPackages</param-name>
33.// <param-value>com.jadyer.action</param-value>
34.// </init-param>
35.//</filter>
36.//此时Action在命名时一定要以Action结尾,注解在解释的时候,会先把名字中后面的Action去掉
37.//然后取Action名字中前面的字段作为访问的名字,比如说UserAction的调用,例如如下
38.//访问时访问【<a href="user.action" mce_href="user.action">添加用户</a>】即可,而且user首字母需要小写方能匹配成功
39.//如果Action的返回结果有多个,测试就需要用到【@Results】注解,具体用法见下方
40.//注意:当配置result的转发模式的时候,若写成type="redirect"会报告Type mismatch: cannot convert from String to Class的错误
41.//注意:这是因为在注解中是允许出现继承过来的东西的,所以此时就需要把redirect对应的类写在type值中
42.//注意:例如type=org.apache.struts2.dispatcher.ServletRedirectResult.class
43.//注意:然后Strtus2就会利用反射机制自动把这个类装载进来。此时不能添加双引号,因为它代表的是完整类,不是字符串
44./************************************************************************************************************************/
45.
46.//@ParentPackage(value="struts-default")
47.//@Namespace(value="")
48.//@Result(value="/userSuc.jsp")
49.//@Result(name="success", value="/userSuc.jsp")
50.@Namespace(value="/annotation")
51.@Results({
52. @Result(name="success", value="/userSuc.jsp"),
53. @Result(name="input", value="/annotationTest.jsp", type=org.apache.struts2.dispatcher.ServletRedirectResult.class)
54.})
55.public class UserAction extends ActionSupport {
56. private static final long serialVersionUID = -5137568969521746637L;
57.
58. private String username;
59. private String password;
60.
61. /* 两个属性对应的setter和getter略 */
62.
63. @Override
64. public String execute() throws Exception {
65. System.out.println("UserAction.execute() is invoked");
66. if(username.trim().equalsIgnoreCase("admin") && password.equals("jadyer")){
67. return SUCCESS;
68. }else{
69. return INPUT;
70. }
71. }
72.}
73.// @Before
74.// public void doBefore(){
75.// System.out.println("注解配置的方法在execute()执行之前调用了");
76.// }
77.//
78.// @After
79.// public void doAfter(){
80.// System.out.println("注解配置的方法在execute()执行之后调用了");
81.// }
82.//
83.// @BeforeResult
84.// public void doBeforeResult(){
85.// System.out.println("注解配置的方法在Result返回之前调用了");
86.// }
87./********************【浅析Interceptor的三大注解】***************************************************************************/
88.//在com.opensymphony.xwork2.interceptor.annotations包中定义了可用的配置拦截器的注解
89.//其中Before和After注解分别是在execute()执行之前和之后调用的,而BeforeResult注解是在结果集返回之前调用的
90.//Action的四大注解都是类级别的,而Interceptor的注解都是属于方法级别的,也就是说它们都需要写在类里面的方法之前
91.//想在拦截器里面使用注解的话,就必须配置xwork2.interceptor.annotations包的AnnotationWorkflowInterceptor类
92.//AnnotationWorkflowInterceptor类是调用这三个注解的前提,必须得把这个类配置在struts.xml中,配置方式如下
93.//<interceptors>
94.// <interceptor name="annoInterceptor" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"/>
95.//</interceptors>
96.//然后在需要使用注解配置的拦截器的<action/>中增加<interceptor-ref name="annoInterceptor"/>即可
97.//注意,这个时候不要忘记再引用一下默认的defaultStack拦截器
98.//那么当发送这个Action请求的时候,执行的就是:@Before----execute()----@BeforeResult----@After顺序
99.//这样看来,使用注解来配置拦截器的话,真的是非常非常简单。但是注解也有一个缺点:不能复用
100.//另外,我们同样也可以通过注解配置拦截器的方式,来实现权限验证。举例如下
101.//@Before
102.//public String doBefore(){
103.// if(session.get("user")==null){
104.// //如果没有session,则表示用户还没有登录,那么返回登录的结果集
105.// return LOGIN;
106.// }else{
107.// //若有session,则表示用户已经登录,可以去执行下一步
108.// //此处返回null,那么程序就会继续执行execute()方法
109.// return null;
110.// }
111.//}//这样就轻松实现了对用户的访问权限的验证了。所以说使用注解来配置Interceptor,有时候还是很方便的
112./************************************************************************************************************************/
annotation配置action
最新推荐文章于 2023-12-02 11:27:55 发布