SpringMVC参数传递

基本数据类型参数

  • 默认保证参数名称和请求中传递的参数名相同

image-20191222100545276

image-20191222100851694

image-20191222101739578

  • 如果请求参数名和方法参数名不对应使用@RequestParam注解中的value值进行匹配赋值(都适用)

image-20191222103513982

image-20191222101135213

image-20191222102226081

  • 如果方法参数是基本数据类型(不是封装类)可以通过@RequestParam中的defaultValue值设置参数默认值,可以防止没有参数时 500错误

image-20191222102123182

image-20191222103435138.png

image-20191222104435172

  • 通过@RequestParam注解中的required的强制要求必须某个参数值不能为空

image-20191222104755445

image-20191222112218115

image-20191222161241614

对象数据类型

  • 请求参数为对象时,前台传递的参数名与对象的属性名一致,且生成get与set方法

image-20191222191243719

image-20191222191444199

image-20191222192448828

image-20191222192353831

  • 请求参数为对象.属性格式

image-20191222194219399

image-20191222194330055

image-20191222194617047

image-20191222195147735

集合对象类型参数

image-20191222201645161

image-20191222201915592

image-20191222202052120

image-20191222203305567

多个同名参数

复选框传递的参数就是多个同名参数

image-20191222202251310

image-20191222210311858

image-20191222203808538

restful 传值方式

  • 在@RequestMapping 中一定要和请求格式对应 :/控制器名/{名称}/{名称}

  • {名称} 中名称可以自定义名称

  • @PathVariable 获取@RequestMapping 中内容,默认按照方法参数名称去寻找.

image-20191222204053578

image-20191222205141021

image-20191222210536164

跳转方式

SpringMVC中默认的跳转方式是请求转发

设置返回值字符串内容

  • 重定向:redirect:资源路径
  • 请求转发:forward:资源路径(省略不写时默认)

SpringMVC中重定向的路径问题

  • 当使用不以/开头的资源路径时,依旧是在相对当前路径进行跳转
  • 使用/开头的资源路径时,SringMVC会默认在前加上contextPath即加上当前项目名

@ResponseBody

作用:

该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

注意事项:

  • 如果返回值满足 key-value 形式(对象或 map)

    1. 自动把响应头设置为 application/json;charset=utf-8
    2. 把对象转化为json串然后写到客户端。按utf-8编码
    3. 底层使用Jackson进行json转换,在项目中一定要导入jackson的jar
  • 如果返回值不满足 key-value,例如返回值为 String

    1. 把响应头设置为 text/html

    2. 把方法返回值以流的形式直接输出.默认按iso8859-1编码。如果返回值包含中文,出现中文乱码

    3. 注解中我们可以手动修改编码格式,例如@RequestMapping(value=”/demo”,produces=”text/html;charset=utf-8”),前面是请求的路径,后面是编码格式。

@ResponseBody这个注解通常使用在控制层(controller)的方法上,其作用是将方法的返回值以特定的格式写入到response的body区域,进而将数据返回给客户端。当方法上面没有写ResponseBody,底层会将方法的返回值封装为ModelAndView对象。也就是说当不写@ResponseBody注解时,单独使用@ResquestMapping都会讲方法的返回值看成是一个视图对象从而进行返回。

image-20191223091731708

image-20191223095629079

image-20191223095744315

image-20191223095858827

image-20191223095943966

image-20191223100059091

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
main.jsp
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="demo1" method="get">
<input type="text" name="name" placeholder="demo1">
<input type="text" name="age">
<input type="submit" value="提交">
</form>

<form action="demo2" method="post">
<input type="text" name="name" placeholder="demo2">
<input type="text" name="age">
<input type="submit" value="提交">
</form>

<form action="demo3" method="post">
<input type="text" name="name1" placeholder="demo3">
<input type="text" name="age1">
<input type="submit" value="提交">
</form>

<form action="demo4" method="post">
<input type="text" name="name" placeholder="demo4">
<input type="text" name="age">
<input type="submit" value="提交">
</form>

