设为首页 收藏本站
查看: 1023|回复: 0

[经验分享] Sprimg+MyBatis实践——中文乱码

[复制链接]

尚未签到

发表于 2016-11-27 06:02:10 | 显示全部楼层 |阅读模式
  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编码,不过进行编码转换后,就正常了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305853-1-1.html 上篇帖子: MyBatis多对多级联查询 下篇帖子: mybatis 新增返回id
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表