SpringMVC-处理乱码问题【开发碰到确实头大】
【先来个平平无奇的表单jsp页面】
<%--
Created by IntelliJ IDEA.
User: asus
Date: 2021/8/16
Time: 16:19
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="${pageContext.request.contextPath}/encoding/enc" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
【再来个平平无奇的Controller】
test跳转页面仅做读取msg
内容的操作(即除应有结构外,只有一句功能语句${msg}
)
package cn.iris.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/encoding")
/**
* @author Iris 2021/8/16
*/
public class EncodingController {
@PostMapping("/enc")
public String testEnc(String name, Model model) {
model.addAttribute("msg", "name = "+name);
return "test";
}
}
【运行Tomcat,输入中文字符串】
- 后台输出
è???????????????????????
- 前端输出
name = è¿ä»£ä¸å½ç»æµå¶åº¦
看着就异常头疼
【解决掉让人头疼的东西】
- 尝试用Servlet过滤器解决(失败)
- get方法能解决(但是会暴露数据!!!)
- 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>
- 如果以上方法依旧不行【找大佬】