Struts2注解详细说明文档.doc
Struts2 Convention Plugin中文文档(一)Introduction从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。 包命名习惯来指定Action位置 命名习惯制定结果(支持JSP,FreeMarker等)路径 类名到URL的约定转换 包名到命名空间(namespace)的约定转换 遵循SEO规范的链接地址(即:使用my-action 来替代 MyAction) 基于注解的Action名 基于注解的拦截机(Interceptor) 基于注解的命名空间(Nameespace) 基于注解的XWork包 默认action以及默认的结果(比如:/products 将会尝试寻找com.example.actions.Products 或 com.example.actions.products.Index进行处理)无需配置Convention即可使用Convention,Convention的某些约束习惯可以通过配置属性来控制,您也可以在类中覆写其中的方法来达到扩展目地。安装使用Convention插件,你需要将其JAR文件放到你应用的WEB-INF/lib目录中,你也可以在你Macen项目的POM文件中添加下面包依赖 1. <dependency> 2. <groupId>org.apache.struts</groupId> 3. <artifactId>struts2-convention-plugin</artifactId> 4. <version>2.1.6</version> 5. </dependency> 转换基于Codebehind项目到Convention跳转到此页面,查看需要修改的变化和小提示如果你想在你系统中结合Convention插件使用REST。需要在你项目的struts.xml中添加如下配置 1. <constant name="struts.convention.action.suffix" value="Controller"/> 2. <constant name="struts.convention.action.mapAllMatches" value="true"/> 3. <constant name="struts.convention.default.parent.package" value="rest-default"/> Hello world到目前为止,你已经在你项目中添加了Convention插件支持,首先我们从一个非常简单的例子开始入手。本例中,我们将演示根据访问URL来访问固定的Action,默认情况下,Convention会默认所有的结果页面都存储在WEB-INF/content下,你也可以在struts的properties文件中设定struts.convention.result.path的值到一个新的路径。路径最后“/”是不必要的,Convention会自动进行处理。以下是本例的JSP文件WEB-INF/content/hello-world.jsp 1. <html> 2. <body> 3. Hello world! 4. </body> 5. </html>启动Tomcat或其他你所使用的JEE容器,在浏览器访问http:/localhost:8080/hello-world , 你可看到以下信息:Hello world!这表明,Convention已经能正常运行,并找到了结果。即使在没有action存在情况下,convention也会根据URL规则来找到结果页面。Code behind hello world我们继续扩展本例并添加代码实现类。为了实现本功能,首先需要Convention能正确找到我们的Action 类,默认情况下,Convention会找到com.opensymphony.xwork2.Action 的实现类,或制定包中以Action 结尾的类 actionConvention使用以下方法来搜索类路径,首先,Convention会从根package中寻找包名含有struts, struts2, action or actions 的任意packages。下一部,Convention从前一步找到的package以及其子package中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类,下面为Convention寻找的类 1. com.example.actions.MainAction 2. com.example.actions.products.Display (implements com.opensymphony.xwork2.Action) 3. pany.details.ShowCompanyDetailsAction 4. com.example.actions.MainAction5. com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)6. pany.details.ShowCompanyDetailsAction每一个被Convention找到action都会对应一个明确的URL地址,URL以package的名字以及Action类名为基础。首先Convention从根package以及类所在的package名来确定对应的URL中的路径(namespace),以下就是根据package确定的URL namespace 1. com.example.actions.MainAction -> / 2. com.example.actions.products.Display -> /products 3. pany.details.ShowCompanyDetailsAction -> /company/details接下来Convention需要确定URL的具体资源部分。第一步取消类名中的Action,并以”-”来分割类名的其他部分,且将每个分部的首字母转为小写。如下所示 1. com.example.actions.MainAction -> /main 2. com.example.actions.products.Display -> /products/display 3. pany.details.ShowCompanyDetailsAction -> /company/details/show-company-details你也可以通过配置struts.convention.exclude.packages 来告诉Convention忽略某些包,也可以设置struts.convention.package.locators 用来更改Convention默认的根packages,最后你还可以设置 struts.convention.action.packages. 来让Convention只搜索特定package下的Action以下就是action类的实现代码: 1. package com.example.actions; 2. 3. import com.opensymphony.xwork2.ActionSupport; 4. 5. public class HelloWorld extends ActionSupport 6. private String message; 7. 8. public String getMessage() 9. return message; 10. 11. 12. public String execute() 13. message = "Hello World!" 14. return SUCCESS; 15. 16. 编译以上代码,并将其class放到 WEB-INF/classes中,Convention将会将 /hello-world 映射到这个Action. 部署上面的类以后,我们在JSP文件中添加打印message的语句,具体代码如下: 1. <html> 2. <body> 3. The message is $message 4. </body> 5. </html> 启动应用服务器,在浏览器访问 http:/localhost:8080/hello-world 地址,我们看到如下结果界面:The message is Hello World!原文:http:/cwiki.apache.org/WW/convention-plugin.html翻译:石太祥Struts2 Convention Plugin中文文档(二Results and result codesStruts启动后,Convention将预设好应用中的所有的action,默认情况下,配置将包含在你应用中能找到的所有JSP文件。同时您也可在Action代码中设置与习惯不同的结果页面。通常Action方法返回一个字符串,通过返回的字符串找到结果页面,而使用Convention允许你在action代码中指定和返回字符串不同的结果页面。编译下面的例子。我们希望在action中返回zero 而不是success,第一步,我们更新action类,返回zero。 1. package com.example.actions; 2. 3. import com.opensymphony.xwork2.ActionSupport; 4. 5. public class HelloWorld extends ActionSupport 6. private String message; 7. 8. public String getMessage() 9. return message; 10. 11. 12. public String execute() 13. if (System.currentTimeMillis() % 2 = 0) 14. message = "It's 0" 15. return "zero" 16. 17. 18. message = "It's 1" 19. return SUCCESS; 20. 21. 接下来,我们添加一个新的JSP页面 WEB-INF/content/hello-world-zero.jsp 。需要注意的是,文件名的第一部分和action名是对应的,后面的部分和action返回的字符串对应。这就是convention确定具体使用那个页面来渲染结果。下面是修改后的JSP代码: 1. <html> 2. <body> 3. The error message is $message 4. </body> 5. </html> 现在,你可以编辑你的程序,重启应用,刷新页面,根据当前时间不通,会看到不通的渲染结果页面结果页面的类型会自动匹配文件,支持的渲染页面的格式为:jsp.ftl,vm,html,htm.下面是actiong和结果模版的映射关系:URLResultFile that could matchResult Type/hellosuccess/WEB-INF/content/hello.jspDispatcher/hellosuccess/WEB-INF/content/hello-success.htmDispatcher/hellosuccess/WEB-INF/content/hello.ftlFreeMarker/hello-worldinput/WEB-INF/content/hello-world-input.vmVelocity/test1/test2/helloerror/WEB-INF/content/test/test2/hello-error.htmlDispatcher Action链如果在一个action结果中调用另外一个action ,他们俩将被链接到一起,如果在第一个action代码中未定义result,如下代码: 1. package com.example.actions; 2. 3. import com.opensymphony.xwork2.Action; 4. import com.opensymphony.xwork2.ActionSupport; 5. 6. public class HelloAction extends ActionSupport 7. Action("foo") 8. public String foo() 9. return "bar" 10. 11. 12. Action("foo-bar") 13. public String bar() 14. return SUCCESS; 15. 16. “foo”action执行时候,由于找不到结果,convention尝试在同一个包下寻找action名为“foo-bar”的action。如果找到这样的action,convention将会调用并返回相关联的result。XWork packages为了避免冲突,可将action放在一个自定义XWORK的package下。package命名由action所在的Java包,action对应的URL中namespace部分以及action的parent XWork package三个部分组成。parent XWork package 值在属性 struts.convention.default.parent.package 中指定(默认为conventionDefault),package的属性值须继承于 strutsDefault因此,Convention插件中XWORK packages 采用如下命名规则: 1. <java-package>#<namespace>#<parent-package> Using our example from above, the XWork package for our action would be:上例中,action对应的 XWORK package如下: 1. com.example.actions#/#conventionDefaultStruts2 Convention Plugin中文文档(三)Annotation 参考Convention使用某些注解语句来覆写插件默认的action到url的映射和自动搜索渲染到的页面。此外,你还可以修改action配置文件中定义的父XWORK的包信息Action annotationConvention 插件可以使用Action注解语句来修改action返回的URL地址。本注解同时也允许包含在Actions语句中,用来使一个action对应于多个URL。在action 方法中使用本注解语句,可以参考以下代码: 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. 现在我们action类中将使用 /different/url 来替代默认的 /hello-world,如果未指定Result(参考下节),result的路径将会使用action的namespace,上面的例子中将会返回一下路径 "/WEB-INF/content/different/url.jsp"。Action类中的单个方法可以使用Actions 注解来映射多个地址。 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 或 Actions 的使用方法是,在单个action类中定义多个action方法,每个方法对应一个不同的地址。下面是多个action方法的范例: 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. Action("url") 14. public String doSomething() 15. return SUCCESS; 16. 17. 前面的例子中,第二个URL地址是不推荐的,上面url将使用java 包名作为namespace,而不会直接使用Action注解的地址。Interceptor 和 interceptor stacks 同样可以使用interceptorRefs 注解来指定。下例演示了在action中同时添加"validation"和"defaultStack"拦截器。 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. 可以通过params属性来将参数传递给结果。属性的值是一个偶数个元素的String的数组,由形如"key0", "value0, "key1", "value1" . "keyN", "valueN"所组成,举个例子: 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. 如果未指定interceptors,将会使用默认的。InterceptorRef annotationInterceptors 可以在方法级进行指定,使用Action 注解或在类上使用InterceptorRefs注解。Class级别的拦截会被应用到类包含的所有action上。可以参考下面例子: 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. 下面的拦截机将会应用到“action1”中:"interceptor-1","defaultStack"中的所有拦截机, "validation""defaultStack"中的所有拦截机也会对”action2”生效Result annotationConvention 允许action类为每个action定义不同的results,results分为两类,全局的(global)和本地的(local),全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。本地results只能在action方法上进行声明。下面是两种results注解的例子: 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. 参数同样可以在results中通过params属性进行传递,和上面一样,由形如"key0", "value0, "key1", "value1" . "keyN", "valueN"所组成。可参考下例: 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. Namespace annotationnamespace注解允许action使用指定的路径替代默认的以package包名作为路径。本注解可以在action类或Java 包中的package-info.java类中进行设置。设置在action类中的namespace注解,对本action类中所有的action都有效,这是不完全合乎规范的action URL处理地址。设置在package-info.java中的namespace注解,将会改变本java包下所有的action的默认namespace。下面是此注解的例子: 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. 在上例中的action 会对2个不同的地址响应:/different/url 和 /custom/url下面是一个在package-info.java中使用namespace注解的例子: 1. org.apache.struts2.convention.annotation.Namespace("/custom") 2. package com.example.actions; 这会改变com.example.actions包下所有action的默认namespace。请注意一点,本注解不会应用到子一级的包中。ResultPath annotationResultPath 注解用来更改默认的results存储路径,注解可以放到action的类中,也可以放到package-info.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. 上面的result将以 WEB-INF/jsps 替换默认的 WEB-INF/contentParentPackage annotationParentPackage注解用来定义具体action类的父XWork包或java包,下面例子演示了在action类上使用本注解: 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.