Spring+MyBatis实践—中文乱码
多种中文乱码问题及解决:
- jsp页面乱码
- 表单提交乱码
- URL中文参数乱码
1、jsp页面乱码解决(2步);
新建jsp页面;
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body></body> </html>
- 习惯上,设置工程中设置项目默认编码为“UTF-8“;
- 将新建的jsp页面的三处编码全部替换为与工程编码一致的“UTF-8”,其中,contentType中的charset是指服务器发送给客户端时的内容编码,pageEncoding是jsp文件本身的编码。
2、表单提交乱码解决(3步);
表单采用POST方法进行数据提交;
<form action="./submitBlog" method="post"> <div class="row"><div class="col-lg-1"><h4>标题</h4></div><div class="col-lg-11"><input type="text" class="form-control" placeholder="标题" id="title" name="blogTitle"></div> </div> <br> <textarea class="form-control" rows="20" id="content" name="content"></textarea> <br> <div style="float:right"> <button type="button" class="btn btn-success" id="submit_btn">提交</button> <button type="button" class="btn btn-info" id="save_btn">保存</button> </div> </form>
对应的Controller中的处理方法;
1 @RequestMapping(value="/submitBlog", method=RequestMethod.POST) 2 public String submitBlog(HttpServletRequest request, Blog blog){ 3 //System.out.println("ContentType: "+request.getContentType()); 4 //System.out.println("Request请求的编码格式为:"+request.getCharacterEncoding()); 5 6 /*请求内容自动注入到blog中,并且能够正常显示*/ 7 8 return "redirect:personalSpace"; 9 } 10 未进行第三步之前,通过request.getCharacterEncoding()查看的HttpServletRequest的默认编码为null;
web.xml中设置过滤器,对每个URL请求进行编码转换;
<!-- 使用Filter进行编码转换 --><filter><filter-name>encodingFilter</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>encodingFilter</filter-name><url-pattern>/*</url-pattern> <!-- 不要使用/,使用/*代替才能够真正起作用,还需要配置在ContextLoaderListener后才能起作用 --></filter-mapping>
设置了针对请求的编码转换的Filter之后,再次通过request.getCharacterEncoding()查看HttpServletRequest的编码为"UTF-8";
查看CharacterEncodingFilter类的源码,可以看到;
1 public class CharacterEncodingFilter extends OncePerRequestFilter { 2 /*设置Request请求要转换为的编码*/ 3 private String encoding; 4 /*设置是否强制对Request和Response内容转码*/ 5 private boolean forceEncoding = false; 6 /*其他方法*/ 7 @Override 8 protected void doFilterInternal( 9 HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 10 throws ServletException, IOException { 11 12 if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { 13 request.setCharacterEncoding(this.encoding); 14 if (this.forceEncoding) { 15 response.setCharacterEncoding(this.encoding); 16 } 17 } 18 filterChain.doFilter(request, response); 19 } 20 }
CharacterEncodingFilter的doFilterInternal方法由它的父类OncePerRequestFilter的doFilter方法进行调用,对HttpServletRequest、HttpServletResponse进行了编码转换。
3、URL中的中文参数注入到Controller对应方法的形参时乱码;
问题:
<a href="./editBlog?blogTitle=${blog.blogTitle}" style="color:black"><span class="glyphicon glyphicon-pencil"></span></a>
或
$(".delete").click(function(){var url = "./deleteBlog?blogTitle="+$(this).attr("id"); //属性id中含中文var temp = $(this).parents(".panel");//被删除的元素需要预先保留,否则已经删除的元素是无法获取其父元素的。 $.post(url, function(data, status){if(status=="success"){temp.fadeOut("slow");}}); });
通过上述两种方式提交的中文注入到Controller的对应方法时出现乱码。
解决方案:
1 @RequestMapping("/editBlog") 2 public String editBlog(HttpServletRequest request,@RequestParam("blogTitle")String blogTitle){ 3 try { 4 blogTitle = new String(blogTitle.getBytes("ISO-8859-1"), "UTF-8"); //需要进行转码 5 } catch (UnsupportedEncodingException e) { 6 System.out.println("编码转换出错了!!!"); 7 } 8 /*其他处理*/ 9 }
不知为啥,注入到URL处理方法形参中的字符串采用ISO-8859-1编码,不过进行编码转换后,就正常了。
转载于:https://www.cnblogs.com/-crazysnail/p/3926815.html
Spring+MyBatis实践—中文乱码相关推荐
- 解决Spring框架下中文乱码的问题
解决Spring框架下中文乱码的问题 参考文章: (1)解决Spring框架下中文乱码的问题 (2)https://www.cnblogs.com/Summer7C/p/4712818.html (3 ...
- mybatis mysql 中文乱码_Mybatis + Mysql 插入数据时中文乱码问题
近日跟朋友一起建立一个项目,用的是spring+mybatis+mysql. 今天碰到一个mybatis向mysql中插入数据时,中文显示为'???'的问题,拿出来说下. 对于数据库操作中出现的中文乱 ...
- Spring MVC POST中文乱码解决方案
2019独角兽企业重金招聘Python工程师标准>>> POST中文乱码解决方案 1.自己编写一个filter,来实现编码,代码如下: 1 public class Characte ...
- pb mysql 中文乱码_解决springmvc+mybatis+mysql中文乱码问题
近日使用ajax请求springmvc后台查询mysql数据库,页面显示中文出现乱码 最初在mybatis配置如下 select text from News where id=#{o} 其中表New ...
- Spring中FrameMaker中文乱码
1. 模板文件 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www ...
- mybatis mysql 中文乱码_mybatis连接mysql数据库插入中文乱码
对于mysql数据库的乱码问题,有两中情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...
- Mybatis Mysql 中文乱码
前置背景: 1.查询服务日志输出中文正常 2.手动数据库insert测试数据中文正常 解决方案: 1.查询mysql设置 show variables like '%character%'; 2.修改 ...
- spring mvc 中文乱码 post与get的方法解决
spring mvc表单提交中文参数乱码问题 今天测试spring mvc ,中文乱码,在web.xml中加上 <filter><filter-name>encodingFi ...
- Java中文乱码破碎重组_总结彻底解决Spring MVC+Mybatis中文乱码问题
Java对于新手最容易出现的问题就是中文乱码的问题.今天我就来总结一下彻底解决Spring mvc+Mybatis中文乱码的方案. 首先要看打一断点看一下Controller接收到参数值是否正常.如果 ...
最新文章
- R语言使用integrate函数进行函数积分计算实战
- 58、vi常见用法,tags(2)
- EM算法和GMM(中)
- 回文树或者回文自动机,及相关例题
- PAT_B_1047_Java(20分)
- Android之FLAG_ACTIVITY_TASK_ON_HOME
- vs2013 中HTML页 无法在设计窗口中查看的解决
- 【报告分享】巨量引擎2019娱乐算数白皮书.pdf(附下载链接)
- Android开发笔记(六十六)自定义对话框
- 【王道操作系统笔记】系统调用
- Python入门--二重循环中的continue和break
- 数据分析告诉你,韦小宝跟他七个老婆哪个最亲?
- 70部MAYA灯光材质渲染教程合集
- 2022-2028全球汽车后置摄像头模组行业调研及趋势分析报告
- 图像处理:语义分割(sematic segmentation)
- 《Implicit Class-Conditioned Domain Alignment for Unsupervised Domain Adaptation》
- 评《软件测试职业发展三步曲》
- 在线提取网页视频的网站
- Spring Security4 CSRF 如何关闭CSRF功能
- 硕士论文查重率是多少?
热门文章
- Centos更换阿里云源
- Excel的日期格式约定与解析
- yml语法规则 (5.spring boot配置文件注入@ConfigurationProperties)、配置文件处理器...
- my wordpress
- poj 2524 Ubiquitous Religions (并查集)
- 文件夹目录结构导出工具及下载
- 83款 网络爬虫开源软件
- .sh文件是什么语言_shell命令在C语言程序中的调用
- Node-Web应用框架Express
- 【Linux 内核】实时调度类 ① ( 进程分类 | 实时进程、普通进程 | Linux 内核 SCHED_FIFO、SCHED_RR 调度策略 | 实时调度实体 sched_rt_entity )