最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置。
配置文件精简了,的确是简便了开发过程,但是,我们熟悉的配置突然disappear了,真是一下很不适应。跟着潮流走吧,看看该怎样来搞定convention-plugin。
使用Convention插件,你需要将其JAR文件放到你应用的WEB-INF/lib目录中,你也可以在你Maven项目的POM文件中添加下面包依赖

  1. <dependency>
  2. <groupId>org.apache.struts</groupId>
  3. <artifactId>struts2-convention-plugin</artifactId>
  4. <version>2.1.6</version>
  5. </dependency>
  1. <dependency>
  2. <groupId>org.apache.struts</groupId>
  3. <artifactId>struts2-convention-plugin</artifactId>
  4. <version>2.1.6</version>
  5. </dependency>

零配置并不是没有配置,而是通过约定大于配置的方式,大量通过约定来调度页面的跳转而使得配置大大减少。所以,首先应该了解下convention-plugin的约定:
1. 默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如:

  1. <constant name="struts.convention.result.path" value="/WEB-INF/page" />
  1. <constant name="struts.convention.result.path" value="/WEB-INF/page" />

则将路径配置到了WEB-INF/page 下。
2. 默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如:

  1. <constant name="struts.convention.package.locators" value="web,action" />
  1. <constant name="struts.convention.package.locators" value="web,action" />

则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。
Com.ustb.web.*/com.ustb.action.*都将被视为含有Action的包路径而被搜索。
3. 接着,Convention从前一步找到的package以及其子package中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:

  1. com.example.actions.MainAction
  2. com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)
  3. com.example.struts.company.details.ShowCompanyDetailsAction
  1. com.example.actions.MainAction
  2. com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)
  3. com.example.struts.company.details.ShowCompanyDetailsAction

4. 命名空间。从定义的.package.locators标示开始到包结束的部分,就是命名空间。举个例子:
Com.ustb.web.user.userAction的命名空间是:”/user”。Com.ustb.web.user.detail.UserAction的命名空间是:”/user/detail”
5. Convention通过如下规则确定URL的具体资源部分:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割,你可以设置struts.convention.action.name.separator 如

  1. <constant name="struts.convention.action.name.separator" value="-" />
  1. <constant name="struts.convention.action.name.separator" value="-" />

还是举个例子:
UserAction->user  UserDetailAction ->user-detail。结合上面的。对于com.ustb.web.user.detail.UserDetailAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp
6. struts支持.jsp .html .htm .vm格式的文件。
下面是actiong和结果模版的映射关系:

URL Result
File that could match Result Type
/hello success /WEB-INF/content/hello.jsp Dispatcher
/hello success /WEB-INF/content/hello-success.htm Dispatcher
/hello success /WEB-INF/content/hello.ftl FreeMarker
/hello-world input /WEB-INF/content/hello-world-input.vm Velocity
/test1/test2/hello error /WEB-INF/content/test/test2/hello-error.html Dispatcher

以上的内容来自struts2的文档http://struts.apache.org/2.1.6/docs/convention-plugin.html

当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的。
通过@Action注释
对如下例子:

  1. package com.example.web;
  2. import com.opensymphony.xwork2.Action;
  3. import com.opensymphony.xwork2.ActionSupport;
  4. public class HelloAction extends ActionSupport {
  5. @Action("action1")
  6. public String method1() {
  7. return SUCCESS;
  8. }
  9. @Action("/user/action2")
  10. public String method2() {
  11. return SUCCESS;
  12. }
  13. }
  1. package com.example.web;
  2.  
  3. import com.opensymphony.xwork2.Action;
  4. import com.opensymphony.xwork2.ActionSupport;
  5.  
  6. public class HelloAction extends ActionSupport {
  7. @Action("action1")
  8. public String method1() {
  9. return SUCCESS;
  10. }
  11.  
  12. @Action("/user/action2")
  13. public String method2() {
  14. return SUCCESS;
  15. }
  16. }
方法名 默认调用路径 默认映射路径
method1 /hello!method1.action . /WEB-INF/content/hello.jsp
method2 /hello!method2.action. /WEB-INF/content/hello.jsp

通过@Action注释后

