本文详细归纳了SpringMVC中控制器可以接收的参数,下面会逐个列出,并给出代码示例
@RequestMapping("/test01.action")
public void test01(HttpServletRequest request, HttpServletResponse response, HttpSession session, WebRequest wreq){
//--request
request.getParameter("username");
request.getHeader("Host");
//--response
response.setContentType("text/html;charset=utf-8");
//--session
session.setAttribute("k1","v1");
//--WebRequest
wreq.getParameter("username");
wreq.getHeader("Host");
wreq.getSessionId();
}
注意!
不能同时获取InputStream和Reader
不能同时获取OutputStream和Writer
@RequestMapping("/test02.action")
public void test02(OutputStream out) throws IOException {
out.write("abcdef".getBytes());
}
测试结果如下
此三类对象都是模型对象,用来封装模型数据,唯一的不同在其api有所区别
@RequestMapping("/test03.action")
public String test03(Model model, Map map, ModelMap modelMap){
model.addAttribute("k1","v1");
model.addAttribute("k2","v2");
map.put("k3","v3");
map.get("k3");
modelMap.addAttribute("k4","v4");
modelMap.put("k5","v5");
modelMap.get("k5");
return "my01test03";
}
41.传统方式获取
@RequestMapping("/test01.action")
public void test01(HttpServletRequest request){
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
System.out.println(username);
System.out.println(age);
}
可以通过浏览器地址栏传参获取,测试结果如下
如:http://localhost/SpringMVCDay02_01_accpetParam_Web_exploded/my02/test01.action?username=zs&age=18
4.2直接获取方式一
SpringMVC中可以直接获取,但是要注意:请求参数名和方法参数名一致
测试方法同上,这里不再给出
@RequestMapping("/test02.action")
public void test02(String username,int age){
System.out.println(username);
System.out.println(age);
}
4.3直接获取方式二
上面4.2方式要求参数名和方法参数名必须一致,如果请求参数名和方法参数名不一致时可以通过@RequestParam注解指定参数即可获取
@RequestMapping("/test03.action")
public void test03(@RequestParam("username") String uname,@RequestParam("age") int uage){
System.out.println(uname);
System.out.println(uage);
}
4.4多个同名请求参数
4.4.1方式一:用字符串接收,则,获取到多个值用逗号拼接的结果
@RequestMapping("/test04.action")
public void test04(String username,int age,String like){
System.out.println(like);
}
测试通过地址栏拼接多个同名请求参数
http://localhost/SpringMVCDay02_01_accpetParam_Web_exploded/my02/test04.action?username=zs&age=18&like=zq&like=pq&like=qq
测试结果如下
4.4.2方式二:用字符串数组接受,则,获取到多个值组成的数组
@RequestMapping("/test04.action")
public void test04(String username,int age,String [] like){
System.out.println(Arrays.asList(like));
}
测试结果为
4.5封装请求参数到bean
可以直接在控制器方法上接收一个bean,springmvc自动将请求参数封装到bean的同名属性中
4.5.1情况一:
User实体类如下:
public class User {
private String username;
private int age;
private String addr;
public User() {
}
public User(String username, int age, String addr) {
this.username = username;
this.age = age;
this.addr = addr;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", age=" + age +
", addr='" + addr + '\'' +
'}';
}
}
控制器中直接获取User对象
@RequestMapping("/test05.action")
public void test05(User user){
System.out.println(user);
}
通过地址栏传参测试
http://localhost/SpringMVCDay02_01_accpetParam_Web_exploded/my02/test05.action?username=zs&age=19&addr=bj
测试结果如下:
4.5.2情况二
复杂类型封装请求参数到bean,如下User2中除了基本的数据类型,还有引用数据类型Dog
public class User2 {
private String username;
private int age;
private String addr;
private Dog dog;
public User2() {
}
public User2(String username, int age, String addr, Dog dog) {
this.username = username;
this.age = age;
this.addr = addr;
this.dog = dog;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "User2{" +
"username='" + username + '\'' +
", age=" + age +
", addr='" + addr + '\'' +
", dog=" + dog +
'}';
}
}
控制器中的代码:
@RequestMapping("/test06.action")
public void test06(User2 user2){
System.out.println(user2);
}
通过地址栏传参测试
http://localhost/SpringMVCDay02_01_accpetParam_Web_exploded/my02/test06.action?username=zs&age=19&addr=bj&dog.name=wc&dog.age=3
测试结果如下:
四大域属性中:
- page(pageContext)域SpringMVC中Controller没什么关联,因为它只是在jsp页面起作用
- ServletContext 在SpringMvc中没有便捷方式
5.1Request域属性 - 传统方式
//先执行下面,设置好值,然后在通过传统的方式获取
@RequestMapping("/test01.action")
public void test01(HttpServletRequest request){
String v1 = (String) request.getAttribute("k1");
String v2 = (String) request.getAttribute("k2");
System.out.println(v1);
System.out.println(v2);
}
///
@RequestMapping("/test00.action")
public void test00(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
// request.setAttribute("k1","v1");
// request.setAttribute("k2","v2");
// request.getRequestDispatcher("/my03/test01.action").forward(request,response);
// request.setAttribute("k1","v1");
// request.setAttribute("k2","v2");
// request.getRequestDispatcher("/my03/test02.action").forward(request,response);
// session.setAttribute("sk1","sv1");
// session.setAttribute("sk2","sv2");
// request.getRequestDispatcher("/my03/test03.action").forward(request,response);
session.setAttribute("sk1","sv1");
session.setAttribute("sk2","sv2");
request.getRequestDispatcher("/my03/test04.action").forward(request,response);
}
5.2通过SpringMVC方式获取Request域属性
@RequestMapping("/test02.action")
public void test02(@RequestAttribute("k1") String k1,@RequestAttribute("k2") String k2){
System.out.println(k1);
System.out.println(k2);
}
5.3Session域属性 - 传统方式获取
@RequestMapping("/test03.action")
public void test03(HttpSession session){
String sk1 = (String) session.getAttribute("sk1");
String sk2 = (String) session.getAttribute("sk2");
System.out.println(sk1);
System.out.println(sk2);
}
5.4Session域属性 - SpringMVC方式获取
@RequestMapping("/test04.action")
public void test04(@SessionAttribute("sk1") String sk1,@SessionAttribute("sk2") String sk2){
System.out.println(sk1);
System.out.println(sk2);
}
获取的方式有两种,获取前可以通过@ModelAttribute将值存入到model
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@Component
@RequestMapping("/my04")
public class MyController04 {
@ModelAttribute("mk1")
public String mx(){
System.out.println("mx...");
return "abc";
}
/**
* 控制器方法可以接受的参数6 - 模型数据
* http://localhost/SpringMVCDay02_01_accpetParam/my04/test02.action
*/
@RequestMapping("/test02.action")
public void test02(@ModelAttribute("mk1") String mk1){
System.out.println(mk1);
}
/**
* 控制器方法可以接受的参数6 - 模型数据
* http://localhost/SpringMVCDay02_01_accpetParam/my04/test01.action
*/
@RequestMapping("/test01.action")
public void test01(Model model){
System.out.println(model.asMap());
}
}
测试结果如下
7.1Cookie值 - 传统方式
@RequestMapping("/test01.action")
public void test01(HttpServletRequest request){
Cookie[] cs = request.getCookies();
Cookie findC = null;
if(cs!=null){
for(Cookie c : cs){
if("JSESSIONID".equals(c.getName())){
findC = c;
break;
}
}
}
if(findC!=null){
String v = findC.getValue();
System.out.println(v);
}
}
7.2Cookie值 - SpringMVC方式
@RequestMapping("/test02.action")
public void test02(@CookieValue("JSESSIONID") String JSESSIONID){
System.out.println(JSESSIONID);
}
7.3Header值 - 传统方式
@RequestMapping("/test03.action")
public void test03(HttpServletRequest request){
String value = request.getHeader("User-Agent");
System.out.println(value);
}
7.4Header值 - SpringMVC方式
@RequestMapping("/test04.action")
public void test04(@RequestHeader("User-Agent") String userAgent){
System.out.println(userAgent);
}
7.5路径参数获取
RESTFul风格的请求
/**
* 控制器方法可以接受的参数9 - 路径参数获取
* http://localhost/SpringMVCDay02_01_accpetParam/my05/test05/zs/19.action
* http://localhost/SpringMVCDay02_01_accpetParam/my05/test05/ls/23.action
* http://localhost/SpringMVCDay02_01_accpetParam/my05/test05/ww/31.action
*/
@RequestMapping("/test05/{uname}/{uage}.action")
public void test05(@PathVariable("uname") String uname,@PathVariable("uage") int uage){
System.out.println(uname);
System.out.println(uage);
}
测试结果如下:
7.6获取请求体
/**
* 控制器方法可以接受的参数10 - 获取请求体
* 用字符串接收可以将请求体以一个字符串来接收
* 如果请求体中是一个json数据,则可以用一个bean来接收,SpringMVC会自动将json中的数据设置到bean的对应属性上
* http://localhost/SpringMVCDay02_01_accpetParam/my05test06.jsp
*/
@RequestMapping("/test06.action")
public void test06(@RequestBody User user){
System.out.println(user);
}
前端页面,数据通过ajax 发送
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
my05test06.jsp
测试结果: