Struts2 REST 小记

来源:互联网 发布:手机淘宝咋用微信支付 编辑:程序博客网 时间:2024/05/17 21:54
REST -Representational State Transfer ( 一种基于网络的软件架构风格 )

struts2支持rest插件:struts2-rest-plugin-.x.x.x.jar
在插件架包中包含一个struts-plugin.xml文件,其中定义了RestActionMapper:
<bean type="org.apace.struts2.dispatcher.mapper.ActionMappername="restclass="org.apache.struts2.rest.RestActionMapper" />

通过查看RestActionMapper的API说明,可以看出它支持以下几种参数形式:
struts.mapper.idParameterName:用于设置ID参数的参数名,该属性值默认是id。
struts.mapper.indexMethodName:设置不带id请求参数的GET请求调用Action的哪个方法,该属性值默认是index。
struts.mapper.getMethodName设置带id请求参数的GET请求调用Action的哪个方法,该属性值默认是show。
struts.mapper.postMethodName设置不带id请求参数的POST请求调用Action的哪个方法,该属性值默认是create。
struts.mapper.putMethodName设置带id请求参数的PUT请求调用Action的哪个方法,该属性值默认是update。
struts.mapper.deleteMethodName设置带id请求参数的DELETE请求调用Action的哪个方法,该属性值默认是destroy。
struts.mapper.editMethodName设置带id请求参数、且指定操作edit的GET请求调用Action的哪个方法,该属性值默认是edit。
struts.mapper.newMethodName设置不带id请求参数、且指定操作edit的GET请求调用Action的哪个方法,该属性值默认是editNew。

RestActionMapper 对HTTP请求的处理

目前标准的HTML语言并不支持PUT和DELETE这两个操作,REST插件提供了_method参数来模拟HTTP协议的PUT和DELETE操作。
例:
在form中加入一个隐藏参数:<input type="hidden" name="_method" value="put" />

struts.xml配置文件:
<!-- 指定控制后缀名 -->
<contant name="struts.convention.action.suffixvalue="Controller" />
<!-- 指定Action所在包继承的父包 -->
<contant name="struts.convention.default.parent.packagevalue="rest-default" />
原创粉丝点击