日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Spring+MyBatis实践—中文乱码

發(fā)布時(shí)間:2025/6/17 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring+MyBatis实践—中文乱码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

多種中文亂碼問(wèn)題及解決:

  • jsp頁(yè)面亂碼
  • 表單提交亂碼
  • URL中文參數(shù)亂碼

1、jsp頁(yè)面亂碼解決(2步);
新建jsp頁(yè)面;

<%@ 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>
  • 習(xí)慣上,設(shè)置工程中設(shè)置項(xiàng)目默認(rèn)編碼為“UTF-8“;
  • 將新建的jsp頁(yè)面的三處編碼全部替換為與工程編碼一致的“UTF-8”,其中,contentType中的charset是指服務(wù)器發(fā)送給客戶端時(shí)的內(nèi)容編碼,pageEncoding是jsp文件本身的編碼。

2、表單提交亂碼解決(3步);
表單采用POST方法進(jìn)行數(shù)據(jù)提交;

<form action="./submitBlog" method="post"> <div class="row"><div class="col-lg-1"><h4>標(biāo)題</h4></div><div class="col-lg-11"><input type="text" class="form-control" placeholder="標(biāo)題" 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>

對(duì)應(yīng)的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請(qǐng)求的編碼格式為:"+request.getCharacterEncoding()); 5 6 /*請(qǐng)求內(nèi)容自動(dòng)注入到blog中,并且能夠正常顯示*/ 7 8 return "redirect:personalSpace"; 9 } 10 未進(jìn)行第三步之前,通過(guò)request.getCharacterEncoding()查看的HttpServletRequest的默認(rèn)編碼為null;

web.xml中設(shè)置過(guò)濾器,對(duì)每個(gè)URL請(qǐng)求進(jìn)行編碼轉(zhuǎn)換;

<!-- 使用Filter進(jìn)行編碼轉(zhuǎn)換 --><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>

設(shè)置了針對(duì)請(qǐng)求的編碼轉(zhuǎn)換的Filter之后,再次通過(guò)request.getCharacterEncoding()查看HttpServletRequest的編碼為"UTF-8";


查看CharacterEncodingFilter類的源碼,可以看到;

1 public class CharacterEncodingFilter extends OncePerRequestFilter { 2 /*設(shè)置Request請(qǐng)求要轉(zhuǎn)換為的編碼*/ 3 private String encoding; 4 /*設(shè)置是否強(qiáng)制對(duì)Request和Response內(nèi)容轉(zhuǎn)碼*/ 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方法進(jìn)行調(diào)用,對(duì)HttpServletRequest、HttpServletResponse進(jìn)行了編碼轉(zhuǎn)換。

3、URL中的中文參數(shù)注入到Controller對(duì)應(yīng)方法的形參時(shí)亂碼;
問(wèn)題:

<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");//被刪除的元素需要預(yù)先保留,否則已經(jīng)刪除的元素是無(wú)法獲取其父元素的。 $.post(url, function(data, status){if(status=="success"){temp.fadeOut("slow");}}); });

通過(guò)上述兩種方式提交的中文注入到Controller的對(duì)應(yīng)方法時(shí)出現(xiàn)亂碼。

解決方案

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"); //需要進(jìn)行轉(zhuǎn)碼 5 } catch (UnsupportedEncodingException e) { 6 System.out.println("編碼轉(zhuǎn)換出錯(cuò)了!!!"); 7 } 8 /*其他處理*/ 9 }

?不知為啥,注入到URL處理方法形參中的字符串采用ISO-8859-1編碼,不過(guò)進(jìn)行編碼轉(zhuǎn)換后,就正常了。

轉(zhuǎn)載于:https://www.cnblogs.com/-crazysnail/p/3926815.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Spring+MyBatis实践—中文乱码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。