p5 Rest风格
b.表单
c.控制器
@RequestMapping(value="testRest/{id}",method=RequestMethod.DELETE)
public String testDelete(@PathVariable("id") Integer id) {
System.out.println("delete:删 " +id);
//Service层实现 真正的增
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
通过 method=RequestMethod.DELETE 匹配具体的请求方式
此外,可以发现 ,当映射名相同时@RequestMapping(value="testRest),可以通过method处理不同的请求。
过滤器中 处理put|delete请求的部分源码:
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
HttpServletRequest requestToUse = request;
if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
requestToUse = new HttpMethodRequestWrapper(request, paramValue);
}
}
filterChain.doFilter(requestToUse, response);
}
原始请求:request,改请求默认只支持get post header
但是如果 是"POST" 并且有隐藏域
则,过滤器 将原始的请求 request加入新的请求方式DELETE,并将原始请求 转为 requestToUse 请求(request+Delete请求)
最后将requestToUse 放入 请求链中, 后续再事情request时 实际就使用改造后的 requestToUse
@RequestParam("uname") String name,@RequestParam(value="uage",required=false,defaultValue="23")
@RequestParam("uname"):接受前台传递的值,等价于request.getParameter("uname");
required=false:该属性 不是必须的。
defaultValue="23":默认值23
获取请求头信息 @RequestHeader
public String testRequestHeader(@RequestHeader("Accept-Language") String al ) {
通过@RequestHeader("Accept-Language") String al 获取请求头中的Accept-Language值,并将值保存再al变量中
通过mvc获取cookie值(JSESSIONID)
@CookieValue
(前置知识: 服务端在接受客户端第一次请求时,会给该客户端分配一个session (该session包含一个sessionId)),并且服务端会在第一次响应客户端时 ,请该sessionId赋值给JSESSIONID 并传递给客户端的cookie中
小结:
SpringMVC处理各种参数的流程/逻辑:
请求: 前端发请求a-> @RequestMappting("a")
处理请求中的参数xyz:
@RequestMappting("a")
public String aa(@Xxx注解("xyz") xyz)
{
}
使用对象(实体类Student)接受请求参数
在SpringMVC中使用原生态的Servlet API :HttpServletRequest :直接将 servlet-api中的类、接口等 写在springMVC所映射的方法参数中即可:
@RequestMapping(value="testServletAPI")
public String testServletAPI(HttpServletRequest request,HttpServletResponse response) {
// request.getParameter("uname") ;
System.out.println(request);
return "success" ;
}
Address.java
package org.lanqiao.entity;
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;
}
}
Student.java
package org.lanqiao.entity;
public class Student {
private int id;
private String name;
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
SpringMVCHandler.java
package org.lanqiao.handler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.lanqiao.entity.Address;
import org.lanqiao.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//接口/类 注解 配置
@Controller
@RequestMapping(value="handler") //映射
public class SpringMVCHandler {
@RequestMapping(value="welcome",method=RequestMethod.POST,params= {"name=zs","age!=23","!height"})//映射
public String welcome() {
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="welcome2",headers= {"Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Encoding=gzip, deflate"})
public String welcome2() {
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="welcome3/**/test")
public String welcome3() {
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="welcome4/a?c/test")
public String welcome4() {
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="welcome5/{name}")
public String welcome5(@PathVariable("name") String name ) {
System.out.println(name);
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.POST)
public String testPost(@PathVariable("id") Integer id) {
System.out.println("post:增 " +id);
//Service层实现 真正的增
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.GET)
public String testGet(@PathVariable("id") Integer id) {
System.out.println("get:查 " +id);
//Service层实现 真正的增
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.DELETE)
public String testDelete(@PathVariable("id") Integer id) {
System.out.println("delete:删 " +id);
//Service层实现 真正的增
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.PUT)
public String testPut(@PathVariable("id") Integer id) {
System.out.println("put:改 " +id);
//Service层实现 真正的增
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="testParam")
public String testParam(@RequestParam("uname") String name,@RequestParam(value="uage",required=false,defaultValue="23") Integer age) {
// String name = request.getParameter("uname");
System.out.println(name);
System.out.println(age);
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="testRequestHeader")
public String testRequestHeader(@RequestHeader("Accept-Language") String al ) {
System.out.println( al);
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="testCookieValue")
public String testCookieValue(@CookieValue("JSESSIONID") String jsessionId) {
System.out.println( jsessionId);
return "success" ;// views/success.jsp,默认使用了 请求转发的 跳转方式
}
@RequestMapping(value="testObjectProperties")
public String testObjectProperties(Student student) {//student属性 必须 和 form表单中的属性Name值一致(支持级联属性)
/*
String name = request.getParameter("name");
int age= Integer.parseInt(request.getParameter("age")s) ;
String haddrss = request.getParameter("homeaddress");
String saddress = request.getParameter("schooladdress");
Address address = new Address();
address.setHomeAddress(haddrss);
address.setSchoolAddress(saddress);
Student student = new Student();
student.setName(name);
student.setAddress(address);
*/
System.out.println(student.getId()+","+student.getName()+","+student.getAddress().getHomeAddress()+","+student.getAddress().getSchoolAddress());
return "success" ;
}
@RequestMapping(value="testServletAPI")
public String testServletAPI(HttpServletRequest request,HttpServletResponse response) {
// request.getParameter("uname") ;
System.out.println(request);
return "success" ;
}
}
springmvc.xml
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
Insert title here
welcome to spring mvc
web.xml
SpringMVCProject
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springDispatcherServlet
/
HiddenHttpMethodFilte
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilte
/*
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
33333333get - welcome
4444444get - welcome
555welcome
======
------------
testRequestHeader
testCookieValue
testServletAPI