SpringMVC:
- GET: 查
- POST:增
- DELETE:删
- PUT:改
普通浏览器只支持 get与post方式,因此通过过滤器实现四种请求方法。
过滤器拦截条件
- 有隐藏域,满足type = “hidden” name="_method" value=“delete/put”
<input type = "hidden" name="_method" value="delete/put"/>
- 请求方式为POST
过滤器设置
<!-- 增加一个HiddenHttpMethodFilter过滤器,目的是给普通浏览器增加delete和put操作 -->
<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>
可使用同一个请求路径
在使用同一个请求路径时,需要指定method的值,用来匹配对应的请求方式
此时均使用testRest的请求路径,但是在value处用不同的请求方式,在handler中对不同的请求方式做不同的处理
<form action="Handler/testRest/1234" method="POST">
<input type="submit" value="增">
</form>
<form action="Handler/testRest/1234" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="删">
</form>
<form action="Handler/testRest/1234" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="改">
</form>
<form action="Handler/testRest/1234" method="get">
<input type="submit" value="查">
</form>
@RequestMapping(value = "testRest/{id}",method=RequestMethod.POST)
public String testPost(@PathVariable("id") Integer id) {
System.out.println("post:增" + id);
return "success";
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.GET)
public String testGet(@PathVariable("id") Integer id) {
System.out.println("get:查" + id);
return "success";
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.DELETE)
public String testDelete(@PathVariable("id") Integer id) {
System.out.println("delete:删" + id);
return "success";
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.PUT)
public String testPut(@PathVariable("id") Integer id) {
System.out.println("put:改" + id);
return "success";
}
接收传输的参数
- 普通参数
使用注解@RequestParam(“key”) 然后赋值给后接的变量
此处key对应前端表单中的name字段
require表示是否为必填字段,默认值true(必填)
defaultValue表示字段的默认值
如有多个字段,用“,”隔开
@RequestMapping(value="testParma")
public String testParma(@RequestParam("uname") String name,@RequestParam(value="uage",required = false,defaultValue = "23") Integer age) {
System.out.println(name);
System.out.println(age);
return "success";
}
<form action="Handler/testParma" method="get">
name:<input name="uname" type="text" />
age:<input name="uage" type="text"/>
<input type="submit" value="查">
</form>
如果要获取RequestHeader和Cookie的值,只需修改注解
@RequestMapping(value="testRequestHeader")
public String testRequestHeader(@RequestHeader("Accept-Language") String language) {
System.out.println(language);
return "success";
}
@RequestMapping(value="testCookieValue")
public String testCookieValue(@CookieValue("JSESSIONID")String jsessionid) {
System.out.println(jsessionid);
return "success";
}
- 对象类型
springmvc支持将传入参数自动转换为对象类型
i.表单中的name值需要与对象的字段名一致
ii.支持级联对象,可使用a.xx
handler控制器
@RequestMapping(value="testObjectProperties")
public String testObjectProperties(Student student) {
System.out.println(student.getId());
return "success";
}
前端表单
<form action="Handler/testObjectProperties" method="post">
id:<input name="id" type="number">
name:<input name="name">
homeaddress:<input name="address.homeAddress">
schooladdress:<input name="address.schoolAddress">
<input type="submit" value="提交">
</form>
实体类Student
package person.demo.entity;
public class Student {
private int id;
private String name;
private Address address;
public Student() {
}
public Student(int id, String name, Address address) {
super();
this.id = id;
this.name = name;
this.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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
实体类Address
package person.demo.entity;
public class Address {
private String homeAddress;
private String schoolAddress;
public Address() {
}
public Address(String homeAddress, String schoolAddress) {
super();
this.homeAddress = homeAddress;
this.schoolAddress = 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;
}
}