处理提交数据
提交的域名和处理方法的参数名一致
提交: /hello?name=yiqing
处理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class UserController {
//localhost:8080/user/t1?name=xxx;
public String test1(String name, Model model){
// 1. 接收前端参数
System.out.println("receive param: " + name);
// 2. 将返回结果传递给前端
model.addAttribute("msg",name);
// 3. 视图跳转
return "test";
}
}提交的域名和处理方法参数不一致
使用@RequestParam规定值的名称 建议全部加上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class UserController {
//localhost:8080/user/t1?username=xxx;
public String test1({ String name, Model model)
// 1. 接收前端参数
System.out.println("receive param: " + name);
// 2. 将返回结果传递给前端
model.addAttribute("msg",name);
// 3. 视图跳转
return "test";
}
}提交的对象
提交的表单域和对象的属性名一致
不一致就是null
1
2
3
4
5
6//pojo
public class User {
private int id;
private String name;
private int age;
}1
2
3
4
5
6
7
8
9
10
11
12//controller
/**
* 接收前端用户传递的参数,判断参数名,如果参数名在方法上,直接使用
* 假设传递的是一个对象User,将会匹配User对象中的字段名,字段要保持一致
* @param user
* @return
*/
public String test2(User user){
System.out.println(user);
return "test";
}
返回数据
Model: 如处理的1所示
- Model是精简版
- 大部分情况,我们都是使用Model的
ModelMap:
- 继承自LinkedHashMap,拥有LinkedHashMap的全部功能
ModelAndView: 在储存数据的同时,进行设置返回的逻辑视图View,进行控制展示层的跳转(在原始的实现Controller接口的方式中有使用,详见https://yiiiqing.github.io/2020/11/06/SpringMVC/)