马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
了解REST
REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。使用这种架构的应用即为RESTFUL。它结构清晰、符合尺度、易于理解、扩展方便, 以是正得到越来越多网站的采用。
在HTTP 协议内里,四个表示利用方式的动词:GET、POST、PUT、DELETE,
它们分别对应四种根本利用:
1、GET ====== 获 取资源
2、POST ======新建资源
3、PUT======= 更新资源
4、DELETE==== 删除资源
我们可以通过rest风格占位符方式,利用@PathVariable注解将占位符的值赋给调用方法参数,实现结果:
/某路径/1 HTTP GET : 得到 id = 1 的 一条数据
/某路径/1 HTTP DELETE: 删除 id = 1的 一条数据
/某路径/1 HTTP PUT: 更新id = 1的 一条数据
/某路径 HTTP POST: 新增一条数据
实现步调如下:
- 在web.xml中设置HiddenHttpMethodFilter过滤器
- <!-- 前端浏览器不支持提交put、delete请求,从而要使用隐藏字段_mothd来提交,hiddenHttpMethodFilte过虑器能过_mothd的值来转换成对应的请求类型提交给Springmvc控制器,
- 从而 实现put、delete请求-->
- <filter>
- <filter-name>hiddenHttpMethodFilte</filter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>hiddenHttpMethodFilte</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
复制代码
- 前端请求中需要参加潜伏字段_method
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>index pag</title>
- </head>
- <body>
- <a href="${pageContext.request.contextPath}/testget">测试get请求</a>
- <form action="${pageContext.request.contextPath}/testpost" method="post">
- id:<input type="text" name="id" value="1"/>
- <button type="submit" value="提交">post提交</button>
- </form>
- <form action="${pageContext.request.contextPath}/testput" method="post">
- 姓名:<input type="text" name="name" value="lish"/>
- <input type="hidden" name="_method" value="put">
- <button type="submit" value="提交">put提交</button>
- </form>
- <form action="${pageContext.request.contextPath}/testdelete" method="post">
- 年龄:<input type="text" name="age" value="30"/>
- <input type="hidden" name="_method" value="delete">
- <button type="submit" value="提交">delete提交</button>
- </form>
- </body>
- </html>
复制代码
- 在控制器中使用method限定方法,使用@PathVariable获取参数
- /**
- *Description:
- *author: ljd
- *@date 2024年9月12日
- *@version 1.0
- */
- package test.springmvc.demo.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- @Controller
- public class PutAndDelete {
- @RequestMapping("/index")
- public String index() {
- return "index";
- }
- @RequestMapping(value = "/testget", method = RequestMethod.GET)
- public String testGet() {
- System.out.println("get...........");
- return "redirect:/index";
- }
- @RequestMapping(value = "/testpost", method = RequestMethod.POST)
- public String testPost(int id) {
- System.out.println("post...........");
- System.out.println("id:" + id);
- return "redirect:/index";
- }
- @RequestMapping(value = "/testput", method = RequestMethod.PUT)
- public String testPut(String name) {
- System.out.println("put...........");
- System.out.println("name:" + name);
- return "redirect:/index";
- }
- @RequestMapping(value = "/testdelete", method = RequestMethod.DELETE)
- public String testDelete(Integer age) {
- System.out.println("delete...........");
- System.out.println("age:" + age);
- return "redirect:/index";
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |