SpringMVC learning ----- filter parameters to solve the garbage problem

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43213517/article/details/99586632

SpringMVC learning ----- filter parameters to solve the garbage problem

Overview of issues

When the browser submits data to the background, if presented in a way to post way, and the Chinese appear argument, garbled problem occurs.

Form submission

Here Insert Picture Description

 <form action="/test2" method="post">
        姓名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        年龄:<input type="text" name="age"><br>
        <input type="submit" value="提交">
   </form>

When reached controller name field, there will be garbled question

User(username=?°??±?, password=123, age=10)

But if the way to get submitted, they would not Chinese garbled problems.

Resolved through the filter

Configure the filter in the web.xml file to solve the garbage problem.

  <!--配置解决乱码的过滤器-->
    <filter>
        <filter-name>filter</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>filter</filter-name>
        <!--拦截所有的-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Then you can receive the correct parameters.

Guess you like

Origin blog.csdn.net/weixin_43213517/article/details/99586632