想飞就别怕摔

大爷的并TM骂人

struts2学习笔记(二)--Conversion-plugin实现零配饰

首先感谢http://www.cnblogs.com/MoShin/archive/2011/04/06/2006591.html给的这么详细的讲解。
如何使用Convention(约定)将struts-Convention-plugin-2.1.6.jar文件复制到WEB-INF/lib路径下

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

 则将路径配置到了WEB-INF/page 下。

 

2. 默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。

(A)Convention插件会把如下两种java类当成Action处理:
    1) 所有实现了com.opensymphony.xwork2.Action的java类。
    2) 所有类名以Action结尾的java类

(B)你也可以通过设置struts.convention.package.locators属性来修改这个配置。如:

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

则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。
Com.ustb.web.*/com.ustb.action.*都将被视为含有Action的包路径而被搜索。
struts.Convention.exclude.packges:指定不扫描哪些包下的java类,位于这些包结构下的java类将不会自动映射成Action;
(C)struts.convention.action.packages:Convention插件以该常量指定包作为根包来搜索Action类。Convention插件除了扫描action,actions,struts,struts2四个包的类以外,还会扫描该常量指定的一个  或多个包,Convention会试图从中发现Action类。
(D)注意:struts.convention.package.locators和struts.convention.action.packages两个常量的作用比较微妙,开发者在利用这两个常量时务必小心。
    如:下面Action所在包被映射的命名空间如下:
    com.fun.actions.LoginAction 映射到 /
    com.fun.actions.myoffice.CarInfoAction 映射到 /myoffice
    com.fun.struts.myoffice.EntINfoAction 映射到 /myofiice
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 

 

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="-" />
    还是举个例子:
    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格式的文件。
下面是action和结果模版的映射关系:
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

 

 

 

 

 

 

 

 

 

 

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

 1 import com.opensymphony.xwork2.Action;
 2 import com.opensymphony.xwork2.ActionSupport;
 3 
 4 public class HelloAction extends ActionSupport {
 5     @Action("action1")
 6     public String method1() {
 7         return SUCCESS;
 8     }
 9 
10     @Action("/user/action2")
11     public String method2() {
12         return SUCCESS;
13     }
14 }

方法名 默认调用路径 默认映射路径
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 import com.opensymphony.xwork2.ActionSupport;   
 2 import org.apache.struts2.convention.annotation.Action;  
 3 import org.apache.struts2.convention.annotation.Actions;  
 4  
 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 }
 
我们可以通过:/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 import com.opensymphony.xwork2.ActionSupport;   
 2 import org.apache.struts2.convention.annotation.Action;  
 3 import org.apache.struts2.convention.annotation.Actions;  
 4    
 5 public class HelloAction extends ActionSupport {  
 6    @Action("/another/url")  
 7    public String method1() {  
 8      return “error”;  
 9    }  
10 }
我们调用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 import com.opensymphony.xwork2.ActionSupport;
 2 import org.apache.struts2.convention.annotation.Action;
 3 import org.apache.struts2.convention.annotation.Actions;
 4 
 5 @Namespace("/other")
 6 public class HelloWorld extends ActionSupport {
 7 
 8     public String method1() {  
 9         return “error”;  
10     }    
11     @Action("url")  
12     public String method2() {  
13         return “error”;  
14     }    
15     @Action("/different/url")  
16     public String method3() {  
17          return “error”;  
18     }    
19 }
通过 /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 import com.opensymphony.xwork2.ActionSupport;   
 2 import org.apache.struts2.convention.annotation.Action;  
 3 import org.apache.struts2.convention.annotation.Actions;  
 4 import org.apache.struts2.convention.annotation.Result;  
 5 import org.apache.struts2.convention.annotation.Results;  
 6      
 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 }
当我们访问 /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    
 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   
 8 public class HelloWorld extends ActionSupport {  
 9    @Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})  
10    public String method1() {  
11      return “error”;  
12    }  
13 }
 
当我们调用 /hello -world !method1.action 时,返回 /WEB-INF/content/hello-error.jsp
当我们调用 /other/bar!method1.action 时,返回 www.baidu.com

posted on 2011-12-10 12:52 生命的绽放 阅读(1365) 评论(0)  编辑  收藏 所属分类: Struts2.0


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