- 浏览: 7250039 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1546)
- 企业中间件 (236)
- 企业应用面临的问题 (236)
- 小布Oracle学习笔记汇总 (36)
- Spring 开发应用 (54)
- IBatis开发应用 (16)
- Oracle基础学习 (23)
- struts2.0 (41)
- JVM&ClassLoader&GC (16)
- JQuery的开发应用 (17)
- WebService的开发应用 (21)
- Java&Socket (44)
- 开源组件的应用 (254)
- 常用Javascript的开发应用 (28)
- J2EE开发技术指南 (163)
- EJB3开发应用 (11)
- GIS&Mobile&MAP (36)
- SWT-GEF-RCP (52)
- 算法&数据结构 (6)
- Apache开源组件研究 (62)
- Hibernate 学习应用 (57)
- java并发编程 (59)
- MySQL&Mongodb&MS/SQL (15)
- Oracle数据库实验室 (55)
- 搜索引擎的开发应用 (34)
- 软件工程师笔试经典 (14)
- 其他杂项 (10)
- AndroidPn& MQTT&C2DM&推技术 (29)
- ActiveMQ学习和研究 (38)
- Google技术应用开发和API分析 (11)
- flex的学习总结 (59)
- 项目中一点总结 (20)
- java疑惑 java面向对象编程 (28)
- Android 开发学习 (133)
- linux和UNIX的总结 (37)
- Titanium学习总结 (20)
- JQueryMobile学习总结 (34)
- Phonegap学习总结 (32)
- HTML5学习总结 (41)
- JeeCMS研究和理解分析 (9)
最新评论
-
lgh1992314:
[u][i][b][flash=200,200][url][i ...
看看mybatis 源代码 -
尼古拉斯.fwp:
图片根本就不出来好吧。。。。。。
Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传 -
ln94223:
第一个应该用排它网关吧 怎么是并行网关, 并行网关是所有exe ...
工作流Activiti的学习总结(八)Activiti自动执行的应用 -
ZY199266:
获取不到任何消息信息,请问这是什么原因呢?
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息 -
xiaoyao霄:
DestinationSourceMonitor 报错 应该导 ...
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息
最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置。
配置文件精简了,的确是简便了开发过程,但是,我们熟悉的配置突然disappear了,真是一下很不适应。跟着潮流走吧,看看该怎样来搞定convention-plugin。
使用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结尾的类:
com.example.actions.MainAction com.example.actions.products.Display (implements com.opensymphony.xwork2.Action) 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 如
<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注释
对如下例子:
package com.example.web; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport { @Action("action1") public String method1() { return SUCCESS; } @Action("/user/action2") public String method2() { return SUCCESS; } }
方法名 | 默认调用路径 | 默认映射路径 |
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注释
package com.example.web; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; public class HelloAction extends ActionSupport { @Actions({ @Action("/different/url"), @Action("/another/url") }) public String method1() { return “error”; }
我们可以通过:/different/url!method1.action 或 /another/url!method1.action 来调用method1 方法。
对应的映射路径分别是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp
可能误导了大家,一个方法被@Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。比如对于如下例子:
-
com.example.web; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; public class HelloAction extends ActionSupport { @Action("/another/url") public String method1() { return “error”; }
我们调用method1方法可以通过两种方式:
1 /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 注释
package com.example.web; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; @Namespace("/other") public class HelloWorld extends ActionSupport { public String method1() { return “error”; } @Action("url") public String method2() { return “error”; } @Action("/different/url") public String method3() { return “error”; } }
通过 /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类上使用注解进行声明。
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="/WEB-INF/fail.jsp") }) public class HelloWorld extends ActionSupport { public String method1() { return “failure”; } @Action("/different/url") public String method2() { return “failure”; } }
当我们访问 /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方法上进行声明。
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="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")}) public String method1() { return “error”; } }
当我们调用 /hello -world !method1.action 时,返回 /WEB-INF/content/hello-error.jsp
当我们调用 /other/bar!method1.action 时,返回 www.baidu.com
评论
多谢大家捧场。
发表评论
-
整合Struts2+JasperReport Web报表应用示例
2013-04-22 13:56 2244整合Struts2+JasperReport Web报表应用 ... -
Struts2+JFreeChart制作图标
2011-11-26 13:52 2043前言 关于Struts2入门以及提高等在这里就不介绍了 ... -
Struts2.0中获取项目的上下文的两种方式
2010-01-19 13:42 3240Struts2.0中获取项目的上下文的两种方式 方 ... -
Struts2.0中通过OGNL访问常量必须注意的一个配置
2010-01-19 13:34 2592在项目的开始阶段,由于没有学习Struts2.0中OGNL的 ... -
Struts2.0 中值栈的实现以及解析OGNL以及值堆栈的原理
2010-01-14 13:19 4625OGNL的值栈实现的堆栈动态OGNL的表达式。何时设置 ... -
关于Struts2.0 标签中采用%{}%的处理原理
2010-01-14 13:08 2658项目中采用struts2.0 ... -
struts 2配置文件解说
2010-01-13 13:09 1369struts 2框架有两个核心配置文件: struts.xml ... -
struts2.0的ognl表达式 研究
2010-01-13 13:05 2611OGNL —— 完美的催化剂 为了解决数据从 ... -
Struts2和OGNL
2010-01-13 10:57 2012OGNL是XWork引入的一个非常有效的数据处 ... -
Struts2参数传递
2010-01-13 10:45 2394本篇主要通过实例来讲述Struts2中各种各样的参数传递。这个 ... -
Struts2.0的不同版本针对枚举转换器应用
2010-01-13 10:39 2806Struts2.0 的Apache项目中从Strut ... -
Struts2.0 中支持的基本数据类的转换的类XWorkBasicConverter的分析
2010-01-13 10:25 4539项目中Struts2.1.6不支持基本数据类型的自动转换, ... -
Struts2.0加載配置順序
2009-12-28 13:33 2472Struts2.0默認的加载顺序: Constants ... -
Struts2.0的IOC方式注入SessionAware, CookiesAware
2009-12-10 19:59 3955在Action实现相应的 private Map<S ... -
Struts2工作原理和框架扩展点
2009-12-08 18:49 3827上图来源于Struts2官方站点,是Struts ... -
Struts2.0 和JQuery實現AjaX的調用功能
2009-11-27 12:47 2321开发过程忽略: 重点注意点解释以下: (1)将Ac ... -
在Struts2.0中批量操作(update Or Add)的實現和注意點
2009-11-15 16:41 2997在項目中,需要針對一批数据进行或添加或着修改操作。使用S ... -
在Struts2.0 中将Enum对象与组件的绑定
2009-08-19 13:05 4118在项目使用一个下拉框绑定或单选或复选框的绑定的应用: ... -
struts2 标签针对字符串截取的处理
2009-06-18 13:16 6151struts2 标签截取字符串的功能 <td> ... -
Struts2中 用OGNL表达式
2009-06-18 12:40 3434具体记录如下:<s:set ...
相关推荐
struts2采用convention-plugin实现零配置,希望大家用的到!
Struts采用convention-plugin实现零配置
struts2-convention-plugin-2.3.32
struts2-convention-plugin-2.3.24.1
struts2-convention-plugin-2.3.15.1.jar
struts2-convention-plugin-2.1.6.jar
Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置,使用Convention插件,你需要此JAR文件
struts2-convention-plugin-2.1.8.jar
struts2-convention-plugin-2.3.1.jar,使用注解的方式代替xml配置action,必须要引用这个包。
struts2-convention-plugin-2.3.15.3.jar struts2配置Action注解要用到的包,真正实现零配置
struts2-convention-plugin-2.3.1.2.jar
struts2-convention-plugin-2.3.24.jar
不论高低版本,要使用struts2-core这个jar包,当又需struts2-convention-plugin.jar时勿必要使两者版本一致哦,否则会有DefError、Unable to read class诸等错误
struts2-convention-plugin-2.3.4.1.jar
struts2-convention-plugin-2.1.8.1.jar
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
struts2-convention-plugin.pdf