方法名 @Action注释后调用路径 @Action注释 后映射路径
method1 /action1!method1.action. /WEB-INF/content/action1.jsp
method1 /user/action2!method2.action /WEB-INF/content/user/action2.jsp

通过@Actions注释

  1. package com.example.web;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. public class HelloAction extends ActionSupport {
  6. @Actions({
  7. @Action("/different/url"),
  8. @Action("/another/url")
  9. })
  10. public String method1() {
  11. return “error”;
  12. }
  1. package com.example.web;
  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 HelloAction extends ActionSupport {
  8. @Actions({
  9. @Action("/different/url"),
  10. @Action("/another/url")
  11. })
  12. public String method1() {
  13. return error”;
  14. }

我们可以通过:/different/url!method1.action /another/url!method1.action 来调用method1 方法。
对应的映射路径分别是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp

可能误导了大家,一个方法被@Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。比如对于如下例子:

  1. package com.example.web;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. public class HelloAction extends ActionSupport {
  6. @Action("/another/url")
  7. public String method1() {
  8. return “error”;
  9. }
  1. package com.example.web;
  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 HelloAction extends ActionSupport {
  8. @Action("/another/url")
  9. public String method1() {
  10. return error”;
  11. }

我们调用method1方法可以通过两种方式:
/hello!method1.action 映射 url:/WEB-INF/content/hello-error.jsp
2 /another/url!method1.action 映射 url:/WEB-INF/content/another/url-error.jsp
可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。

通过@Namespace 注释

  1. package com.example.web;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. @Namespace("/other")
  6. public class HelloWorld extends ActionSupport {
  7. public String method1() {
  8. return “error”;
  9. }
  10. @Action("url")
  11. public String method2() {
  12. return “error”;
  13. }
  14. @Action("/different/url")
  15. public String method3() {
  16. return “error”;
  17. }
  18. }
  1. package com.example.web;
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;
  4. import org.apache.struts2.convention.annotation.Action;
  5. import org.apache.struts2.convention.annotation.Actions;
  6. @Namespace("/other")
  7. public class HelloWorld extends ActionSupport {
  8.  
  9. public String method1() {
  10. return error”;
  11. }
  12. @Action("url")
  13. public String method2() {
  14. return error”;
  15. }
  16.  
  17. @Action("/different/url")
  18. public String method3() {
  19. return error”;
  20. }
  21. }

通过 /other/hello-world!method1.action 访问method1 方法。
通过 /other/url!method2.action 访问method2 方法
通过 /different /url!method3.action 访问method3 方法
与@Action 注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello!method1.action 已经不能访问method1 了.
@Results和@Result
1 全局的(global)。
全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。

  1. package com.example.actions;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. import org.apache.struts2.convention.annotation.Result;
  6. import org.apache.struts2.convention.annotation.Results;
  7. @Results({
  8. @Result(name="failure", location="/WEB-INF/fail.jsp")
  9. })
  10. public class HelloWorld extends ActionSupport {
  11. public String method1() {
  12. return “failure”;
  13. }
  14. @Action("/different/url")
  15. public String method2() {
  16. return “failure”;
  17. }
  18. }
  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="/WEB-INF/fail.jsp")
  11. })
  12. public class HelloWorld extends ActionSupport {
  13. public String method1() {
  14. return failure”;
  15. }
  16. @Action("/different/url")
  17. public String method2() {
  18. return failure”;
  19. }
  20.  
  21. }

当我们访问 /hello -world !method1.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /hello -world !method2.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /different/url!method2.action 时,返回 /WEB-INF/fail.jsp

2 本地的(local)。
本地results只能在action方法上进行声明。

  1. package com.example.actions;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. import org.apache.struts2.convention.annotation.Result;
  6. import org.apache.struts2.convention.annotation.Results;
  7. public class HelloWorld extends ActionSupport {
  8. @Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})
  9. public String method1() {
  10. return “error”;
  11. }
  12. }
  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="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})
  11. public String method1() {
  12. return error”;
  13. }
  14. }

当我们调用 /hello -world !method1.action 时,返回 /WEB-INF/content/hello-error.jsp
当我们调用 /other/bar!method1.action 时,返回 www.baidu.com

