【转】Struts2零配置开发(注解Annotation的使用)

以前使用Struts2的时候参数都是在struts.xml里面配置的,现在转入了一个新的项目中,发现这个项目struts.xml中只定义了几个常量,并没有大量的action、interceptor的配置信息,项目显得非常整洁,但是同时也看的云里雾里。今天花了一小会看了一个Struts2 Convention Plugin的官方文档,才大致了解了一二,这里简单叙述一下。 

    具体的阐述请参考官网http://struts.apache.org/2.1.6/docs/convention-plugin.html。Convention Plugin是从2.1版本开始引进的。2.1以前的版本请参考http://struts.apache.org/2.0.14/docs/zero-configuration.html。不同的版本大家再到官网查看一下吧。呵呵 

下面是常用的常量 

name default value description
struts.convention.result.path /WEB-INF/content/ Directory where templates are located
struts.convention.result.flatLayout true If set to false, the result can be put in its own directory: resultsRoot/namespace/actionName/result.extension
struts.convention.package.locators action,actions,struts,struts2 Packages whose name end with one of these strings will be scanned for actions
struts.convention.exclude.packages org.apache.struts.*,org.apache.struts2.* Packages excluded from the action scanning
struts.convention.package.locators.basePackage   If set, only packages that start with its value will be scanned for actions



下面是步骤: 
1,首先需要将架包(struts2-convention-plugin-xxx.jar)导入工程中(如果将action打包在了jar包中,那么属性struts.convention.action.disableJarScanning需要设置为true)。 
2,跳转路径是根据请求路径的url处理的,即使没有请求对应的action,但是WEB-INF目录下有对应的页面,也可以跳转到页面上去。例如我们有页面WEB-INF/content/hello-world.jsp,如果我们请求http://localhost:8080/hello-world,即使没有HelloWorldAction,那么我们仍然能跳转到上面的欢迎页面,这是因为Convention plugin获取跳转结果只是根据Struts获取的URL,而不是action中配置的跳转路径。 

下面是Annotation的分类: 
1,Action annotation。 
最简单的例子 

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5.   
  6. public class HelloWorld extends ActionSupport {  
  7.   @Action("/different/url")  
  8.   public String execute() {  
  9.     return SUCCESS;  
  10.   }  
  11. }  

那么这个HelloWorld的访问url就变为了/different/url。 

一个方法可以被映射到多个url上面,如下所示,方位注解中的两个url都可以访问这个方法 

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Actions;  
  6.   
  7. public class HelloWorld extends ActionSupport {  
  8.   @Actions({  
  9.     @Action("/different/url"),  
  10.     @Action("/another/url")  
  11.   })  
  12.   public String execute() {  
  13.     return SUCCESS;  
  14.   }  
  15. }  


如果一个action中有多个方法,那么可以分别为各个方法指定访问url

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Actions;  
  6.   
  7. public class HelloWorld extends ActionSupport {  
  8.   @Action("/different/url")  
  9.   public String execute() {  
  10.     return SUCCESS;  
  11.   
  12.   }  
  13.   
  14.   @Action("url")  
  15.   public String doSomething() {  
  16.     return SUCCESS;  
  17.   }  
  18. }  

请注意上面这个类的第二个方法doSomething(),它的url是“url”,这是个相对路径是,也就是说访问这个方法时的正确路径是namespace+url。而execute()通过访问/different/url就可以访问。 

使用@Action的interceptorRefs 属性可以指定action或者方法的interceptor,如下面的例子

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Actions;  
  6.   
  7. public class HelloWorld extends ActionSupport {  
  8.   @Action(interceptorRefs={@InterceptorRef("validation"), @InterceptorRef("defaultStack")})  
  9.   public String execute() {  
  10.     return SUCCESS;  
  11.   }  
  12.   
  13.   @Action("url")  
  14.   public String doSomething() {  
  15.     return SUCCESS;  
  16.   }  
  17. }  

上面的action中execute()方法应用了validation拦截器和defaultStack拦截器栈。 

