`

struts2 注解

阅读更多

一、配置web.xml 
<filter> 
<filter-name>struts2</filter-name> 
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
<init-param> 
<param-name>actionPackages</param-name> 
<param-value>com.test.action</param-value> 
</init-param> 
</filter> 

<filter-mapping> 
<filter-name>struts2</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 

二、加入注解 
@Namespace(value="/test") 
@Action(value = "file-manager", 
interceptorRefs = { 
@InterceptorRef(value = "fileUpload",params={"maximumSize","1024000","allowedTypes","image/pjpeg"}), 
@InterceptorRef(value = "basicStack")}, 
results = {@Result(name = ActionSupport.SUCCESS, location = "/view/file-manager-sucess.jsp"), 
@Result(name = ActionSupport.ERROR, location = "/view/file-manager-error.jsp") }, 
exceptionMappings = {@ExceptionMapping(exception = "java.lang.Exception", result = ActionSupport.ERROR)} 


验证注解: 
@Validations( 
requiredStrings={ 
@RequiredStringValidator(fieldName="username",message="用户名不能为空!"), 
@RequiredStringValidator(fieldName="telNum",message="电话号码不能为空!") 
}, 
regexFields={@RegexFieldValidator(fieldName="telNum",expression="^(//+86|0|1)//d{10,11}$", 
message="电话号码格式不正确!")} 


跳过验证注解: 
@SkipValidation 

三、Convention的Annotation 
1)与Action相关的两个Annotation是@Action 和@Actions 
2)@Action中可指定一个value属性。类似于指定<action name=””/>属性值 
3)@Action中还可以指定一个params属性,该属性是一个字符串数组,用于该Acion指定的参数名和参数值。params属性应遵守如下格式:{“name1”,”value1”,”name2”,”value2”} 
4)@Actions 也用于修饰Action类里的方法,用于将该方法映射到多个URL.@Actions用于组织多个@Action.因此它可将一个方法映射成多个逻辑Action。 

四、与Result配置相关的Annotation 
1)@ResultPath @Result 和Results 
2)@Results用于组织多个@Result因此它只需指定一个value属性值,该value属性值为多个@Result 
3)@Result相当于struts.xml文件中的<result/>元素。使用@Result必须指定一个name属性,相当于<result name=””/>另外,它还有几个可选的属性。 
     ☆ type 相当于<result type=””/>指定返回视图资源的类型 
     ☆ location 相当于<result>…..</result>中间部分,用于指定实际视图位置 
     ☆ params:该属性相当于<result/>元素里多个<param../>子元素的作用,用于为该Result指定参数值。该属性应满足{“name1”,”value1”,”name2”,”value2”}格式 
4)@Result有以下两种用法 
1.Action级的Result映射:以@Actions组合多个@Action后修饰的Action类。这种Result映射对该Action里的所有方法都有效。 
2.方法级Result映射:将多个@Result组成数组后作为@Action的results属性值。这种Result映射仅对被修饰的方法有效。 
5)@ResultPath则用于修饰包和Action类,用于改变被修饰Action所对应的物理视图资源的根路径。举例说:默认情况下,Convention都会到WEB-INF/content路径下找物理视图资源,一旦我们使用@ResultPath("/view")修饰该Action,系统将回到view目录下寻找物理视图资源。 

五、与包和命名空间相关的Annotation: 
@Namespace:修饰Action类或其所在的包。该Annotation中指定一个value属性值,用于指定被修饰的Action所在的命名空间 
@Namespaces:修饰Action类或其所在的包,用于组合多个@Namespace 
@ParentPackage: 用于指定被修饰的Action所在包的父包。 

六、与异常处理相关的Annotation 
@ExceptionMappings 用于组织多个@ExceptionMapping,因此它只需指定一个value属性值,该value属性值为多个@ExceptionMapping。 
@ExceptionMapping 用于定义异常类和物理视图之间的对应关系,也相当于struts.xml文件里<exception-mapping../>元素的作用 使用时,必须注意以下两个属性: 
exception: 用于指定异常类 
result:用于指定逻辑视图 
@ExceptionMpping有如下两种用法 
Action级的异常定义:以@ExceptionMappings组合多个@ExceptionMapping后修饰的Action类。这种异常定义对Action中的所有方法有效 
方法级的异常定义:将多个@ExceptionMapping组成数组后作为@Action的exceptionMappings属性值,这种异常定义仅对被修饰的方法有效。 

七、与拦截器配置相关的Annotation 
与拦截器配置的Annotation有@InterceptorRef、@InterceptorRefs和@DefaultInterceptorRef 
@InterceptorRefs用于组织多个@InterceptorRef,因此它只需要指定一个value属性值,该value属性值为多个@InterceptorRef 
@InterceptorRef用于为指定Action引用lanjieq或者是拦截器栈。也就相当于strut.xml中位于<action../>元素内部的<interceptor-ref../>子元素的作用。使用@InterceptorRefAnnotation时,必须制定一个value属性,用于指定所引用的拦截器或拦截器栈的名字。相当于<interceptor-ref../>子元素里name属性的作用。 

八、查看struts2配置 
为了看到struts2应用里的Action等各种资源的影射情况,struts2提供了Config Browser插件。 
使用方法:将struts2-config-browser-plugin-2.1.6.jar文件复制到struts2应用的WEB-INF/lib目录中。 
打开首页地址:http://localhost:8080/应用名字/config-browser/actionNames.action 这里可以看到Config Browser插件的首页。 


多语言切换: 
ActionContext.getContext().getSession().put("WW_TRANS_I18N_LOCALE", locale); locale为:国家语言,例如:Locale locale = new Locale("zh","CN"); 

 

 

==================

 

转载于:http://ajava.org/course/open/16827.html

 

 

在Action中提供了下列几种注解:

ParentPackage

Namespace

Result

Results

注解
 描述
 
Namespace
 所期望的命名空间(在“struts.xml”文件中也有定义)的字符串值
 
ParentPackage
 所期望的父package的字符串值
 
Results
 “Result”注解列表
 
Result
 提供了Action结果的映射,它有四个属性:

²        name ——action方法的结果名

²        type—— 结果类型

²        value——任意的结果值。可以是rediect结果类型对应的action名,也可以是dispatcher结果类型对应的JSP

²        parameters ——字符串参数组成的数组
 


使用方式如下:

  1. @ParentPackage(value="struts-default")   
  2.   
  3. @Namespace(value="/test")   
  4.   
  5. @Result(name="success",value="/userSuc.jsp")   
  6.   
  7. @Results({   
  8.   
  9.        @Result(name="success",value="/userSuc.jsp",type=org.apache.struts2.dispatcher.ServletRedirectResult.class),   
  10.   
  11.        @Result(name="input",value="/main.jsp",type=org.apache.struts2.dispatcher.ServletRedirectResult.class)   
  12.   
  13. })   

在使用这些注解的时候,还需要进行额外的一些配置。在web.xml的filter配置中,需要指定哪些package是使用了注解的。配置如下所示,其中参数名必须为“actionPackages”,参数的值就是package的名称列表。

  1. <filter>  
  2.   
  3.        <filter-name>struts</filter-name>  
  4.   
  5.               <filter-class>  
  6.   
  7.                      org.apache.struts2.dispatcher.FilterDispatcher   
  8.   
  9.               </filter-class>  
  10.   
  11.        <init-param>  
  12.   
  13.               <param-name>actionPackages</param-name>  
  14.   
  15.               <param-value>user.actions,other.actions</param-value>  
  16.   
  17.        </init-param>  
  18.   
  19. </filter>  
  20.   

被配置过的每一个package和它的子package都会被扫描到,看其中哪些类实现了Action或者类名以“Action”结尾,然后注解就会被加入到运行时配置中去。如果没有使用namespace注解的话,那么命名空间就会由package名来生成。把“actionPackages”配置值中使用的package名称截掉,就得到了命名空间。也就是说,如果某个被配置好的action的名字是“actions.admin.user.AddAction”,而“actionPackages”的值为“actions”,那么这个action的命名空间就是“/admin/user”。

分享到:
评论
1 楼 lianlupengUestc 2011-12-03  
From my testing (Struts2 version 2.1.6 and 2.1.8), this is not true, no matter what you put in the “param-value” or “struts.convention.action.packages“, Struts 2 will just ignore it and scan the specified folders named struts, struts2, action or actions only.

Here’s how the scanning work

Scan the annotated classes which located at the packaged named “struts, struts2, action or actions“.
Next, scan the file which match either of the following criteria :
Implemented the com.opensymphony.xwork2.Action interface.
Extends the com.opensymphony.xwork2.ActionSupport class.
File name ends with Action (e.g UserAction, LoginAction).



See this [url]Struts 2 convention plugin documentation.[/url]


我也是刚接触,你看看这篇文章

相关推荐

    struts2注解与拦截器demo

    该例子为struts2注解与拦截器demo,利用myEclipse8.5开发,导入刚才后,自动加载所需struts2的jar包,可以直接运行,是初学struts2注解、拦截器很好的例子,保证10分钟学会2种技术,愿意分享给大家。

    Struts2注解开发jar

    Struts2注解开发jar,必须导入struts2-convention-plugin-2.3.15.jar包,它在struts2安装包下lib目录中。

    Maven实现struts2注解

    Maven实现struts2注解

    Struts2注解详细说明文档

    Struts2注解详细说明文档,详细讲述struts2的注解使用

    struts2注解详解

    从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...

    struts2注解登陆

    注解实现的页面跳转其特点是不用配置文件struts.xml因而可以实现零配置,同时由于有注解的功能,所以说大大增加了程序的可维护性。

    Struts2 注解 Demo

    Struts2Demo 注解的小例子,jar包全包括

    struts2注解小实例

    这是我自己写的一个struts注解的小实例,非常的实用,大家一看就懂的

    struts2注解必须包

    struts2-convention-plugin-2.1.8.1.jar

    struts2注解配置Action及拦截器几种不同方式写法对应的路径指向.docx

    struts2注解配置Action及拦截器几种不同方式写法对应的路径指向.docx

    Struts2框架及注释和用法

    Struts2的框架及注释和使用法,希望大家支持,我们一起努力,谢谢!

    struts2注解配置

    struts2 注解 配置 资料 最近学习struts2的注解配置,从网上找的一些资料,分享给大家。

    struts2利用注解annotation实现文件下载

    本文档对利用struts2 注解 annotation 实现文件下载作了简单介绍,并有代码为例,希望对学习struts2注解annotation的人有帮助,特别是需要动态传参方面。

    struts2注解配置全面解析

    很多人在学习struts2注解的时候,都被它莫名其妙的错误搞的郁闷,而网上关于这方面的东西大多都是基于struts2.0版本的,对我们现在用的2.1以上的版本不起什么作用,所以特整理出一份文档,里面详细说明了怎样用注解...

    Struts2注解配置教程

    《Struts2注解配置》 ——系列精品教程

    spring和Struts2注解文档

    spring和struts2注解讲解说明和使用

    Struts 2使用注解配置Action

    Struts 2使用注解配置Action,不配置struts.xml,通过注解直接配置action

    struts2注释

    struts2注释,关于struts2注释的详细的介绍,非常的使用,有助于web开发初学者了解struts2,会有很大的帮助

    Struts2注解使用说明文档

    从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...

    Struts2+spring注解配置

    Struts2+spring注解配置 很好哦

Global site tag (gtag.js) - Google Analytics