乱码问题产生
测试步骤:
-
在首页中编写一个提交的表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form method="post" action="${pageContext.request.contextPath}/encoding/t1"> <input type="text" name="name"> <input type="submit" value="提交"> </form> </body> </html>
-
后台编写对应的处理类
@Controller @RequestMapping("/encoding") public class EncodingController { @RequestMapping("/t1") public String test01(@RequestParam("name") String name, Model model) { System.out.println(name); model.addAttribute("msg", name); return "test"; } }
-
输入中文测试
解决方案
在web.xml
中配置SpringMVC自带的过滤器即可!
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<!--
/ 表示所有请求,不包含jsp
/* 表示所有请求,包含jsp
-->
<url-pattern>/*</url-pattern>
</filter-mapping>
输入中文测试:
完美解决!