---
title: "struts2注解"
author: "dingqinghu"
date: "2011-03-24T23:58:00Z"
tags: ["Apache", "JSP", "Struts", "XML", "freemarker"]
slug: "struts2"
draft: false
lastmod: "2011-03-24T23:58:00Z"
id: 982284
url: "https://dingqinghu.iteye.com/blog/982284"
---

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

&lt;filter-mapping&gt;  
&lt;filter-name&gt;struts2&lt;/filter-name&gt;  
&lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
&lt;/filter-mapping&gt;

二、加入注解  
@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属性。类似于指定&lt;action name=””/&gt;属性值  
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文件中的&lt;result/&gt;元素。使用@Result必须指定一个name属性，相当于&lt;result name=””/&gt;另外，它还有几个可选的属性。  
☆ type 相当于&lt;result type=””/&gt;指定返回视图资源的类型  
☆ location 相当于&lt;result&gt;…..&lt;/result&gt;中间部分，用于指定实际视图位置  
☆ params：该属性相当于&lt;result/&gt;元素里多个&lt;param../&gt;子元素的作用，用于为该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文件里&lt;exception-mapping../&gt;元素的作用 使用时，必须注意以下两个属性：  
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中位于&lt;action../&gt;元素内部的&lt;interceptor-ref../&gt;子元素的作用。使用@InterceptorRefAnnotation时，必须制定一个value属性，用于指定所引用的拦截器或拦截器栈的名字。相当于&lt;interceptor-ref../&gt;子元素里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插件的首页。

**struts2注解实例:**

使用注解完成一个Action的流程必须要如下的7个jar包  
1.commons-fileupload-1.2.1.jar  
2.commons-io-1.3.2.jar  
3.freemarker-2.3.15.jar  
4.ognl-2.7.3.jar  
5.struts2-convention-plugin-2.1.8.1.jar  
6.struts2-core-2.1.8.1.jar  
7.xwork-core-2.1.6.jar

如下用户登录的Action通过注解的方式验证通过: