Struts2中的零配置

Struts2中的零配置

二、零配置
首先要澄清一点,这里说的零配置并不是一点配置都没有,只是说配置很少而已。
Struts2(我只用过Struts 2.0.6和2.0.9,不清楚其它版本是否支持零配置)引入了零配置的新特性,元数据可以通过规则和注解来表达:A "Zero Configuration" Struts application or plugin uses no additional XML or properties files. Metadata is expressed through convention and annotation.
目前,这个新特性还在测试阶段,但经过一段时间的使用,我觉得这个特性已经可用。下面我讲一下如何使用它。
1. Actions的定位
以前需要在xml配置文件中配置Action的name和class,如果使用零配置,所带来的一个问题就是如何定位这些Action。我们需要在 web.xml中找到struts2的filter的配置,增加一个名为actionPackages的init-param,它的值是一个以逗号分隔的 Java包名列表,比如:demo.actions1,demo.actions2。struts2将会扫描这些包(包括这些包下边的子包),在这些包下,所有实现了Action接口的或者是类名以“Action”结尾的类都会被检查到,并被当做Action。
以前,我们写Action必须要实现Action接口或者继承ActionSupport。但是,上面提到的类名以"Action"结尾的类并不需要这样做,它可以是一个POJO,Struts2支持POJO Action!
下面是actionPackages的配置示例:
xml 代码
<filter>  
  <filter-name>struts2</filter-name>  
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  <init-param>  
    <param-name>actionPackages</param-name>  
    <param-value>demo.actions1,demo.actions2</param-value>  
  </init-param>  
</filter>  

2. 示例
现在我们建立demo.actions1.app.person和demo.actions2.app.group两个包,在 demo.actions1.app.person包下建立ListPeopleAction.java,在 demo.actions2.app.group下建立ListGroupAction.java。作为示例,这两个类只是包含一个execute方法,返回"success"或"error",其它什么都不做:
java 代码
public String execute() {  
    return "success";  
}  

在Filter的配置中,我指定actionPackages为demo.actions1,demo.actions2,当系统启动时,Struts2 就会在这两个包下扫描到demo.actions1.app.person.ListPeopleAction和 demo.actions2.app.group.ListGroupAction。

3. Action and Package name
Struts2扫描到Action后,从actionPackages指定的包开始,子包名会成为这个Action的namespace,而Action 的name则由这个Action的类名决定。将类名首字母小写,如果类名以Action结尾,则去掉"Action"后缀,形成的名字就是这个 Action的名字。在如上所述的示例中,actionPackages指定为demo.actions1,demo.actions2,那么你可以这样访问demo.actions1.app.person.ListPeopleAction:
                http://localhost:8080/app/person/listPeople

4. Results
Struts2是通过"Result"和"Results"两个类级别的annotations来指定Results的。
作为示例,我们在webapp目录下建两个html文件:success.html和error.html,随便写点什么内容都可以。现在假设我们访问 /app/person/listPeople时,或Action返回success就转到success.html页面,若是error就转到 error.html页面,这只需要在ListPeopleAction类上加上一段注解就可以了:
java 代码
@Results({  
    @Result(name="success", type=NullResult.class, value = "/success.html", params = {}),  
    @Result(name="error", type=NullResult.class, value = "/error.html", params = {})  
})  
public class ListPeopleAction {  
    public String execute() {  
        return "success";  
     }  
}  

同上,我们给ListGroupAction也加上注解。
现在,我们已经完成了一个零配置的示例。我们并没有在xml文件里配置ListPeopleAction和ListGroupAction,但它们已经可以工作了!
用Eclipse运行RunJetty,然后用浏览器访问http://localhost:8080/app/person/listPeoplehttp://localhost:8080/app/group/listGroup看看,是不是正是success.html(或 error.html)的内容?

5. Namespaces
如上所述,namespace由包名所形成,但我们可以使用"Namespace"注解来自己指定namespace。

6. Parent Package
这个配置用得较少。Struts2提供一个"ParentPackage"注解来标识Action应该是属于哪个package。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值