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中的处理方法;
@RequestMapping(value="/submitBlog", method=RequestMethod.POST) public String submitBlog(HttpServletRequest request, Blog blog){ //System.out.println("ContentType: "+request.getContentType()); //System.out.println("Request请求的编码格式为:"+request.getCharacterEncoding()); /*请求内容自动注入到blog中,并且能够正常显示*/ return "redirect:personalSpace"; } 未进行第三步之前,通过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类的源码,可以看到;
public class CharacterEncodingFilter extends OncePerRequestFilter { /*设置Request请求要转换为的编码*/ private String encoding; /*设置是否强制对Request和Response内容转码*/ private boolean forceEncoding = false; /*其他方法*/ @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); } }
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的对应方法时出现乱码。
解决方案:
@RequestMapping("/editBlog") public String editBlog(HttpServletRequest request,@RequestParam("blogTitle")String blogTitle){ try { blogTitle = new String(blogTitle.getBytes("ISO-8859-1"), "UTF-8"); //需要进行转码 } catch (UnsupportedEncodingException e) { System.out.println("编码转换出错了!!!"); } /*其他处理*/ }
不知为啥,注入到URL处理方法形参中的字符串采用ISO-8859-1编码,不过进行编码转换后,就正常了。
相关推荐
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
jsq+mybatis+mysql中文乱码的详细方案。
Spring+SpringMVC+Mybatis框架整合例子——亲测可用.zip
这是一个基于springboot+vue+mybatis的学生成绩管理系统。java开发基于springboot的管理系统源码。这是一个基于springboot+vue+mybatis的学生成绩管理系统。java开发基于springboot的管理系统源码。这是一个基于...
完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统...
目标:本示例说明SFM(SpringBoot+WebFlux+Mybatis)单体高并应用开发。 开发环境:IDEA集成工具,JDK 1.8 使用步骤:下载资源后解压项目,使用IDEA导入项目文件。 使用步骤: 1、下载加压后,使用IDEAl导入项目 2、...
1、基于SpringBoot+MyBatis前后端开发的博客网站项目源码.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料...
SpringBoot+MyBatis+Mysql+Layui实现功能完善的原创文学CMS系统 SpringBoot+MyBatis+Mysql+Layui实现功能完善的原创文学CMS系统 SpringBoot+MyBatis+Mysql+Layui实现功能完善的原创文学CMS系统 SpringBoot+...
SSM(spring+spring MVC+mybatis)开发学生信息后台管理系统,实现学生增删改查功能设计一个简单的学生信息管理系统,要求使用SSM框架技术整合实现,用户登录后能够通过Web页面添加、删除、修改和查询学生信息 ...
完整的springboot+mybatis框架,打开就可运行。不用再去找POM配置了。
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis).pdfSSM框架——详细整合教程(Spring+SpringMVC+MyBatis).pdf
maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建
基于maven+springmvc+redis+mybatis整合案例框架,主要实现redis的读取案例
Spring+SpringMVC+Mybatis+Velocity+Maven整合
《spring+mybatis 企业应用实战》源码、类库全资料
spring+springmvc+mybatis 适合练手,但是jdk版本不要太高,不然运行容易出现版本的问题
这是一个vue+nodejs+express+mybatis够成的一个项目案例
springboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单...
校社联社团管理系统(Spring MVC+Spring+Mybatis+Redis),用来记录进度,和保存文件,完成一定阶段都上传到小组仓库中。 校社联社团管理系统(Spring MVC+Spring+Mybatis+Redis),用来记录进度,和保存文件,完成...
一个简单的spring+struts2+hibernate+mybatis整合(数据库脚本放在项目资源文件的sql目录下) 因为没想好mvc用springmvc好,还是struts2好 所以没有整合进去