还可以使用params属性指定要传给拦截器的参数。形式为{键,值,键,值…………},键值总是会成对出现,如下面的例子

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Actions;  
  6.   
  7. public class HelloWorld extends ActionSupport {  
  8.   @Action(interceptorRefs=@InterceptorRef(value="validation",params={"programmatic""false""declarative", "true}))  
  9.   public String execute() {  
  10.     return SUCCESS;  
  11.   }  
  12.   
  13.   @Action("url")  
  14.   public String doSomething() {  
  15.     return SUCCESS;  
  16.   }  
  17. }  

如果Action没有显式的指定拦截器的话,默认的拦截器会应用在这个Action上。 

2,Interceptor Annotation。 
拦截器可以在类和方法的层面上应用。在方法层面指定拦截器使用@Action注解,在类层面指定拦截器使用@InterceptorRefs注解。类层面引用的拦截器会应用在所有的方法上,如下面的例子

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Actions;  
  6.   
  7. @InterceptorRefs({  
  8.     @InterceptorRef("interceptor-1"),  
  9.     @InterceptorRef("defaultStack")  
  10. })  
  11. public class HelloWorld extends ActionSupport {  
  12.   @Action(value="action1", interceptorRefs=@InterceptorRef("validation"))  
  13.   public String execute() {  
  14.     return SUCCESS;  
  15.   }  
  16.   
  17.   @Action(value="action2")  
  18.   public String doSomething() {  
  19.     return SUCCESS;  
  20.   }  
  21. }  

如上代码所示,execute()方法应用了interceptor-1,validation和defaultStack中的所有拦截器;而doSomething()方法则没有validation拦截器。 

3,Result Annotation。 
Convention plugin允许为一个Action设置多个跳转路径,使用@Result注解标识。@Result可以已经用在Action上,可以应用在方法上,应用在Action上作为全局路径,应用在Method上那么只对当前的Method起作用。如下面的例子

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Actions;  
  6. import org.apache.struts2.convention.annotation.Result;  
  7. import org.apache.struts2.convention.annotation.Results;  
  8.   
  9. @Results({  
  10.   @Result(name="failure", location="fail.jsp")  
  11. })  
  12. public class HelloWorld extends ActionSupport {  
  13.   @Action(value="/different/url",   
  14.     results={@Result(name="success", location="http://struts.apache.org", type="redirect")}  
  15.   )  
  16.   public String execute() {  
  17.     return SUCCESS;  
  18.   }  
  19.   
  20.   @Action("/another/url")  
  21.   public String doSomething() {  
  22.     return SUCCESS;  
  23.   }  
  24. }  

同@InterceptorRef注解,@Result注解同样可以使用params属性设置参数,实例如下

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Actions;  
  6. import org.apache.struts2.convention.annotation.Result;  
  7. import org.apache.struts2.convention.annotation.Results;  
  8.   
  9. public class HelloWorld extends ActionSupport {  
  10.   @Action(value="/different/url",   
  11.     results={@Result(name="success", type="httpheader", params={"status""500""errorMessage""Internal Error"})}  
  12.   )  
  13.   public String execute() {  
  14.     return SUCCESS;  
  15.   }  
  16.   
  17.   @Action("/another/url")  
  18.   public String doSomething() {  
  19.     return SUCCESS;  
  20.   }  
  21. }  


4,Namespace annotation 
可以定义在Action中或者是定义在package-info.java文件中。在Action中定义的@Namespace会应用在该Action中所有的相对url上。如果定义在package-info.java文件中,那么该包中的所有的Action的namespace(不包括子包中的action)。 
实例代码一:在Action中使用@Namespace注解。

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Namespace;  
  6.   
  7. @Namespace("/custom")  
  8. public class HelloWorld extends ActionSupport {  
  9.   @Action("/different/url")  
  10.   public String execute() {  
  11.     return SUCCESS;  
  12.   }  
  13.   
  14.   @Action("url")  
  15.   public String doSomething() {  
  16.     return SUCCESS;  
  17.   }  
  18. }  

如上代码,execute()访问的url为/different/url,doSomething()的访问url为/custom/url,这个路径前面追加了@Namespace。 

示例代码二:在package-info.java中配置@Namespace注解

Java代码   收藏代码
  1. @org.apache.struts2.convention.annotation.Namespace("/custom")  
  2. package com.example.actions;  

那么包com.example.actions中所有的action的Namespace都被指向了/custom。 

5,ResultPath annotation,用来改变结果页面所在的目录。如下例所示

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.ResultPath;  
  6.   
  7. @ResultPath("/WEB-INF/jsps")  
  8. public class HelloWorld extends ActionSupport {  
  9.   public String execute() {  
  10.     return SUCCESS;  
  11.   }  
  12. }  

该Action跳转的结果页面将指向/WEB-INF/jsps目录。 

6,ParentPackage annotation,用来改变特定的Action类或者是java包的parent XWork packages(不好意思,这几个单词怎么翻译觉得都不好,就直接引用了)。示例代码如下

Java代码   收藏代码
  1. package com.example.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.ParentPackage;  
  6.   
  7. @ParentPackage("customXWorkPackage")  
  8. public class HelloWorld extends ActionSupport {  
  9.   public String execute() {  
  10.     return SUCCESS;  
  11.   }  
  12. }  



7,ExceptionMapping Annotation,用来配置action出现异常时的跳转路径。该注解同样可以使用params属性配置要传入的参数。该注解可以配置在Action级别和Method级别,配置在Action级别对所有的Method都适用;配置在Method级别只对当前的Method使用。示例代码如下: 
在Action上配置ExceptionMapping Annotation:

Java代码   收藏代码
  1. @ExceptionMappings({  
  2.     @ExceptionMapping(exception = "java.lang.NullPointerException", result = "success", params = {"param1""val1"})  
  3. })  
  4. public class ExceptionsActionLevelAction {  
  5.   
  6.     public String execute() throws Exception {  
  7.         return null;  
  8.     }  
  9. }  

在Method上配置ExceptionMapping Annotation,使用params属性传入要设置的参数

Java代码   收藏代码
  1. public class ExceptionsMethodLevelAction {  
  2.     @Action(value = "exception1", exceptionMappings = {  
  3.             @ExceptionMapping(exception = "java.lang.NullPointerException", result = "success", params = {"param1""val1"})  
  4.     })  
  5.     public String run1() throws Exception {  
  6.         return null;  
  7.     }  
  8. }  



Convention Plugin在Action中的配置改变时,可以自动加载改变的内容而不用重新启动容器。这时需要在struts.xml文件中做如下设置

Java代码   收藏代码
  1. <constant name="struts.devMode" value="true"/>  
  2. <constant name="struts.convention.classes.reload" value="true" />  

但这种加载方式只是实验性的并且还没有在所有的容器中进行测试,因此官方不推荐使用。 

==========常见错误============ 
1,"There is no Action mapped for namespace /orders and action name view.",这说明URL"/orders/view.action"并没有被映射到任何一个action,这是你需要检查action的namespace或者action的名称是否正确。 
2,如果碰见如“"No result defined for action my.example.actions.orders.ViewAction and result success"的错误,说明action被映射到了正确的url上,但是action返回success时要跳转的页面并没有找到,请确保您的工程的正确目录中含有诸如/WEB-INF/content/orders/view-success.jsp的页面。 

============难点============= 
1,要确保Action的namespace要有对应的页面。定位器(action,actions,struts,strutss)后面的namespace将作为action的namespace,并且用来定位该action要跳转到的页面。例如包my.example.actions.orders中有个名为ViewAction的action,那么他会被映射到/orders/view.action,该Action对应的页面必须在目录/WEB-INF/content/orders下面,例如/WEB-INF/content/orders/view-success.jsp。 
2,关于Convention Plugin的日志输出。本插件在Debug模式下会输出大量的日志信息,因此不建议开启Debug模式。如果你是用的是JDK自带的logger,可以使用"trace"日志级别打印日志;如果使用的是Log4J,可以在配置文件中做如下配置

Java代码   收藏代码
  1. log4j.logger.org.apache.struts2.convention=DEBUG  

转自:http://blog.csdn.net/oathevil/article/details/7084979

猜你喜欢

转载自mib168.iteye.com/blog/2009672