Struts2的零配置和rest插件
1. 零配置使用struts2-convention-plugin-2.3.16.jar,rest使用struts2-rest-plugin-2.3.16.jar
1.1 Struts2的convention插件的主要特点是“约定优于配置”,对于Struts2而言,它会自动在你创建的action、actions、struts、struts2这四个包下自动搜索,只要实现了com.opensymphony.xwork2.Action接口的类或者是类名以“Action”结尾的类,Struts2就会认为包里的类是Action类。
当Struts2按约定找到了这些符合条件的类以后,就会自动部署这些Action,但在不同的包结构下,访问这些Action的URL也是不同的,请看下面的表格举例:
包 | 访问URL |
org.crazyit.actions.LoginAction | 映射到/ |
com.test.action.abc.UserAction | 映射到/abc |
org.crazyit.struts2.wage.hr.AddEmployeeAction | 映射到/wage/hr |
org.crazyit.struts.auction.bid.BidAction | 映射到/auction/bid |
而访问Action的名字,也应遵循两个规则,第一:如果类名包含Action后缀,那么把Action后缀去掉;第二:将以骆驼命名法的类名转成中画线写法,所有的字母都小写,单词之间用中画线分割。比如:
类名 | 映射 |
LoginAction | /login.action |
GetBooks | /get-books.action |
AddEmployeeAction | /add-employee.action |
Action处理用户请求之后都会返回一个字符串作为逻辑视图,该逻辑视图必须映射到实际的物理视图。Convention插件默认也为作为逻辑视图和物理视图之间的映射提供了约定。默认情况下,Convention插件总会到WEB-INF/content路径下定位物理资源,定位资源的约定是:actionName+result+suffix.当某个逻辑后视图找不到对应的物理视图时,Convention插件会自动试图使用actionName+suffix作为物理资源。 例如:zj.qiao.actions.user.LoginAction类,返回success字符串时,Convention将优先考虑使用WEB-INF/content/user/login-success.jsp作为视图资源,如果找不到,WEB-INF/content/user/login.jsp也可作为对应的视图资源。
1.2 REST插件可以让Struts2实现RESTful风格的URL访问资源方式,其实Struts2本质上是一个MVC框架,而REST插件是将原本的URL转换成RESTful风格的URL而已, REST插件中RestActionMapper负责接收参数,把HTTP的请求方式分别用7个方法来做出处理:
HTTP 方法 |
URI |
调用的action方法 |
参数 |
---|---|---|---|
GET |
/movie |
index |
|
POST |
/movie |
create |
|
PUT |
/movie/Thrillers |
update |
id="Thrillers" |
DELETE |
/movie/Thrillers |
destroy |
id="Thrillers" |
GET |
/movie/Thrillers |
show |
id="Thrillers" |
GET |
/movie/Thrillers/edit |
edit |
id="Thrillers" |
GET |
/movie/new |
editNew |
使用了REST插件之后,Action类就不使用execute()方法来处理用户请求了,而是上面的7个方法来实现
注意:如果浏览器不支持DELETE和PUT操作,需要在表单中多传一个值来模拟这种方式,在你的<form>标记中加入一个隐藏域:<input type="hidden" name="_method" value="DELETE"/>,这样,在提交表单的时候,Struts2就会知道你当前的请求方式是DELETE,而执行destory()方法。更新也是一样的,加入PUT隐藏域就可以了
2. 默认的约定总是不爽,所以可以在struts.xml中配置convert
- <!-- 类名后缀,默认是Action -->
- <constant name="struts.convention.action.suffix" value="Controller"/>
- <!-- 设置即使没有@Action注释,依然创建Action映射。默认值是false -->
- <constant name="struts.convention.action.mapAllMatches" value="true"/>
<!--设置Action的默认继承包-->- <constant name="struts.convention.default.parent.package" value="rest-default"/>
- <!-- 包名后缀,默认为action、actions、struts、struts2-->
- <constant name="struts.convention.package.locators" value="controller" />
- <!--设置Convention插件是否从其它jar包中搜索Action类,默认值为true-->
- <constant name="struts.convention.action.disableJarScanning" value="true" />
- <!--设置Convention插件文件协议类型-->
- <constant name="struts.convention.action.fileProtocols" value="jar,wsjar" />
- <!--设置Convention插件需要搜索的其它jar包,Convention插件除了扫描默认的action,actions,struts,struts2,还会扫描该常量指定的一个或多个包,Convention会试图从指定包中发现Action类-->
- <constant name="struts.convention.action.includeJars" value=".*?/struts2-action*.*?jar(!/)?" />
- <!--指定其它jar的包作为根包来搜索Action类-->
- <constant name="struts.convention.action.packages" value="*.*.myaction" />
- <!-- 指定视图文件所在的目录地址 -->
- <constant name="struts.convention.result.path" value="/WEB-INF/content/" />
- 18 <!--使用rest映射/rest,struts映射其它的-->
19 <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
20 <constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts" />
21 <constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
在网上看到许多使用这两个插件发现的问题,在此也记下
1. 以war部署在weblogic(10.3.3)时找不到action,在war包classes下添加META-INF目录随便放个文件,在struts.xml中添加
- <constant name="struts.convention.action.includeJars" value=".*?/_wl_cls_gen.*?jar(!/)?" />
- <constant name="struts.convention.exclude.parentClassLoader" value="true" />
- <constant name="struts.convention.action.fileProtocols" value="jar,zip,vfsfile,vfszip" />
2. c#以post方式调用时各种报错
c#中如果以post方法请求url时,不论是HttpWebRequest还是WebClient,默认都会添加expect = 100-continue的头信息,org.apache.struts2.rest.RestActionMapper对此特殊处理,去调用createContinue方法,但是如果Controller没有提供createContinue方法,c#调用时就会报错了。解决办法要么客户端
- webReq.ServicePoint.Expect100Continue = false;//禁止自动添加Except:100-continue到http头信息
要么服务器端Controller总添加createContinue方法
Struts2的零配置和rest插件的更多相关文章
- Struts2 注解零配置方法(convention插件使用)
最近接触到一个新的项目,是做一个使用S2SH的电子商务商城的二次开发.之前使用过S2SH,在此之前的项目中,Struts2 使用的是XML配置而这个项目是使用注解.在这个项目中,注解还不需要使用Act ...
- struts2的零配置
最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置.配置文件精简了,的确是简便了 ...
- Struts2 Convention Plugin ( struts2 零配置 )
Struts2 Convention Plugin ( struts2 零配置 ) convention-plugin 可以用来实现 struts2 的零配置.零配置的意思并不是说没有配置,而是通过约 ...
- struts2 Convention插件零配置,使用注解开发
从struts21开始,struts2不再推荐使用codebehind作为零配置插件,而是改用Convention插件来支持零配置.与以前相比较,Convention插件更彻底. 使用Conventi ...
- 13、零配置Struts2开发
Convention 插件 从 Struts 2.1 开始, Struts 可以使用 Convention 插件来支持零配置: Convention 插件完全抛弃配置信息, 不仅不需要使用 strut ...
- Struts2零配置介绍(约定访问)
从struts2.1开始,struts2 引入了Convention插件来支持零配置,使用约定无需struts.xml或者Annotation配置 需要 如下四个JAR包 插件会自动搜索如下类 act ...
- 从struts2.1开始Convention零配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...
- struts2采用convention-plugin实现零配置
最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置. 配置文件精简了,的确是简便 ...
- 菜鸟学Struts2——零配置(Convention )
又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...
随机推荐
- 记录一次Android交叉编译ffmpeg排查错误
Android版本手机直播引擎中,引用了libvlc开源库.项目接过来,发现编译脚本中使用了很多用户名下的绝对路径.项目相关人离职,导致这个脚本实际上已经废掉.而且不知道相关路径下有没有其他文件和第三 ...
- usb mass storage device
Problem adding USB host device to KVM Windows guest machine. Status: CLOSED CURRENTRELEASE Aliases ...
- libpcap/wwinpcap
winpcap(windows packet capture)是windows平台下一个免费,公共的网络访问系统.开发winpcap这个项目的目的在于为win32应用程序提供访问网络底层的能力.win ...
- Nginx和Tengine解决高并发和高可用,而非推荐Apache
什么是Nginx 什么是Tengine 看看国内大公司在用Nginx和Tengine吗? 步骤一:进入 https://www.taobao.com/,按F12.可看到 有很多APP对淘宝进行请求. ...
- codeforces 630D Hexagons!
D. Hexagons! time limit per test 0.5 seconds memory limit per test 64 megabytes input standard input ...
- Spring Data JPA教程, 第五部分: Querydsl(未翻译)
The fourth part of my Spring Data JPA tutorialdescribed how you can implement more advanced queries ...
- IOC使用Unity 实现依赖注入
转自:http://www.cnblogs.com/techborther/archive/2012/01/06/2313498.html http://www.cnblogs.com/xishuai ...
- SQL存储过程调试
转自:http://www.cnblogs.com/xiangzhong/archive/2012/10/27/2742974.html 今天突然有同事问起,如何在sqlserver中调试存储过程(我 ...
- git乱码问题
直接看连接吧. http://my.oschina.net/lujian863/blog/168837
- iOS 逆向之ARM汇编
最近对iOS逆向工程很感兴趣. 目前iOS逆向的书籍有: <Hacking and Securing IOS Applications>, <iOS Hacker's Handboo ...