1.处理提交数据
1.1提交的域名和处理方法的参数名一致
提交数据:http://localhost:8080/user/t1?name=小刘
代码:
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/t1")
public String test1(String name, Model model){
//1.接收前端参数
System.out.println("接收到前端参数为:"+name);
//2.将返回的结果传递给前端(让model接收)
model.addAttribute("msg",name);
//3.跳转视图
return "test";
}
}
后台输出:小刘
1.2提交的域名和处理方法的参数名不一致
提交数据:http://localhost:8080/user/t1?username=小刘
代码:
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/t1")
public String test1(@RequestParam("username") String name, Model model){
//1.接收前端参数
System.out.println("接收到前端参数为:"+name);
//2.将返回的结果传递给前端(让model接收)
model.addAttribute("msg",name);
//3.跳转视图
return "test";
}
}
后台输出:小刘
1.3提交的是一个对象
要求提交的表单域名和对象的属性名一致,参数使用对象即可
- 实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private int age;
}
-
提交数据:http://localhost:8080/user/t2?id=1&name=小刘&age=2
-
代码:
@Controller
@RequestMapping("/user")
public class UserController {
//前端接收的是一个对象: id, name, age
/*
* 1.接收前端用户传递的参数,判断参数的名字,假设名字直接在方法上,可以直接使用
* 2.假设传递的是一个对象User,匹配User对象中的字段名;如果名字一致则可行,否则匹配不到
* */
@GetMapping("/t2")
public String test2(User user){
System.out.println(user);
return "test";
}
}
后台输出:User(id=1, name=小刘, age=2)
注意:如果使用对象,前端传递的参数名和对象名必须一致,否则输出null。
2.数据显示到前端
2.1通过ModelAndView
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
//业务代码
String result = "HelloSpringMVC";
mv.addObject("msg",result);
//视图跳转
mv.setViewName("test");
return mv;
}
}
2.2通过Model
@Controller
@RequestMapping("/user")
public class UserController {
//localhost:8080/user/t1?name=xxx;
@GetMapping("/t1")
public String test1(@RequestParam("username") String name, Model model){
//1.接收前端参数
System.out.println("接收到前端参数为:"+name);
//2.将返回的结果传递给前端(让model接收)
model.addAttribute("msg",name);
//3.跳转视图
return "test";
}
2.3通过ModelMap
@Controller
@RequestMapping("/user")
public class UserController {
//localhost:8080/user/t1?name=xxx;
@GetMapping("/t1")
public String test1(@RequestParam("username") String name, ModelMap model){
//1.接收前端参数
System.out.println("接收到前端参数为:"+name);
//2.将返回的结果传递给前端(让model接收)
model.addAttribute("msg",name);
//3.跳转视图
return "test";
}
3.对比
- Model只有几个方法只适合用于存储数据,简化了新手对于Model对象的操作和理解
- ModelMap继承了LinkedMap,除了实现了自身的一些方法,同样的继承LinkeMap的方法和特性
- ModelAndView在存储数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转
4.乱码问题
进行测试:
- 在web文件夹下新建form.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
- 新建处理类EncodingController.class
@Controller
public class EncodingController {
@PostMapping("/e/t1")
public String test1(String name, Model model){
model.addAttribute("msg",name);
return "test";
}
}
- 输入中文测试,发现乱码
以前乱码问题通过过滤器解决,而SpringMVC提供了一个过滤器,可以在web.xml中配置。
修改了xml文件需要重启服务器
<!--2.配置SpringMVC的乱码过滤器-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果依旧不能解决乱码:
- 修改D:\environment\apache-tomcat-8.0.38\conf目录下的server.xml中的编码:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
- 找大佬配置的自定义过滤器