RequestParam
- 作用:把请求中指定名称的参数赋值给控制器中的形参。
- 常用属性:
属性 | 功能 |
---|---|
value | 请求参数中的名称赋值给方法形参 与name属性互为别名 |
name | 请求参数中的名称赋值给方法形参 与value属性互为别名 |
required | 请求参数中是否必须传入此参数。默认值:true,表示必须提供,如果不提供将报错 |
示例代码
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--requestParams注解的使用-->
<a href="springmvc/useRequestParam?name=test">测试 requestParam注解</a>
</body>
</html>
编写Controller代码
package com.liang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {
@RequestMapping(path = "/useRequestParam")
public String useRequestParam(@RequestParam(name = "name") String username, @RequestParam(value = "age", required = false) Integer age){
System.out.println("执行了useRequestParam方法..."+username+" : "+age);
return "success";
}
}
RequestBody
- 作用:用于获取请求体内容,直接使用得到key=value&key=value……结构的数据。get请求方式不适用。
- 常用属性:
属性 | 功能 |
---|---|
required | 是否必须有请求体,默认值:true,此时get请求方式会报错,若值为false时,get请求为null |
示例代码
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--RequestBody注解的使用-->
<!--post请求方式-->
<form action="springmvc/useRequestBody" method="post">
用户名称:<input type="text" name="username"/><br/>
用户密码:<input type="password" name="password"/><br/>
用户年龄:<input type="text" name="age"/><br/>
<input type="submit" value="保存"/>
</form>
<!--get请求方式-->
<a href="springmvc/useRequestBody?body=test">测试 RequestBody 注解 get请求</a>
</body>
</html>
编写Controller代码
package com.liang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {
@RequestMapping(path = "/useRequestBody")
public String useRequestBody(@RequestBody(required = false) String body){
System.out.println("执行了useRequestBody方法..."+body);
return "success";
}
}
PathVaribale
- 作用:用于绑定URL中的占位符,Spring3.0后加入了url支持占位符
- 常用属性:
属性 | 功能 |
---|---|
value | 用于指定url中占位符名称 |
required | 是否必须提供占位符 |
示例代码
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--PathVariable注解-->
<a href="springmvc/usePathVariable/100/200">PathVariable注解</a>
</body>
</html>
编写Controller代码
package com.liang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {
@RequestMapping(path = "/usePathVariable/{id}/{i}")
public String usePathVariable(@PathVariable("id") Integer id , @PathVariable("i") Integer i){
System.out.println("执行了usePathVariable方法... id=" + id + " , i="+i);
return "success";
}
}
RequestHeader
- 作用:用于获取请求消息头
- 常用属性:
属性 | 功能 |
---|---|
value/name | 需要获取到的消息头名称 |
required | 是否必须有此消息头 |
示例代码
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--RequestHeader注解-->
<a href="springmvc/useRequestHeader">获取请求消息头</a>
</body>
</html>
编写Controller代码
package com.liang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {
@RequestMapping(path = "/useRequestHeader")
public String useRequestHeader(@RequestHeader(name = "Accept-Language", required = false) String requestHeader){
System.out.println("执行了useRequestHeader方法... "+requestHeader );
return "success";
}
}
CookieValue
- 作用:用于把指定cookie名称的值传入控制器方法参数中。
- 常用属性
属性 | 功能 |
---|---|
value | 指定cookie的名称 |
required | 是否必须有此cookie |
示例代码
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--CookieValue注解-->
<a href="springmvc/useCookieValue">获取cookie的值</a>
</body>
</html>
编写Controller代码
package com.liang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {
@RequestMapping(path = "/useCookieValue")
public String useCookieValue(@CookieValue(value = "JSESSIONID", required = false) String cookieValue){
System.out.println("执行了useCookieValue方法... " + cookieValue );
return "success";
}
}