Spring MVC 解决form表单提交中文乱码问题

时间:2021-12-27 13:05:14

1. 表单提交controller获得中文参数后乱码解决方案

将jsp页面的设置为utf-8字符编码集

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>

form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果。

<form:form modelAttribute="user" action="user/add" method="post" >

将以下配代码配置到web.xml中,增加编码过滤器。

<filter>
        <filter-name>characterEncodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern> </filter-mapping>

参考其他乱码详细解决