restful

restful是一种格式,具体来说,就是同一个请求,访问相同的内容,但是用不同的请求方式区分不同的操作。

查询所有信息   get方法,不带参数

查询单个信息  get方法, 带参数

插入信息,     post方法,带参数

修改信息,     put方法,带参数

删除信息,   delete方法,带参数

控制方法

package org.hxut.rj1192.zyk;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class testful_test {
    @RequestMapping(value = "/test_restful",method = RequestMethod.GET)
    public  String query_all()
    {   System.out.println("查询所有用户信息");
        return "sucess";
    }
    @RequestMapping(value = "/test_restful/{id}",method = RequestMethod.GET)
    public  String query_one(@PathVariable("id") String uid)
    {   System.out.println("查询单个用户信息"+uid);
        return "sucess";
    }
    @RequestMapping(value = "test_restful",method = RequestMethod.POST)
    public String add_one(int id ,String username)
    {
        System.out.println("添加单个用户信息"+id+username);
        return "sucess";
    }
    @RequestMapping(value = "test_restful",method = RequestMethod.PUT)
    public String change_one(int id ,String username)
    {  System.out.println("修改某个用户信息"+id+username);
        return "sucess";
    }
    @RequestMapping(value = "test_restful",method = RequestMethod.DELETE)
    public String del_one()
    {  System.out.println("删除某个用户信息");
        return "sucess";
    }
}

 前端页面

<!DOCTYPE html >
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>请求</title>
</head>
<body>

<a th:href="@{/test_restful}">查询所有用户信息</a>
<a th:href="@{/test_restful/11}">查询id为1的用户信息</a>
<form th:action="@{/test_restful}" method="post">
    <input type="text" name="id">
    <input type="text" name="username">
    <button type="submit">插入单个用户信息</button>
</form>
<form th:action="@{/test_restful}" method="post">
<!-- DispatcherServlet源码  已经设置了拦截器,拦截所有请求,并将_method不为空的请求,转为_method的对应的请求(仅限于PUT DELETE)-->
<input type="hidden" name="_method" value="put">
    <input type="text" name="id">
    <input type="text" name="username">
    <button type="submit">插入单个用户信息</button>
</form>
<form th:action="@{/test_restful}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit">删除单个用户信息</button>
</form>
</body>
</html>

在web.xml中注册HiddenHttpMethodFilter

SpringMVC 提供了 HiddenHttpMethodFilter 帮助我们将 POST 请求转换为 DELETE 或 PUT 请求

HiddenHttpMethodFilter 处理put和delete请求的条件:

a>当前请求的请求方式必须为post

b>当前请求必须传输请求参数_method

满足以上条件,HiddenHttpMethodFilter 过滤器就会将当前请求的请求方式转换为请求参数_method的值,因此请求参数_method的值才是最终的请求方式

就是所有的请求要给HiddenHttpMthodFilter过滤一遍,如果是post请求,且_method属性不为空,则会将请求类型转为_method的值,放行。如果不是post请求,且_method不为空,直接放行

<!-- 拦截所有请求,并交给hiddenhttpmethodfilter 检测否是post请求,且_method不为空,如果是,就将请求类型改为_method的值-->
    <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>

想要发送post请求,且有个参数_method可以使用form表单,如下

<form th:action="@{/test_restful}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit">删除单个用户信息</button>
</form>

如果想用a标签删除元素

要新建一个隐藏的form,在form中写个隐藏域,声明_method方法,将form为post提交

,将a标签的提交绑定在这个表单上,a标签一点击,vue就提交这个form。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Employee Info</title>
</head>
<body>

<table id="dataTable" border="1" cellspacing="0" cellpadding="0" style="text-align: center;">
    <tr>
        <th colspan="5">Employee Info</th>
    </tr>
    <tr>
        <th>id</th>
        <th>lastName</th>
        <th>email</th>
        <th>gender</th>
        <th><a th:href="@{/toAdd}">add</a></th>
    </tr>
    <tr th:each="employee : ${all_employees}">
        <td th:text="${employee.id}"></td>
        <td th:text="${employee.lastName}"></td>
        <td th:text="${employee.email}"></td>
        <td th:text="${employee.gender}"></td>
        <td>
            <a @click="deleteEmployee" th:href="@{'/employee/'+${employee.id}}">delete</a>
            <a th:href="@{'/employee/'+${employee.id}}">update</a>
        </td>
    </tr>
</table>
<!--a标签点击,提交这个表单-->
<form id="deleteForm" method="post">
    <input type="hidden" name="_method" value="delete">
</form>

<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
<script type="text/javascript">
    var vue = new Vue({
        el:"#dataTable",
        methods:{
            //使用vue,让a标签点击的时提交表单
            deleteEmployee:function (event) {
                //根据id获取表单元素
                var deleteForm = document.getElementById("deleteForm");
                //将触发点击事件的超链接的href属性赋值给表单的action,a的href属性就带有参数,要删除的编号
                deleteForm.action = event.target.href;
                //提交表单
                deleteForm.submit();
                //取消超链接的默认行为
                event.preventDefault();
            }
        }
    });
</script>
</body>
</html>

注:

在web.xml中注册时,必须先注册CharacterEncodingFilter,再注册其他过滤器或者拦截器,

因为编码设置一定要在第一位,否则会造成其他拦截器获得的内容乱码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值