zoukankan      html  css  js  c++  java
  • Struts注解介绍

    http://wenku.baidu.com/view/b8b71725bcd126fff7050b3a.html

    ————————————————————————————————————————————————————————

    Struts注解介绍

    注解名称

    备注

    作用域

    @Action

    指定一个action

    类,方法,actions注解中

    @Actions

    给一个action指定多个名称

    方法

    @ParentPackage

    指定继承的包(struts2)名

    包,类

    @Namespace

    指定action所属的名称空间

    包,类

    @Namespaces

    指定一组action所属的名称空间

    包,类

    @Result

    定义一个result映射

    类,Results注解中

    @Results

    定义一组result映射

    类,action注解中

    @InterceptorRef

    一个拦截器

    类,InterceptorRefs注解中

    @InterceptorRefs

    一组拦截器集合

    类,action注解中

     

    下面是各注解的具体实现说明:

    1.@ParentPackage 注解的参数:

    参数

    类型

    是否必须

    默认值

    描述

    value

    String

    指要继承的父包

     

    2.@Namespace注解的参数如下表:

    参数

    类型

    是否必须

    默认值

    描述

    value

    String

    指action所属的名称空间

    如:@Namespace(value=”/”)或@Namespace(“/list”)。

    注意:如果没有给定Namespace选项,会默认为从定义的.package.locators标示开始到包结束的部分,就是hi命名空间,比如:

    com.tepia.project.user.userAction的命名空间是:”/user”。

    com.tepia.project.user.detail.UserAction的命名空间是:”/user/detail”。

     

    3.@Namespaces用于声明一组命名空间,参数如下表所示:

    参数

    类型

    是否必须

    默认值

    描述

    value

    Namespace

    在里面定义一个Namespace的数组

    如:

    @Namespaces({

    @Namespace(“/user”),

    @Namespace(“/main”)

    })

    public class BookAction extends ActionSupport{……}

    也就是可以通过发起:/user/book!add.action

                                              /main/book!add.action

    两个uri请求都能访问到Action中的方法。

     

    4.@Result注解的参数如下表所示:

    参数

    类型

    是否必须

    默认值

    描述

    name

    String

    Action.SUCCESS

    指定result的逻辑名即代码结果

    location

    String

             无

    指定result对应的URL

    type

    String

    dispatcher

    指定result的类型

    params

    String

    {}

    为result传递参数,格式为{key1,value1,key2,value2}

    对应的@Results和@Result可分为全局的与本地的

    1.全局的(global)

         @Result({

             @Result(name=”failure”,location=”/WEB-INF/fail.jsp”) 

         })

    public class HelloWorld extends ActionSupport{

        Public String execute(){ return “failure”;}

    }

    2.本地的(local)

         public class HelloWorld extends ActionSupport{

       @Action(value=”/other/bar”,results=(

       @Result{name=”error”,location=”www.baidu.com”,type=”redirect”)})

        public String method1(){return “error”;}

     }

     

    5.@Results注解的参数如下表所示:

    参数

    类型

    是否必须

    默认值

    描述

    value

    Result[]

    为action定义一组result映射

    如:

    @Results({

       @Result(name=”input”,value=”/input.jsp”),

       @Result(name=”success”,value=”/success.jsp”)

    })

    //上例中定义了两个result映射,一个逻辑名是input,资源位置是/input.jsp;另一个逻辑名是success,资源位置是/success.jsp

     

    public class HelloAction extends ActionSupport{

    @Action(“action1”)

    public String method1(){……}

    @Action(“action2”)

    public String method2(){……}

    @Action(value=”/user/action3”,

    results={

      @Result(

         name=”viewsuccess”,

         location=”/view/showbooks.jsp”),

       @Result(

          name=”viewinput”,

          location=”/view/showbooks.jsp”,

          type=”redirectAction”)

      },

    interceptorRefs={@InterceptorRef(“my defaultStack)}

    )

    public String method3(){……}

    }

    //如果采用此种方式,可以使用如下方式访问

    method1  /action1!method1.action

    method2  /user/action2!method2.action

    method3  /user/action3!method3.action

     

    6.@Actions 注解可以为action类定义一组访问的action名称,主要有如下参数:

    参数

    类型

    描述

    value

    Action[]

    用于包含一组@Action注解

    比如:

    public class HelloAction extends ActionSupport{

    @Actions({

       @Action(“/different/url”),

        @Action(“/another/url”)

    })

    public String method1(){……}

    }

    /different/url!method1.action或/another/url!method1.action来调用method1方法。

     

    public class HelloAction extends ActionSupport {  

      @Action("/another/url")  

      public String method1() {...} 

    }

    我们调用method1方法可以通过两种方式:
    1  /hello!method1.action 
    2 /another/url!method1.action
    可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。总之,一个方法被@Action或@Actions注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。

     

    7.@InterceptorRef及@InterceptorRefs

         可以使用@InterceptorRef来为一个action定义一个拦截器,注意此注解只是在此action上引用拦截器,此拦截器需要在struts.xml中进行定义。此注解可以加在类,InterceptorRefs等上面 。 @DefaultInterceptorRef可以合定义一个默认的拦截器.

    参数

    类型

    描述

    value

    String

    用于指定拦截器的名字

    params

    String[]

    用于指定传递的参数

    如:

    @InterceptorRefs({

         @InterceptorRef("mydefaultStack")

     })

        public class HelloWorld extends ActionSupport {  

             public String method2() { ...}

        }

        public class HelloWorld extends ActionSupport {  

           @Action(“url”,interceptorRefs={

           @InterceptorRef{“mystack”},

           @InterceptorRef(“logincheck”)

            })  

          public String method2() { ...}  

    }

     

    使用注解配置Action注意事项

       在Struts2中使用注解配置action,有一些需要注意的地方。

    1. 与action配置相关的4个注解只能用于类级别
    2. 需要在web.xml文件中为FilterDispatcher过滤器设置actionPackages参数  指定使用注解配置的Action所在包的列表,多个包之间以逗号(,)分隔。这些包和它们的子包都将被扫描,定包中的所有实现了Action接口的类或是以”Action”结尾的类都将被检测。所以后者允许我们编写POJO Action类,而不需要实现Action接口。如果有多个包,则以逗 号(,)分隔。

          即:

    <filter-name>struts</filter-name>

                <filter-class>

                    org.apache.struts2.dispatcher.FilterDispatcher

                </filter-class>

                <init-param>

                        <!--  //固定格式-->

                 <param-name>actionPackages</param-name>

                    com.struts2.action1,com.struts.action2-->

                 <param-value>com.struts2.action</param-value>

               </init-param>

        </filter><!--  action所在的包,如果在多个包中,用逗号隔开- ->

     

    1. 包名,名称空间和action名的产生方式如下:

    l  Action类所在的包作为action包名

    l  除了使用Namespace注解指定名称空间外,也可以通过子包名来产生名称空间。如:action所在的包为cn.com.test.action,如果 参数指定的包是cn.com.test,那么“/action”将作为名称空间的名字。

    l  通过action的类名来产生action的名字。如类名以“Action”结尾,那么“Action”将被去掉,其余部分将首字母变小写作为action的名字。如:

    RegisterAction 将将会产生的action的名字是register。

    1. 如果想让包继承struts-default包,无须使用ParentPackage注解。在框架内部,会自动让你的包继承struts-default包。

     

     

     

    @Namespace@Action配合使用注意事项

    如果在@Action注解中包括”/”字符地,则意味着该注释覆盖了默认的namespace(这里是’/’),此时访问action就有一些小的改动。如下例:

    @Namespace("/other")  

    public class HelloWorld extends ActionSupport {  

      public String method1() {...}  

        @Action("url")  

      public String method2() { ...}  

        @Action("/different/url")  

      public String method3() {... }  

    }

    通过 /other/hello-world!method1.action 访问method1 方法
        通过 /other/url!method2.action 访问method2 方法
        通过 /different /url!method3.action 访问method3 方法 
        与@Action 注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello!method1.action 已经不能访问method1 了。

     

     

    ——————————————————————————————————
    傲轩游戏网
  • 相关阅读:
    <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析
    <WP7>(二)手把手教你写天气预报程序:UserControl的运用
    <cocos2dx for wp7>使用cocos2dx和BOX2D来制作一个BreakOut(打砖块)游戏(一)
    <WP7>(一)手把手教你写天气预报程序:序言
    <cocos2dx for wp7>使用cocos2dx和BOX2D来制作一个BreakOut(打砖块)游戏(二)
    <cocos2dx for wp7>使用box2d来做碰撞检测(且仅用来做碰撞检测)
    JQuery的开发与使用心得
    babylonjs
    [算法简结]动态规划(一):承
    centos7相关操作
  • 原文地址:https://www.cnblogs.com/cuizhf/p/3079360.html
Copyright © 2011-2022 走看看