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 ...
随机推荐
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- Azure 公网及内网ip绑定方法
此文章为我的云服务绑定情况,仅供参考,适用于已经创建vm,但开始未绑定vip,后期进行vip的绑定,注意:绑定ip会造成虚拟机暂时离线. -Location(vm所在地) 注意区分 北部和东部: Ch ...
- JSF 2 checkboxes example
In JSF, <h:selectBooleanCheckbox /> tag is used to render a single HTML input element of " ...
- JSF 2.0 hello world example
In this tutorial, we will show you how to develop a JavaServer Faces (JSF) 2.0 hello world example, ...
- Spring 使用注解方式进行事物管理
大家在使用spring的注解式事务管理时,对事务的传播行为和隔离级别可能有点不知所措,下边就详细的介绍下以备方便查阅. 事物注解方式: @Transactional 当标于类前时, 标示类中所有方法都 ...
- IP访问SQL数据库设置
http://wenku.baidu.com/link?url=mnjuPMo9qJvzluCHEvqVDawpuloKeGla05a2L3UtqzD_bF1VJMb7jHY4SBhuYH3-K_xF ...
- HDU/杭电2013多校第三场解题报告
今天悲剧了,各种被虐啊,还是太年轻了 Crime 这道题目给的时间好长,第一次就想到了暴力,结果华丽丽的TLE了. 后来找了一下,发现前24个是1, 2, 6, 12, 72, 72, 864, 17 ...
- [转]freemarker中的list
转至:http://zhuyuehua.iteye.com/blog/1975251 freemarker list (长度,遍历,下标,嵌套,排序) 1. freemarker获取list的size ...
- 【JDBC】预编译SQL与防注入式攻击
在JDBC编程中,常用Statement.PreparedStatement 和 CallableStatement三种方式来执行查询语句,其中 Statement 用于通用查询, PreparedS ...
- VC中监测函数运行时间(一)—分钟,秒,毫秒
//myTimer.h // [10/16/2013 Duan Yihao] #pragma once #include "StdAfx.h" ////////////////// ...