乱码问题的解决

我们从前台页面传递参数到后台,经常会遇到乱码问题!!

例子:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--<form action="http://localhost:8080/SpringMVC_05/e/t" method="post">--%>

<form action="${pageContext.request.contextPath}/e/t" method="post">
    <input type="text" name="name">
    <input type="submit">
</form>

</body>
</html>
    @PostMapping("/e/t")
    public String test3(  String name, Model model){
        model.addAttribute("msg",name);
        return "test1";
    }

测试结果:

 解决办法如下:

不得不说,乱码问题是在我们开发中十分常见的问题,也是让我们程序猿比较头大的问题!

以前乱码问题通过过滤器解决 , 而SpringMVC给我们提供了一个过滤器 , 可以在web.xml中配置 .

修改了xml文件需要重启服务器!

<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>

posted @ 2021-12-05 19:27  qwedfrgh  阅读(64)  评论(0)    收藏  举报