以前我们解决乱码是:写一个过滤器然后去xml配置所有路径拦截
现在springmvc不用这么麻烦直接web.xml配置乱码交给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>```
全部源码:
这是我自己写的表单:
```javascript
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/9/5
Time: 13:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/post" method="post">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
测试类:
package com.zsp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class ControllerTest4 {
@PostMapping("/post")
public String test(String username, Model model){
model.addAttribute("msg",username);
return "test";
}
}
跳转的页面:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/9/5
Time: 0:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>