struts2采用convention-plugin实现零配置,Struts2 Convention插件的使用

使用Convention插件,你需要将其JAR文件放到你应用的WEB-INF/lib目录中,你也可以在你Maven项目的POM文件中添加下面包依赖

    <dependency>  
      <groupId>org.apache.struts</groupId>  
      <artifactId>struts2-convention-plugin</artifactId>  
      <version>2.1.6</version>  
    </dependency>  

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

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

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

    <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结尾的类

    <constant name="struts.convention.action.suffix" value="Action"/>

<!-- 用于配置类名后缀,默认为Action,设置后,Struts2只会去找这种后缀名的类做映射 -->

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 如


    <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


<!-- 设置即使没有@Action注释,依然创建Action映射。默认值是false。因为Convention-Plugin是约定优于配置的风格,         可以不通过注解根据预先的定义就能访问相应Action中的方法 --> 

  1. <constant name="struts.convention.action.mapAllMatches" value="true"/> 


6. struts支持.jsp .html .htm .vm格式的文件。
下面是actiong和结果模版的映射关系:

URLResult
File 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



常量说明

      struts.convention.result.path="/WEB-INF/content/": 结果页面存放的根路径,必须以 "/" 开头。
      struts.convention.action.suffix="Action": action名字的获取 
      struts.convention.action.name.lowercase="true": 是否将Action类转换成小写
      struts.convention.action.name.separator="-": 
      struts.convention.action.disableScanning="false": 是否不扫描类。
      struts.convention.default.parent.package="convention-default":设置默认的父包。
      struts.convention.package.locators="action,actions,struts,struts2": 确定搜索包的路径。
      struts.convention.package.locators.disable="false": 
      struts.convention.package.locators.basePackage="": 
      struts.convention.exclude.packages="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*": 排除哪些包不搜索。
      struts.convention.relative.result.types="dispatcher,velocity,freemarker": 默认返回的结果类型搜索。
      struts.convention.result.flatLayout="true": 是否结果类型作为文件名的一部分。假如结果返回值为error,则true时为hello_world_error.jsp,false时为hello_world/error.jsp。
      struts.convention.classes.reload="false" : 
  
      struts.convention.action.mapAllMatches="false": 
      struts.convention.action.checkImplementsAction="true":
      struts.mapper.alwaysSelectFullNamespace="true":   
      struts.convention.redirect.to.slash="true": 
      struts.convention.action.alwaysMapExecute="true":
      struts.convention.action.fileProtocols="jar" :

 

默认约定

      Action名的映射:去掉Action后缀,单词之间加中画线,单词首字母变小写。比如TestFormAction类对应的Action名为test-form。

 

      Action类的包路径转为其命名空间路径,命名空间路径转为URL路径。比如com.cjm.action.card.sim.SimCardInputAction,则其命名空间为/card/sim,Action名为sim-card-input。


      Action类的包路径转为结果页面的存储目录,查找结果页面的约定:
            找action名-result名.jsp,找不到
            找action名.jsp,找不到
            找action名-result名.action

 


注解


@Action/@Actions:

@Action指定一个类为action,对应配置文件中的....标签,其中可以配置如下属性

results:配置返回的结果集属性,相当于struts2中的列表,可以在{}中配置属性,具体如下value:配置action的名字,相当于中的name属性interceptorRefs:配置拦截器 @Action可以定义在类上,也可以定义在方法上 如下(@Result的作用后面讲,也可以和后面的配合着看)
@Action(value = "testAction",results = {@Result(name="success",location="/success.jsp")})
public class testAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}
这就相当于如下的xml配置
	/success.jsp
在xml配置中如果name不写,那么默认就是success,在注解中也是,如果results中的name不写,那么默认就是success
也可以使用@Actions来指定多个action映射,这样可以做到一个类对应多个地址映射,如下
@Actions({
	@Action(value = "testAction",results = {@Result(location="/success.jsp")}),
	@Action(value = "testAction2",results = {@Result(location="/success.jsp")})
})
public class testAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}
这是使用/testAction或者/testAction2都可以跳转到success.jsp上,因为配置了两个action映射

在xml配置中,我们有如下的配置方法
	/{1}.jsp
这是xml配置中的通配符方式,即当我们以add来访问action时,将会进到action的add方法进行处理,当返回add时会跳转到add.jsp页面 在注解中没有通配符可以使用,但是也可以实现类似的效果,这时@Action就要写在方法上了,就像下面这样

public class testAction extends ActionSupport {
	@Action(value = "add",results = {@Result(name="add",location="/add.jsp")})
	public String add() throws Exception {
		return "add";
	}
	@Action(value = "delete",results = {@Result(name="delete",location="/delete.jsp")})
	public String delete() throws Exception {
		return "delete";
	}
}

@Result/@Results:

@Result配置具体返回结果,在results中使用,也可以单独在类上使用,有如下属性

name:对应中的name属性location:对应间的地址type:对应的type属性 @Result可以在类上声明,也可以和Action配置声明,如果在类上声明,那么就是全局的结果,如下

@Result(name="delete",location = "/delete.jsp")
public class testAction extends ActionSupport {
	@Action(value = "add", results = { @Result(name = "add", location = "/add.jsp") })
	public String add() throws Exception {
		return "add";
	}

	@Action(value = "delete")
	public String delete() throws Exception {
		return "delete";
	}
}
虽然delete方法没有指定返回delete时要跳转到哪个页面页面,但是在类上用@Result声明了,那么就会找到类上面的这个@Result,然后跳转到delete.jsp页面

@Results是用来声明多个结果集,用法和@Actions类似,这里就不再详述

@ParentPackage("admin-login")
@Results({ @Result(name = "input", location = "activity/list.jsp", type = "dispatcher"),
                @Result(name = "toAdd", location = "activity/add.jsp", type = "dispatcher"),
                @Result(name = "toEdit", location = "activity/edit.jsp", type = "dispatcher"),
                @Result(name = "detail", location = "activity/detail.jsp", type = "dispatcher"),
                @Result(name = "redirectIndex", location = "admin_mall_activity", type = "redirectAction"),
                @Result(name = "orderList", location = "activity/orderList.jsp", type = "dispatcher"),
                @Result(name = "couponList", location = "activity/couponList.jsp", type = "dispatcher")
})



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你是我的天晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值