springmvc字符 中文乱码问题

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>

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

处理方法 :

修改tomcat配置文件 :

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

​ 加入:URIEncoding="utf-8"

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

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

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

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

2.json乱码问题

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

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

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

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


解决方法:

2.1 第一种解决方法

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

加入了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>

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>

基本上问题就解决了

个人博客为:

MoYu's Github Blog

MoYu's Gitee Blog

springmvc字符 中文乱码问题的更多相关文章

  1. springmvc返回中文乱码问题

    关于springmvc的返回中文乱码的问题,网上可谓是清一色的一样,无外乎就两种,要么在局部类或这方法上解决,类似如下的代码: @GetMapping(value="/error/query ...

  2. SpringMVC RESTful中文乱码

    开发中常遇到各种中文乱码很少心烦,这里总结了各种中文乱码https://www.cnblogs.com/lwx521/p/9856186.html 下面以SpringMVC遇到的中文乱码为例详解 首先 ...

  3. SpringMVC页面中文乱码

    刚开始学习使用SpringMVC,完成配置之后开始编辑页面源码,添加了几个中文字符(index.jsp) <html> <body> <h2>hello world ...

  4. springmvc解决中文乱码问题

    1 第一种情况(get接收参数): 最近在用solr做一个搜索服务,发布给手机和pc等客户端调用,调用方式为:   http://www.ganbo.search/search?q="手机& ...

  5. springmvc数据处理-中文乱码

    首先解决中文乱码 通过mvc过滤器解决,在web.xml中配置 <filter> <filter-name>CharacterEncodingFilter</filter ...

  6. SpringMVC redirect中文乱码问题

    在使用"redirect:xxx.do?param=中文"时会出现乱码问题,解决方案如下: 使用model.addAttribute来替代直接拼接参数.如下: @RequestMa ...

  7. SpringMVC(二) —— 中文乱码处理

    Get的乱码处理 改tomcat中server.xml中的port=“8080”,加上一个 URIEncoding=”utf-8” 如下图: 2.Post乱码的处理 在web.xml文件中加入 < ...

  8. 解决SpringMVC+Thymeleaf中文乱码

    乱码效果截图 解决办法:在org.thymeleaf.templateresolver.ServletContextTemplateResolver和org.thymeleaf.spring5.vie ...

  9. springmvc StringHttpMessageConverter 中文乱码的几种解决办法(亲测)

    昨天,将一个原来使用JSR 311作为restful实现的测试系统改成了使用spring mvc,最后测试的时候发现输出的json字符串为乱码,从日志可以看出使用的是StringHttpMessage ...

随机推荐

  1. 南阳ccpc C题 The Battle of Chibi 树状数组+dp

    题目: Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried ab ...

  2. CodeForces - 449B 最短路(迪杰斯特拉+堆优化)判断最短路路径数

    题意: 给出n个点m条公路k条铁路. 接下来m行 u v w      //u->v 距离w 然后k行 v w         //1->v 距离w 如果修建了铁路并不影响两点的最短距离, ...

  3. 牛客编程巅峰赛S1第3场 - 青铜&白银 A.位数求和

    题意:求所有\(n\)位数每位之和等于\(m\)的数的和. 题解:数据范围非常小,我们可以直接暴力枚举\(t\)到\(10*t\)的所有数字,逐位分解判断即可. 代码: class Solution ...

  4. Linux core dump使用

    什么是 core dump? core dump是一个当进程意外终止时包含进程内存内容的文件.当程序崩溃的时候,core dump由kernel触发.core dump可以作为程序崩溃时的事后快照(p ...

  5. RuntimeError already started

    Env: os: Ubuntu python3 pytorch vscode Desc 在上述环境中运行A3C多进程模型,使用命令行时没问题,使用vscode时出现 'RuntimeError: al ...

  6. OpenStack Train版-2.安装keystone身份认证服务

    安装 keystone 认证 mysql -uroot create database keystone; grant all privileges on keystone.* to 'keyston ...

  7. LeetCode 856. Score of Parentheses 括号的分数

    其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...

  8. scu-4440 rectangle (非原创)

    Rectangle frog has a piece of paper divided into nn rows and mm columns. Today, she would like to dr ...

  9. Celery&Flower文档笔记

    1.Celery # tasks.py from celery import Celery app = Celery('tasks', broker='redis://localhost:6379', ...

  10. Netty(二)Netty 与 NIO 之前世今生

    2.1 Java NIO 三件套 在 NIO 中有几个核心对象需要掌握:缓冲区(Buffer).选择器(Selector).通道(Channel). 2.1.1 缓冲区 Buffer 1.Buffer ...