springMVC—Restful风格:get,post,delete,put请求案例
Restful
核心:请求地址URL不变,根据请求方式的不同,对操作资源的方式进行区分
- get 查询
- post 添加
- delect 删除
- put 修改
处理put和delete方法
HiddenHttpMethodFilte
配置HiddenHttpMethodFilter:
<!--配置HiddenHttpMethodFilter-->
<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属性提交
查看源码:
其实绕这么一大串,就是对你传过来的请求方式名和过滤器里的允许的请求方式名匹配
以POST方式发送请求,然后在request域中携带参数_method = “实际的请求方法(DELETE or PUT )”,包装器类封装新的请求,然后filterChain.doFilter(新包装请求,原响应),把post替换成了其他请求
就是将请求方式作为参数传给后端,过滤器会将参数指定的请求方式作为请求的真正方式
@Controller
public class UserController {
/**
* 使用Restful风格实现用户资源的增删改查
* /user
*/
@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getAllUser(){
System.out.println("查询所有用户信息");
return "success";
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public String getUserById(@PathVariable("id") Integer id){
System.out.println("查询用户:"+id);
return "success" ;
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String addUser(String username,String password){
System.out.println("添加用户信息:"+username+password);
return "success";
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String modifyUser(String username,String password){
System.out.println("修改用户信息:"+username+password);
return "success";
}
}
<a th:href="@{/user}">查询所有用户信息</a><br>
<a th:href="@{/user/1}">根据id查询用户信息</a><br><!--超链接的请求方式就是get-->
<!--表单的method如果不写post或者get.也会默认为get-->
<form th:action="@{/user}" method="post">
用户名:<input type="text" name="username">
密码<input type="password" name="password">
<input type="submit" value="添加">
</form>
<form th:action="@{/user}" method="post">
<input type="hidden" name="_method" value="put">
用户名:<input type="text" name="username">
密码<input type="password" name="password">
<input type="submit" value="添加">
</form>
delete 如果要使用超链接,要绑定到一个表单,利用表单提交post,隐藏域delete。
注意!!!
springMVC的编码过滤器 过滤前不能获取参数,否则失效。
如果HiddenHttpMethodFilter写在前面,获取了_method的常数,那么编码过滤器就会失效
解决方法:在xml中springMVC的编码过滤器配置在前面即可
Restful使用案例
delete请求(包含get请求)
<!--原理-->
<form th:action="@{/employee/1001}" method="post">
<!-- HiddenHttpMethodFilter要求:必须传输_method请求参数,并且值为最终的请求方式 -->
<input type="hidden" name="_method" value="delete"/>
<input type="submit" value="删除第一个">
</form>
//@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
public String deleteEmployee(@PathVariable("id") Integer id) {
employeeDao.delete(id);
return "redirect:/employee";//相当于地址栏输入,就是get请求
}
删除功能时 使用了本地的vue要:1.重新打包一下项目 2.因为所有请求都经过了前端控制器,所以要在SpringMVC中配置一下(添加下面的代码)。
原请求前端控制器找不到对应的控制器,开放了默认的servlet就可以了
访问静态资源的问题:
意思就是说,这个Handler接收了前端控制器处理不了的请求,并在内部将请求交给了Tomcat的DefaultServlet去处理!所以真正处理请求的是Tomcat的DefaultServlet
<!--开启MVC注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--开放对静态资源的访问-->
<!--如果在控制器找不到相当于的请求映射,就交给默认的servlet去找-->
<mvc:default-servlet-handler/>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-buYg5gMB-1649255520840)(D:\学习\截图\image-20220404131518110.png)]
绑定了超链接的实现:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>员工信息</title>
</head>
<body>
<table id="dataTable" border="1" cellspacing="0" cellpadding="0" style="text-align: center">
<tr>
<th colspan="5">员工信息</th>
</tr>
<tr>
<th>id</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>操作</th>
</tr>
<tr th:each="employee:${employeeList}">
<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>
<!-- th:href="@{/emloyee/}+${employee.id}" 也可以-->
<a @click="deleteEmployee" th:href="@{'/employee/'+${employee.id}}">delete</a>
<a>update</a>
</td>
</tr>
</table>
<!-- 作用:通过超链接控制表单的提交,将post请求转换为delete请求 -->
<form id="delete_form" method="post">
<!-- HiddenHttpMethodFilter要求:必须传输_method请求参数,并且值为最终的请求方式 -->
<input type="hidden" name="_method" value="delete"/>
</form>
<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
<!--<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> 使用这个就不用弄springMVC注解搞了-->
<script type="text/javascript">
var vue = new Vue({
el:"#dataTable",
methods:{
deleteEmployee:function (event){
var delete_form = document.getElementById("delete_form");
//点击事件的href属性 赋值给表单的action
delete_form.action=event.target.href;
//提交表单
delete_form.submit();
//取消超链接的默认行为(原get提交)
event.preventDefault();
}
}
});
</script>
<!--原理-->
<form th:action="@{/employee/1001}" method="post">
<!-- HiddenHttpMethodFilter要求:必须传输_method请求参数,并且值为最终的请求方式 -->
<input type="hidden" name="_method" value="delete"/>
<input type="submit" value="删除第一个">
</form>
</body>
</html>
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@GetMapping("/employee")
public String getAllEmployee(Model model) {
Collection<Employee> employeeList = employeeDao.getAll();
model.addAttribute("employeeList", employeeList);
return "employee_list";
}
//@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
public String deleteEmployee(@PathVariable("id") Integer id) {
employeeDao.delete(id);
return "redirect:/employee";//相当于地址栏输入,就是get请求
}
}
post请求:
<body>
<form th:action="@{/employee}" method="post">
lastName:<input type="text" name="lastName"><br>
email:<input type="text" name="email"><br>
gender:<input type="radio" name="gender" value="1">male<br>
<input type="radio" name="gender" value="0">female<br>
<input type="submit" value="提交添加"><br>
</form>
</body>
@PostMapping("/employee")
public String addEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/employee";
}
put请求(修改)
首先使用了get信息回显:
field单选框回显
<form th:action="@{/employee}" method="post">
<input type="hidden" name="_method" value="put">
<input type="hidden" name="id" th:value="${employee.id}">
lastName:<input type="text" name="lastName" th:value="${employee.lastName}"><br>
email:<input type="text" name="email" th:value="${employee.email}"><br>
gender:
<input type="radio" name="gender" value="1" th:field="${employee.gender}">male<br>
<input type="radio" name="gender" value="0" th:field="${employee.gender}">female<br>
<input type="submit" value="提交修改"><br>
</form>
@GetMapping("/employee/{id}") //这是回显信息
public String getEmployee(@PathVariable("id") Integer id,Model model) {
Employee employee = employeeDao.get(id);
model.addAttribute("employee", employee);
return "employee_updata";
}
@PutMapping("/employee") //表单提交后的修改
public String updateEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/employee";
}