Spring MVC解决中文乱码问题

一、设置前端页面编码

JSP页面

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %>

HTML页面

<meta charset="UTF-8">

二、设置服务器编码

打开apache-tomcat-10.1.12/conf/server.xml文件,在如下标签添加URIEncoding="UTF-8"属性,这样便可解决请求行传值时的乱码问题。

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxParameterCount="1000" URIEncoding="UTF-8"/>

三、设置Spring MVC编码

在web.xml文件中配置Spring MVC编码过滤器的编码方式

<filter>
    <filter-name>EncodingFilterfilter-name>
    
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    
    <init-param>
        <param-name>encodingparam-name>
        <param-value>UTF-8param-value>
    init-param>
filter>
<filter-mapping>
    <filter-name>EncodingFilterfilter-name>
    
    <url-pattern>/*url-pattern>
filter-mapping>
public class CharacterEncodingFilter extends OncePerRequestFilter {
    @Nullable
    private String encoding;
    private boolean forceRequestEncoding;
    private boolean forceResponseEncoding;
    // ......
}

你可能感兴趣的:(Spring,MVC,spring,mvc,java)