乱码问题:
测试步骤:
1.编写一个表单
<form action="/encoding" method="post"> <input type="text" name="username">姓名 <input type="submit"> </form>
2.对应的处理类
@PostMapping("/encoding") public String encoding(@RequestParam("username")String username, Model model){ model.addAttribute("msg",username); return "hello"; }
3.输出中文测试,出现乱码
浣犲ソ
解决:
方式一:
只需要在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>
方式二:
修改tomcat配置文件:设置编码
<Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />