使用@RequestMapping 注解映射请求
- 映射类
-
映射方法
- 映射请求参数,请求方法或请求头
- 使用@RequestMapping指定请求方式get/post,(常用)
@RequestMapping("springmvc")
@Controller
public class SpringMVCTest {
public static final String SUCCESS = "success";
/**
* 比较常用:使用method属性来指定请求方式(post方式)
* @return
*/
@RequestMapping(value="/testMethod", method=RequestMethod.POST)
public String testMethod()
{
System.out.println("testMethod");
return SUCCESS;
}
}
前台 index.jsp
页面
@PathVariable映射URL绑定的占位符
REST
Rest风格的URL.
以CURD为例:
新增:/order POST
修改:/order/1 PUT =======> update?id=1
获取:/order/1 GET =======> get?id=1
删除:/order/1 DELETE====== delete?id=1
如何在浏览器的页面中发送PUT请求和DELETE请求呢?
1.需要配置HiddenHttpMethodFilter, web.xml
配置
httpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
httpMethodFilter
/*
2.需要发送POST请求 index.jsp
内容,并需要在发送POST请求时携带一个name="_method"
的隐藏域,值为DELETE或PUT
3.写对应的控制器 SpringMVCTest.java
/**
* 比较常用:使用method属性来指定请求方式(post方式)
* @return
*/
@RequestMapping(value="testMethod", method=RequestMethod.POST)
public String testMethod()
{
System.out.println("testMethod");
return SUCCESS;
}
/**
* Rest 测试PUT请求
*/
@RequestMapping(value="/testRest/{id}", method=RequestMethod.PUT )
public String testRestPut(@PathVariable(value="id") Integer newId)
{
System.out.println("testRestPut request id = " + newId);
return SUCCESS;
}
/**
* Rest 测试delete请求
*/
@RequestMapping(value="/testRest/{id}", method=RequestMethod.DELETE)
public String testRestDelete(@PathVariable(value="id") Integer newId)
{
System.out.println("testRestDelete request id = " + newId);
return SUCCESS;
}
/**
* Rest测试post请求
* @return
*/
@RequestMapping(value="/testRest", method=RequestMethod.POST)
public String testRestPost()
{
System.out.println("testRestPost request !");
return SUCCESS;
}
/**
* Rest测试get请求
* @return
*/
@RequestMapping(value="/testRest/{id}", method=RequestMethod.GET)
public String testRestGet(@PathVariable(value="id") Integer newId)
{
System.out.println("testRestGet request id = " + newId);
return SUCCESS;
}
@RequestParam注解映射请求参数到对应的方法入参
- SpringMVCTest.java控制器
/**
* @RequestParam 来映射请求参数
* value 请求参数的参数名
* required 该参数是否必填 默认true
* defaultValue 请求参数默认值
*/
@RequestMapping(value="testRequestPram", method=RequestMethod.GET)
public String testRequestPram(@RequestParam(value="username") String uname,
@RequestParam(value="age",required=false,defaultValue="0" ) int age )
{
System.out.println("testRequestPram 测试请求参数映射到方法的入参,参数为:username=" + uname + ", age = " + age);
return SUCCESS;
}
- 请求测试:
http://localhost:8081/spring-mvc-helloworld/springmvc/testRequestPram?username=lxf&age=21
输出:
testRequestPram 测试请求参数映射到方法的入参,参数为:username=lxf, age = 21
@RequestHeader注解,映射请求头信息到对应方法的入参
- SpringMVCTest.java控制器
/**
* @RequestHeader注解,映射请求头信息到对应方法的入参
* 用法同@RequestParam
*/
@RequestMapping(value="testRequestHeader")
public String testRequestHeader(@RequestHeader(value="Accept-Language") String lang)
{
System.out.println("testRequestHeader测试映射请求头信息到对应方法的入参,使用@RequestHeader注解: " + lang);
return SUCCESS;
}
- 请求测试:
http://localhost:8081/spring-mvc-helloworld/springmvc/testRequestHeader
输出:
testRequestHeader测试映射请求头信息到对应方法的入参,使用@RequestHeader注解: zh-CN,zh;q=0.8,en;q=0.6
使用@CookieValue注解,映射cookie值到对应方法的入参
- SpringMVCTest.java控制器
/**
* 使用@CookieValue注解,映射cookie值到对应方法的入参
* @param cookie
* @return
*/
@RequestMapping(value="testRequestCookieValue")
public String testRequestCookieValue(@CookieValue(value="JSESSIONID") String cookie)
{
System.out.println("testRequestCookieValue测试映射请求cookie值到对应方法的入参,使用@CookieValue注解: sessionId = " + cookie);
return SUCCESS;
}
- 请求测试:
http://localhost:8081/spring-mvc-helloworld/springmvc/testRequestCookieValue
输出:
testRequestCookieValue测试映射请求cookie值到对应方法的入参,使用@CookieValue注解: sessionId = CFAFBF09257FF9ADCB885298827EE57A
使用POJO对象绑定请求参数
1.index.jsp表单
2.新写两个实体类 User.java
和 Address.java
User.java
package lxf.springmvc.entity;
/**
* 用户实体类
* @author lxf
*/
public class User {
private String username;
private String password;
private Address address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", address=" + address + "]";
}
}
Address.java
package lxf.springmvc.entity;
/**
* 地址实体类
* @author lxf
*
*/
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
}
3.SpringMVCTest.java 控制器
/**
* 测试使用Pojo对象绑定前台页面提交的表单数值
* @param user
* @return
*/
@RequestMapping(value="testPojo")
public String testPojo(User user)
{
System.out.println("测试使用Pojo对象绑定前台页面提交的表单数值,User对象= "+user);
return SUCCESS;
}
4.提交表单后:
测试使用Pojo对象绑定前台页面提交的表单数值,User对象= User [username=1, password=123, address=Address [city=2, street=3]]
使用原生Servlet API作为目标方法的参数
SpringMVCTest.java控制器
/**
* 使用原生Servlet API作为目标方法的参数 具体支持以下类型
* HttpServletRequest
* HttpServletResponse
* HttpSession
* java.security.Principal
* Local InputStream
* OutputStream
* Reader
* Writer
*/
@RequestMapping("/testServletAPI")
public void testServletAPI(HttpServletRequest request, HttpServletResponse response, Writer out)
{
System.out.println("testServletAPI + request: " + request + ", response : " + response);
try {
//向浏览器输出
out.write("Hellow springMvc!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
访问测试:
http://localhost:8081/spring-mvc-helloworld/springmvc/testServletAPI
浏览器输出:
Hellow springMvc!
控制台输出:
testServletAPI + request: org.apache.catalina.connector.RequestFacade@6062722, response : org.apache.catalina.connector.ResponseFacade@7df32c44
点击查看演示代码