springmvc实现REST中的GET、POST、PUT和DELETE
spring mvc 支持REST风格的请求方法,GET、POST、PUT和DELETE四种请求方法分别代表了数据库CRUD中的select、insert、update、delete,下面演示一个简单的REST实现过程。
参照http://blog.csdn.net/u011403655/article/details/44571287创建一个spring mvc工程
创建一个包,命名为me.elin.rest,添加一个RESTMethod类,代码如下
package me.elin.rect;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/rest")
public class RESTMethod {
private static final String SUCCESS = "success";
// 该方法接受POST传值,请求url为/rest/restPost
@RequestMapping(value = "restPost", method = RequestMethod.POST)
public String restPost(@RequestParam(value = "id") Integer id) {
System.out.println("POST ID:" + id);
return SUCCESS;
}
// 该方法接受GET传值,请求url为/rest/restGet
@RequestMapping(value = "/restGet", method = RequestMethod.GET)
public String restGet(@RequestParam(value = "id") Integer id) {
System.out.println("GET ID:" + id);
return SUCCESS;
}
// 该方法接受PUT传值,请求url为/rest/restPut
@RequestMapping(value = "/restPut", method = RequestMethod.PUT)
public String restPut(@RequestParam(value = "id") Integer id) {
System.out.println("PUT ID:" + id);
return SUCCESS;
}
// 该方法接受DELETE传值,请求url为/rest/restDelete
@RequestMapping(value="/restDelete",method=RequestMethod.DELETE)
public String restDelete(@RequestParam(value = "id") Integer id) {
System.out.println("DELETE ID:" + id);
return SUCCESS;
}
}
在web.xml中添加一个filter,用来过滤rest中的方法。代码如下
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在WebContent下创建index.jsp文件,添加如下内容
<a href="rest/restGet?id=1">发送GET请求</a>
<form action="rest/restPost" method="post">
<input type="text" name="id" value=""/>
<input type="submit" value="发送POST请求"/>
</form>
<form action="rest/restPut" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="text" name="id" value="">
<input type="submit" value="发送PUT请求">
</form>
<form action="rest/restDelete" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="text" name="id" value="">
<input type="submit" value="发送DELETE请求">
</form>
其中get和post方法是html中自带的,但是不支持PUT和DELETE方法,所以需要通过POST方法模拟这两种方法,只需要在表单中添加一个隐藏域,名为_method,值为PUT或DELETE。
运行程序,index.jsp中一个超链接和三个表单分别表示了四种请求方法。
springmvc实现REST中的GET、POST、PUT和DELETE的更多相关文章
- SpringMVC结合easyUI中datagird实现分页
SpringMVC结合easyUI中datagird实现分页 DataGrid以表格形式展示数据,并提供了丰富的选择.排序.分组和编辑数据的功能支持.轻量级,单元格合并.多列标题.冻结列和页脚只是其中 ...
- 解决SpringMVC拦截器中Request数据只能读取一次的问题
解决SpringMVC拦截器中Request数据只能读取一次的问题 开发项目中,经常会直接在request中取数据,如Json数据,也经常用到@RequestBody注解,也可以直接通过request ...
- SpringMVC重定向路径中带中文参数
SpringMVC重定向路径中带中文参数 springboot重定向到后端接口测试 package com.mozq.http.http_01.demo; import org.springframe ...
- springmvc配置过程中遇到的一些问题总结
springmvc配置过程中遇到的一些问题总结 1.配置tomcat过程中的错误: 2.配置web.xml中DispatchServlet报红(配置好已有依赖条件下) 解决的办法: 因为新添加依赖,m ...
- Web Api中实现Http方法(Put,Post,Delete)
在Web Api中实现Http方法(Put,Post,Delete) 系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在Web Api中,我 ...
- 一起学HBase——总结HBase中的PUT、GET、DELETE操作
传统的关系型数据库有CRUD增删改查操作,同样对于NoSQL列式数据库也有CRUD操作.本文对HBase中常用的Scan.GET.PUT.DELETE操作的用法做个总结. Put操作 Put相当于传统 ...
- ZT c++ 中的重载全局new,delete
c++ 中的重载全局new,delete 分类: c++ 2010-08-06 10:31 116人阅读 评论(1) 收藏 举报 deletec++file编译器语言工作 最近做一个小项目,对c++又 ...
- SpringMVC的Controller中使用线程安全的初始化
因为SpringMVC的Controller默认是单例, 在这种情况下, Controller中使用的私有变量必须也是单例, 例如各种service, 否则会有多线程访问数据互相修改的问题. 对于需要 ...
- 一个SpringMVC简单Demo中出现的错误
最近在学springmvc 一个简答的Springmvc配置包括如下步骤: 1.在 web.xml 文件中配置 DispatcherServlet (该中央控制器相当于 MVC 模式中的 C),还可以 ...
- springmvc的3中路径风格
1.导入相应的jar包,文件放置情况 2.web.xml <?xml version="1.0" encoding="UTF-8"?> <we ...
随机推荐
- 刚入门的easyui
这两天看了下easyui的教学先说说自己的一些小小理解吧! ----在使用easyui中也遇到了一个问题 : Uncaught TypeError:cannot call method ‘offset ...
- 正则表达式匹配(python)
获取图片的python代码 #coding=utf-8 import urllib import re def getHtml(url): page = urllib.urlopen(url) htm ...
- (转)dedecms代码详解 很全面
dedecms代码研究(1)开篇dedecms 相信大家一定都知道这个cms 系统,功能比较强大,有比较完善的内容发布,还有内容静态化系统,还有就是它有自己独特的标签系统和模板系统.而模板系统也是其他 ...
- c# 学习笔记(三)
程序集程序集的私有部署 不用在注册表中注册组件卸载只需要从文件系统中删除他即可 共享程序集和GAC 只有强命名程序集能被添加到GAC中程序集数据签名只需在安装到GAC时检查一次 GAC内的并肩执行GA ...
- asp.net 真正实现完全跨域单点登录
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. asp.ne ...
- 015_xml_函数
015_xml_函数 --环境准备******************************************************************* USE test --f:/t ...
- 【转】 iOS如何实现表格的折叠效果?
原文 : http://blog.csdn.net/youcanping2008/article/details/9202167 一.实现原理:就是在点击表格组头视图的时候,如果该表格视图的组展开了 ...
- [转]Delphi导出Excel的设置操作
procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer); {新建Excel工作簿.进行页面设置} begin {新建Exc ...
- C++工厂方法模式
class IFactoryBase { public: IFactoryBase(void); virtual ~IFactoryBase(void); public: virtual IProdu ...
- AngularJs的Select演示
昨天需要在项目使用Angular.js的select,测试了好久才研究出怎么进行赋值,操作. HTML代码 <!DOCTYPE html> <html> <head> ...