张小驰出没 张小驰出没
首页
  • Spring合集
  • SpringMVC合集
  • Mybatis合集
  • Spring Boot合集
  • Mybatis-plus合集
  • Spring Security合集
  • Vue合集
  • Redis合集
  • RabbitMQ合集
  • 数据结构

    • 数据结构
  • 算法解析

    • 日常算法
    • 剑指Offer
    • LeetCode
  • 技术与Bug

    • 技术文档
    • Bug解决方法
  • 个人博客

    • Hexo博客搭建
  • 我的项目
  • 我的面试
  • 分类
  • 标签
  • 归档
友链
关于
Hexo
GitHub

MoYu

何德何能,何其荣幸
首页
  • Spring合集
  • SpringMVC合集
  • Mybatis合集
  • Spring Boot合集
  • Mybatis-plus合集
  • Spring Security合集
  • Vue合集
  • Redis合集
  • RabbitMQ合集
  • 数据结构

    • 数据结构
  • 算法解析

    • 日常算法
    • 剑指Offer
    • LeetCode
  • 技术与Bug

    • 技术文档
    • Bug解决方法
  • 个人博客

    • Hexo博客搭建
  • 我的项目
  • 我的面试
  • 分类
  • 标签
  • 归档
友链
关于
Hexo
GitHub
  • springmvc字符中文乱码问题
    • 1.字符过滤器
    • 2.json乱码问题
      • 2.1 第一种解决方法
      • 2.2 第二种解决方法
  • 2字节的UTF-8序列的字节2无效解决方案
  • source is null for getProperty(null, "name")
  • 使用yum出现Loaded plugins:fastestmirror
  • FastDFS依赖无法导入
  • Could not autowire. No beans of 'xxx' type found
  • 《Bug解决》笔记
MoYu
2021-06-20

springmvc字符中文乱码问题

# springmvc字符 中文乱码问题

# 1.字符过滤器

输入中文测试,发现乱码

1

以前乱码问题通过过滤器解决 , 而SpringMVC给我们提供了一个过滤器 , 可以在web.xml中配置,修改了xml文件需要重启服务器。

springmvc未设置字符过滤器,获取的信息,在前端页面显示的中文都是中文乱码。

解决方法:在web.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>
    <init-param>
         <param-name>forceEncoding</param-name>
         <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

但是发现 , 有些极端情况下.这个过滤器对get的支持不好 .

处理方法 :

修改tomcat配置文件 :

​ 文件位置:tomcat文件夹---conf---server.xml

​ 加入:URIEncoding="utf-8"

<Connectorport="8080" protocol="HTTP/1.1"
          connectionTimeout="20000"
          redirectPort="8443" URIEncoding="utf-8"/>
1
2
3

一般情况下,SpringMVC默认的乱码处理就已经能够很好的解决了!

乱码问题,需要平时多注意,在尽可能能设置编码的地方,都设置为统一编码 UTF-8

这种过滤器对大部分中文乱码都有用了,但是还有一种情况为json中文乱码

# 2.json乱码问题

中文变成????,这种情况常出现在:Controller类 使用了 @ResponseBody

​ @ResponseBody注解的作用是将controller的方法返回的对象 通过适当的转换器 转换为指定的格式之后,写入到response对象的body区(响应体中),通常用来返回JSON数据或者是XML。

​ 数据,需要注意的呢,在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,它的效果等同于通过response对象输出指定格式的数据。

​ 这里还要着重强调一下,要通过@ResponseBody 注解 将返回的json字符串放入响应体中,然后在前台js才能拿到json字符串进行解析,如果不加,响应体中就没有放入json字符串,前台自然是拿不到数据的,希望大家别理解错。


4

解决方法:

# 2.1 第一种解决方法

@RequestMapping(value = "/xxx",produces = "application/json;charset=utf-8")
1

加入了produces = "application/json;charset=utf-8"

这种方法一般就可以解决问题了,真不行就下面这种

# 2.2 第二种解决方法

上一种方法比较麻烦,如果项目中有许多请求则每一个都要添加,可以通过Spring配置统一指定,这样就不用每次都去处理了

1.导入依赖:

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.9</version>
</dependency>
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9</version>
</dependency>
1
2
3
4
5
6
7
8
9
10

2.在springmvc.xml中配置

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

基本上问题就解决了

#Bug
上次更新: 2021/06/20, 09:51:53
2字节的UTF-8序列的字节2无效解决方案

2字节的UTF-8序列的字节2无效解决方案→

最近更新
01
链表
01-25
02
约瑟夫问题
01-25
03
快慢指针
01-25
更多文章>
Theme by Vdoing | Copyright © 2021-2022 MoYu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式