以前使用Struts2的时候参数都是在struts.xml里面配置的,现在转入了一个新的项目中,发现这个项目struts.xml中只定义了几个常量,并没有大量的action、interceptor的配置信息,项目显得非常整洁,但是同时也看的云里雾里。今天花了一小会看了一个Struts2 Convention Plugin的官方文档,才大致了解了一二,这里简单叙述一下。
具体的阐述请参考官网
http://struts.apache.org/2.1.6/docs/convention-plugin.html。Convention Plugin是从2.1版本开始引进的。2.1以前的版本请参考
http://struts.apache.org/2.0.14/docs/zero-configuration.html。不同的版本大家再到官网查看一下吧。呵呵
下面是常用的常量
name | default value | description |
struts.convention.result.path | /WEB-INF/content/ | Directory where templates are located |
struts.convention.result.flatLayout | true | If set to false, the result can be put in its own directory: resultsRoot/namespace/actionName/result.extension |
struts.convention.package.locators | action,actions,struts,struts2 | Packages whose name end with one of these strings will be scanned for actions |
struts.convention.exclude.packages | org.apache.struts.*,org.apache.struts2.* | Packages excluded from the action scanning |
struts.convention.package.locators.basePackage | | If set, only packages that start with its value will be scanned for actions |
下面是步骤:
1,首先需要将架包(struts2-convention-plugin-xxx.jar)导入工程中(如果将action打包在了jar包中,那么属性struts.convention.action.disableJarScanning需要设置为true)。
2,跳转路径是根据请求路径的url处理的,即使没有请求对应的action,但是WEB-INF目录下有对应的页面,也可以跳转到页面上去。例如我们有页面WEB-INF/content/hello-world.jsp,如果我们请求http://localhost:8080/hello-world,即使没有HelloWorldAction,那么我们仍然能跳转到上面的欢迎页面,这是因为Convention plugin获取跳转结果只是根据Struts获取的URL,而不是action中配置的跳转路径。
下面是Annotation的分类:
1,Action annotation。
最简单的例子
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
public class HelloWorld extends ActionSupport {
@Action("/different/url")
public String execute() {
return SUCCESS;
}
}
那么这个HelloWorld的访问url就变为了/different/url。
一个方法可以被映射到多个url上面,如下所示,方位注解中的两个url都可以访问这个方法
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
public class HelloWorld extends ActionSupport {
@Actions({
@Action("/different/url"),
@Action("/another/url")
})
public String execute() {
return SUCCESS;
}
}
如果一个action中有多个方法,那么可以分别为各个方法指定访问url
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
public class HelloWorld extends ActionSupport {
@Action("/different/url")
public String execute() {
return SUCCESS;
}
@Action("url")
public String doSomething() {
return SUCCESS;
}
}
请注意上面这个类的第二个方法doSomething(),它的url是“url”,这是个相对路径是,也就是说访问这个方法时的正确路径是namespace+url。而execute()通过访问/different/url就可以访问。
使用@Action的interceptorRefs 属性可以指定action或者方法的interceptor,如下面的例子
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
public class HelloWorld extends ActionSupport {
@Action(interceptorRefs={@InterceptorRef("validation"), @InterceptorRef("defaultStack")})
public String execute() {
return SUCCESS;
}
@Action("url")
public String doSomething() {
return SUCCESS;
}
}
上面的action中execute()方法应用了validation拦截器和defaultStack拦截器栈。
还可以使用params属性指定要传给拦截器的参数。形式为{键,值,键,值…………},键值总是会成对出现,如下面的例子
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
public class HelloWorld extends ActionSupport {
@Action(interceptorRefs=@InterceptorRef(value="validation",params={"programmatic", "false", "declarative", "true}))
public String execute() {
return SUCCESS;
}
@Action("url")
public String doSomething() {
return SUCCESS;
}
}
如果Action没有显式的指定拦截器的话,默认的拦截器会应用在这个Action上。
2,Interceptor Annotation。
拦截器可以在类和方法的层面上应用。在方法层面指定拦截器使用@Action注解,在类层面指定拦截器使用@InterceptorRefs注解。类层面引用的拦截器会应用在所有的方法上,如下面的例子
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
@InterceptorRefs({
@InterceptorRef("interceptor-1"),
@InterceptorRef("defaultStack")
})
public class HelloWorld extends ActionSupport {
@Action(value="action1", interceptorRefs=@InterceptorRef("validation"))
public String execute() {
return SUCCESS;
}
@Action(value="action2")
public String doSomething() {
return SUCCESS;
}
}
如上代码所示,execute()方法应用了interceptor-1,validation和defaultStack中的所有拦截器;而doSomething()方法则没有validation拦截器。
3,Result Annotation。
Convention plugin允许为一个Action设置多个跳转路径,使用@Result注解标识。@Result可以已经用在Action上,可以应用在方法上,应用在Action上作为全局路径,应用在Method上那么只对当前的Method起作用。如下面的例子
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
@Results({
@Result(name="failure", location="fail.jsp")
})
public class HelloWorld extends ActionSupport {
@Action(value="/different/url",
results={@Result(name="success", location="http://struts.apache.org", type="redirect")}
)
public String execute() {
return SUCCESS;
}
@Action("/another/url")
public String doSomething() {
return SUCCESS;
}
}
同@InterceptorRef注解,@Result注解同样可以使用params属性设置参数,实例如下
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
public class HelloWorld extends ActionSupport {
@Action(value="/different/url",
results={@Result(name="success", type="httpheader", params={"status", "500", "errorMessage", "Internal Error"})}
)
public String execute() {
return SUCCESS;
}
@Action("/another/url")
public String doSomething() {
return SUCCESS;
}
}
由于篇幅太长,其他的注解下一篇文章
http://lichuanbao.iteye.com/admin/blogs/1095151再介绍。
分享到:
相关推荐
struts2 annotation 注解的详细介绍及使用方法
本文档对利用struts2 注解 annotation 实现文件下载作了简单介绍,并有代码为例,希望对学习struts2注解annotation的人有帮助,特别是需要动态传参方面。
参考博文:http://blog.csdn.net/u011638419/article/details/41743049
而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行配置,甚至不需要使用Annotation进行配置,而是由struts2根据约定...
而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行配置,甚至不需要使用Annotation进行配置,而是由struts2根据约定...
超级详细的SSH2项目实例详解,并且附带两个项目详解。两种注解实现方式。...在JavaEE企业级开发中,以SSH2框架为核心的应用非常广,大象根据项目实践经验,通过二个实例,详细的为大家讲解如何实现全注解式的开发。
SSH2 annotation 实现struts2.1.6 spring2.5.6 hibernate3.3 全注解开发 hibernate延迟加载_懒加载 具体应用
基于注解Annotation的最新版SSH(Struts2.3.7+Hibernate4.1.9+Spring3.2.0)整合开发,真正实现零配置。 最新版本Struts、Spring、Hibernate框架整合: struts-2.3.7 spring-framework-3.2.0.RELEASE hibernate-...
struts2,json-plugin, 并全注解零配置
Spring3Annotation+Struts2Annotation+Hibernate3Annotation 全注解集成开发,操作Emp表
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
用注解(annotation)方式配置struts2 (webwork) 中action的拦截器(interceptor)
全注解,演示Struts2 + Spring + MyBatis整合,开发环境为Eclipse,数据库为mysql。
springmvc注解.doc,Spring注解讲解.doc,struts2标签详解.pdf,struts2常用标签.pdf,Struts2页面开发中常用标签.pdf,Struts2注解详细说明文档.doc,Hibernate注解教程:Hibernate Annotation使用实例.maff
使用Maven3.0.5, Struts2, Spring, Hibernate3, annotation实现零配置的开发框架.里面有详细的开发指导文档, 后台用googlecode的 hibernate-generic-dao - Generic DAO 和 Search的代码,完全进行封装. 由于Generic ...
NULL 博文链接:https://vincenta.iteye.com/blog/1535330
1、shiro+strut2 2、shiro注解 + struts2注解 2、shiro 缓存:因为使用了缓存,第一使用admin登录,如果没有logout,再用test登录,则test具有admin的权限
7.开启struts2自带的开发模式常量 6 8.vo传参模式 7 9.ModerDriven传参模式(不建议采用) 7 10.为什么要使用struts2代替struts1.x 7 二、struts.xml配置及例程 7 1.配置文件的优先级 7 2.配置形式 8 3.package配置...
基于Struts2.18+Spring2.5+Hibernater3.3+Annotation注解开发的电子商务网站demo
struts2+hibernate+spring无配置架构