struts2的零配置的更多相关文章

  1. Struts2的零配置和rest插件

    1. 零配置使用struts2-convention-plugin-2.3.16.jar,rest使用struts2-rest-plugin-2.3.16.jar 1.1 Struts2的conven ...

  2. Struts2 注解零配置方法(convention插件使用)

    最近接触到一个新的项目,是做一个使用S2SH的电子商务商城的二次开发.之前使用过S2SH,在此之前的项目中,Struts2 使用的是XML配置而这个项目是使用注解.在这个项目中,注解还不需要使用Act ...

  3. Struts2 Convention Plugin ( struts2 零配置 )

    Struts2 Convention Plugin ( struts2 零配置 ) convention-plugin 可以用来实现 struts2 的零配置.零配置的意思并不是说没有配置,而是通过约 ...

  4. 菜鸟学Struts2——零配置(Convention )

    又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...

  5. 13、零配置Struts2开发

    Convention 插件 从 Struts 2.1 开始, Struts 可以使用 Convention 插件来支持零配置: Convention 插件完全抛弃配置信息, 不仅不需要使用 strut ...

  6. Struts2零配置介绍(约定访问)

    从struts2.1开始,struts2 引入了Convention插件来支持零配置,使用约定无需struts.xml或者Annotation配置 需要 如下四个JAR包 插件会自动搜索如下类 act ...

  7. struts2 零配置

    一.新建一个web项目,命名为:struts2 二.导入strut2所需的jar包 所需jar下载:http://pan.baidu.com/s/1dDxP4Z3 三.配置struts2的启动文件,在 ...

  8. 从struts2.1开始Convention零配置

    从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...

  9. struts2 convention-plugin实现零配置

    零配置并不是没有配置,而是通过约定大于配置的方式,大量通过约定来调度页面的跳转而使得配置大大减少.使得Action等配置不必写在Struts.xml中. convention-plugin的约定 1. ...

随机推荐

  1. SQL Developer 警告--无法安装某些模块

    第一次启动正常,第二次启动就开始报错,说无法安装某写模块.具体如下: 警告--无法安装某些模块(Warning - could not install some modules) 在oracle官方的 ...

  2. vim学习日志(5):vim下wimrc的配置,解决中文乱码问题

    解决linux下vim乱码的情况:(修改vimrc的内容) 全局的情况下:即所有用户都能用这个配置 文件地址:/etc/vimrc 在文件中添加: ,ucs-bom,gb18030,gbk,gb231 ...

  3. ny225 小明求素数积

    小明求素数积时间限制:1000 ms  |  内存限制:65535 KB 难度:1描述 小明最近遇到了一个素数题,是给你一个正整数N(2=<N<=1000)让你求出2~N的所有素数乘积的后 ...

  4. ny220 推桌子

    推桌子 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 The famous ACM (Advanced Computer Maker) Company has rent ...

  5. maven引入ojdbc

    http://blog.csdn.net/linminqin/article/details/17922413 ******************************************** ...

  6. xml相关术语说明

    <project xmlns="http://maven.apache.org/POM/4.0.0"  --命名空间,类似包名,因为xml的标签可自定义,需要命名空间来区分x ...

  7. jQuery-File-Upload文件上传

    http://blueimp.github.io/jQuery-File-Upload/index.html

  8. mysql主从复制的介绍

    引用:https://my.oschina.net/u/255939/blog/505598 MySQL复制就是一台MySQL服务器(slave)从另一台MySQL服务器(master)进行日志的复制 ...

  9. VBA学习笔记(3)--文件夹操作

    说明(2017.3.22): 1. 根据兰色幻想VBA80集视频教学,总结 2. 大部分可以用自带函数处理,不过复制文件夹需要用到FileSystemObject对象,打开文件夹用到shell Pub ...

  10. C 程序的存储空间记录

    一直以来,我们只是单纯的去运行执行 C 程序,并没有关心这个可执行文件里面包含着什么东西. 参考UNIX 环境高级编程 7.6,记录C程序的存储空间布局. C程序由 正文段,初始化数据段,非初始化数据 ...