<1>请求方式
GET: 查
POST:增
DELETE:删
PUT:改
但是一般的浏览器只支持get和post请求,不支持put和delete请求,那么我们就要配置过滤器,拦截所有的请求,识别真的请求方式,将其改变之后再传给DispatcheServlet
过滤器的配置方法,在web.xml中加上一下代码
<filter> <filter-name>filter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern> <!-- <servlet-name>dispatcher</servlet-name>--> </filter-mapping>
``
过滤器已经配置好。
下面是测试delete和put请仔细阅读
@ResponseBody
@Controller
public class Resultful {
@RequestMapping(value = “/testPut”,method = RequestMethod.PUT) //测试PUT请求
public String demo1(){
//假设这里是dao的update
return “hello”;
}
@RequestMapping(value = “/testDelete”,method = RequestMethod.DELETE) //测试DELETE请求
public String demo2(){
System.out.println(123);
//假设这里是dao的delete
return “hello”;
}
@RequestMapping(value = “/testParam”,method = RequestMethod.GET) //获取text中的值
public String demo3(@RequestParam(“userName”) String name){
System.out.println(123);
System.out.println(name);
return “hello”;
}
@RequestMapping(value = "/testParam1",method = RequestMethod.GET) //非必须参数的接受
public String demo4(@RequestParam("userName") String name,@RequestParam(value = "age",required = false) int age){
System.out.println(123);
System.out.println(name+age);
return "hello";
}
@RequestMapping(value = "/testHeader",method = RequestMethod.GET) //接受请求头的信息和cookie的值
public String demo5(@RequestHeader("Accept") String length,@CookieValue("JSESSIONID") String jss){
System.out.println(length);
System.out.println(jss);
return "hello";
}
/**
这才是mvc的强大之处,可以将前端的数据直接封装到引用类型的对象中。而且可以级联
Student类Address类 还有前端请看下面啊
*/
@RequestMapping(value = "/objectTest",method = RequestMethod.GET) //接受引用类
public String demo6(Student student){
System.out.println(student==null);
System.out.println(student.getName());
return "hello";
}
@RequestMapping(value = "/servletApiTest",method = RequestMethod.GET) //mvc中也可以使用
public String demo7(HttpServletRequest request){
System.out.println(request.getCookies()); //就像servlet中的一样使用即可
return "hello";
}
}
//studen类
public class Student {
private int id;
private String name;
private int age;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return this.name+age+address.toString();
}
}
Address类
package Controller.Enitry;
import lombok.Data;
import lombok.ToString;
/**
* @author 刘棋军
* @date2019-03-11
*/
public class Address {
private String homeAddress;
private String schoolAddress;
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getSchoolAddress() {
return schoolAddress;
}
public void setSchoolAddress(String schoolAddress) {
this.schoolAddress = schoolAddress;
}
@Override
public String toString() {
return homeAddress+schoolAddress;
}
}
前端
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/3/11
Time: 19:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/testDelete" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="删"/>
</form>
<form action="/testPut" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="改"/>
</form>
<form action="/testParam">
<input type="text" name="userName" />
<input type="submit" value="参数"/>
</form>
<form action="/testHeader">
<input type="submit" value="Header"/>
</form>
<form action="/objectTest">
id:<input type="text" name="id"/>
name:<input type="text" name="name"/>
age:<input type="text" name="age"/>
home:<input type="text" name="Address.homeAddress"/>
school:<input type="text" name="Address.schoolAddress"/>
<input type="submit" value="object"/>
</form>
</body>
</html>