struts拓展restful

为restful开发的话,一定要设计到URL,struts正好是管理URL的

所以在struts的项目中用restful,要遵守struts的规则

rest在struts中对应的方法

restful

上面是官方给出的帮助文档(默认情况下)

  • 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 name of the parameter under which the id is stored. The id will then be removed from the action name. Whether or not the method is specified, the mapper will  try to truncate the identifier from the url and store it as a parameter.
  • struts.mapper.indexMethodName - The method name to call for a GET request with no id parameter. Defaults to index.
  • struts.mapper.getMethodName - The method name to call for a GET request with an id parameter. Defaults to show.
  • struts.mapper.postMethodName - The method name to call for a POST request with no id parameter. Defaults to create.
  • struts.mapper.putMethodName - The method name to call for a PUT request with an id parameter. Defaults to update.
  • struts.mapper.deleteMethodName - The method name to call for a DELETE request with an id parameter. Defaults to destroy.
  • struts.mapper.editMethodName - The method name to call for a GET request with an id parameter and the edit view specified. Defaults to edit.
  • struts.mapper.newMethodName - The method name to call for a GET request with no id parameter and the new view specified. Defaults to editNew.

在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/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RESTful是一种设计风格和架构原则,用于构建可伸缩的网络应用程序。它是一种基于HTTP协议的约束性架构,旨在提供一种简单、轻量级、可扩展和可维护的方式来设计和构建Web服务。 RESTful API是基于RESTful原则设计的API。它使用HTTP协议的GET、POST、PUT和DELETE等请求方法,通过URL和请求头传递参数,使用状态码和响应体返回数据,实现了客户端和服务器之间的通信和数据交互。RESTful API通常使用JSON或XML格式来传输数据。 在引用中提到的SpringBoot Mybatis CXF框架,可以帮助我们快速开发和部署RESTful API。而引用中的ansible-api是一个基于RESTful HTTP API的IT自动化系统,它提供了一种使用RESTful API进行配置和管理的方式。 总之,RESTful是一种设计风格和架构原则,而RESTful API是基于RESTful原则设计的API,用于实现客户端和服务器之间的通信和数据交互。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验](https://download.csdn.net/download/u011938827/87662947)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ansible-api:Ansible的RESTful HTTP Api](https://download.csdn.net/download/weixin_42138139/14971104)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值