<form action="demo5" method="post">
<input type="text" name="name" placeholder="demo5">
<input type="text" name="age">
<input type="checkbox" name="hover" value="学习">
<input type="checkbox" name="hover" value="写代码">
<input type="checkbox" name="hover" value="看视频">
<input type="checkbox" name="hover" value="看笔记">
<input type="submit" value="提交">
</form>

<form action="demo6" method="post">
<input type="text" name="peo.name" placeholder="demo6">
<input type="text" name="peo.age" >
<input type="submit" value="提交">
</form>


<form action="demo7" method="post">
<input type="text" name="peo[0].name" placeholder="demo7">
<input type="text" name="peo[0].age" >
<input type="text" name="peo[1].name" placeholder="demo7">
<input type="text" name="peo[1].age" >
<input type="submit" value="提交">
</form>

<form action="page" method="post">
<input type="text" name="pageSize" placeholder="page">
<input type="text" name="pageNumber">
<input type="submit" value="提交">
</form>

<a href="demo8/zs/121">跳转</a>

</body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.kylin.controller;

import com.kylin.pojo.Demo;
import com.kylin.pojo.Demo1;
import com.kylin.pojo.People;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class DemoController {

//根据请求注解里面的值调用下面的方法
@RequestMapping("/demo1")
//接收请求数据参数为基本数据类型
//跟属性名同名.数据类型你想接收什么类型自己设定
public String demo1(String name, int age) {
System.out.println("执行demo1" +" "+ name + " " + age);
return "main.jsp";
}


@RequestMapping("/demo2")
//接收请求数据参数为对象
public String demo2(People people, String name, String age) {
System.out.println("执行demo2" + people + " " + name + " " + age);
return "main.jsp";
}

@RequestMapping("/demo3")
//接收请求数据参数名与方法参数名不统一。利用@RequestParam注解
public String demo3(@RequestParam("name1") String name, @RequestParam("age1") String age) {
System.out.println(name + " " + age);
return "main.jsp";
}

@RequestMapping("/demo4")
//required = true 强制参数不能为空,为空则报错。
public String demo4(@RequestParam(required = true) String name,int age) {
System.out.println("name是SQL的查询条件,必须要传递name参数" + name);
return "main.jsp";
}


@RequestMapping("/demo5")
//请求参数中包含多个同名参数的获取方式
public String demo5(String name, int age, @RequestParam("hover") List<String> hover) {
System.out.println(name + " " + age + " " + hover);
return "main.jsp";
}

@RequestMapping("/demo6")
//请求参数中对象.属性格式
public String demo6(Demo demo) {
System.out.println(demo);
return "main.jsp";
}

@RequestMapping("/demo7")
//在请求参数中传递集合对象类型参数
public String demo7(Demo1 demo1) {
System.out.println(demo1);
// demo1.getPeo().get(1).getName();
return "main.jsp";
}

//restful传值方式
// /控制器名/{名称}/{名称}
@RequestMapping("/demo8/{name}/{age}")
public String demo8(@PathVariable String name, @PathVariable("age") String age1) {
System.out.println(name + " " + age1);
return "/main.jsp";
}

//重定向跳转方式
@RequestMapping("/demo9")
public String demo9() {
System.out.println("重定向");
// 以/默认加上contextPath
return "redirect:/main";
}

@RequestMapping("/page")
//利用@RequestParam注解defaultValue属性设定当方法的基本数据类型参数值为空时,默认值为..
public String page(@RequestParam(defaultValue = "2") int pageSize, @RequestParam(defaultValue = "1") int pageNumber) {
System.out.println(pageSize + " " + pageNumber);
return "main.jsp";
}

@RequestMapping("/demo10")
@ResponseBody
public People demo10() {
People people = new People();
people.setName("张三");
people.setAge(18);
return people;
}

@RequestMapping(value = "/demo11", produces = "text/html;charset=utf-8")
@ResponseBody
public String demo11() {
return "中文";
}
}