为restful开发的话,一定要设计到URL,struts正好是管理URL的
所以在struts的项目中用restful,要遵守struts的规则
rest在struts中对应的方法
上面是官方给出的帮助文档(默认情况下)
- get,不带id参数——index
- get,带id——show
- post,不带id——create
- put,带id——update
- delete,带id——destroy
- get,带id且访问edit资源——edit
- get,不带id访问edit资源——editNew
rest的相关常量
-
struts.mapper.idParameterName
- If set, this value will be the nameof the parameter under which the id is stored. The id will then be removedfrom the action name. Whether or not the method is specified, the mapper willtry to truncate the identifier from the url and store it as a parameter. -
struts.mapper.indexMethodName
- The method name to call for a GETrequest with no id parameter. Defaults toindex. -
struts.mapper.getMethodName
- The method name to call for a GETrequest with an id parameter. Defaults toshow. -
struts.mapper.postMethodName
- The method name to call for a POSTrequest with no id parameter. Defaults tocreate. -
struts.mapper.putMethodName
- The method name to call for a PUTrequest with an id parameter. Defaults toupdate. -
struts.mapper.deleteMethodName
- The method name to call for a DELETErequest with an id parameter. Defaults todestroy. -
struts.mapper.editMethodName
- The method name to call for a GETrequest with an id parameter and theeditview specified. Defaults toedit. -
struts.mapper.newMethodName
- The method name to call for a GETrequest with no id parameter and thenewview specified. Defaults toeditNew.
在struts.xml里面设置这些常量可以改变访问的method以及id参数在action中的的变量名
配置
- 使用restful开发action的package要继承自rest-default
- 在struts.xml中配置
<constant name="struts.mapper.class" value="rest" />
为了让struts根据rest的约定解析URL且映射到具体的action - 将struts2-convention-plugin-2.1.6.jar、struts2-rest-plugin-2.1.6.jar导入项目
- 将 xstream-1.2.2.jar、json-lib-2.1.jar、ezmorph-1.0.3.jar导入项目
注意
- jsp只支持get、post、head方法访问,所以put和delete就返回json数据给前台
- 要返回json数据的话,需要在package继承"json-default"
- 带id参数,是指在url中直接加入id,而不是parameter中,也不需要key,只用value 比如/123,123就是id,/haha,haha也是个id
- ?id=123这种不是带id参数的访问
- id和edit结合的话,id在edit前面 比如/123;edit或者/123/edit
查看原文:http://www.wewill.top/2016/06/07/struts%e6%8b%93%e5%b1%95restful/
相关推荐
因為剛入職 公司要求會使用struts2 restful風格,剛開始都沒聽說過restful,然後在網上找了N久的講解或demo,都沒找到,花了一週 在老大的指導下搞出來了,希望可以幫助到想學restful風格的同學們
使用_Struts_2_开发_RESTful_服务
STRUTS2 RESTFUL PLUGIN
本文内容包括:REST简介资源和标识符操作资源的方式Struts2的REST支持RestActionMapper简介为Struts2应用安装REST插件实现支持REST的Action类实现视图层参考资料从V2.1开始,Struts2开始提供Convention插件,它允许...
Struts2请求转restful所需jar包 ezmorph-1.0.6.jar json-lib-2.3-jdk15.jar struts2-convention-plugin-2.3.14.jar struts2-rest-plugin-2.3.14.jar xstream-1.4.3.jar
Struts 2 依然是一个 MVC 框架,最初设计 Struts 2 时并没有按 REST 架构进行设计,因此 Struts 2 本质上并不是一个 REST 框架。由于 Struts 2 提供了良好的可扩展性,因此允许通过 REST 插件将其扩展成支持 REST 的...
使用 Struts 2 开发 RESTful案例
不包含jar文件,Struts2需要用2.2.3以上版本,JPA使用的是Eclipselink的实现。
struts2整合RESTful所需全部JAR包,其中struts2版本为2.3.15
由于老项目用的struts2,需要扩展支持restful访问接口,前提是不要破坏原系统struts的访问方式。网上查了好多方法,都很麻烦。最后使用了最简单也是最有效的方法,解决了大问题。在此提供给大家参考,希望对大家有...
restful接口示例代码restful接口示例代码restful接口示例代码restful接口示例代码
C# 一个简单的 Restful 服务端和 Restful 客户端 Demo
RESTFUL接口文档模板,样式好看的接口文档模板,docx格式
Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web...
Restful C# 服务端篇之实现RestFul Service开发(简单实用)
struct2-restful-Demo
restful restful所需要的jar包 ========================================= Restlet, a RESTful Web framework for Java ========================================= http://www.restlet.org -------------------...
Spring CXF Restful 实例
一个RESTful的文件下载方法