日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring+MyBatis实践—中文乱码

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

多種中文亂碼問題及解決:

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

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是指服務器發(fā)送給客戶端時的內(nèi)容編碼,pageEncoding是jsp文件本身的編碼。

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

<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 /*請求內(nèi)容自動注入到blog中,并且能夠正常顯示*/ 7 8 return "redirect:personalSpace"; 9 } 10 未進行第三步之前,通過request.getCharacterEncoding()查看的HttpServletRequest的默認編碼為null;

web.xml中設置過濾器,對每個URL請求進行編碼轉(zhuǎn)換;

<!-- 使用Filter進行編碼轉(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>

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


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

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

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

通過上述兩種方式提交的中文注入到Controller的對應方法時出現(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"); //需要進行轉(zhuǎn)碼 5 } catch (UnsupportedEncodingException e) { 6 System.out.println("編碼轉(zhuǎn)換出錯了!!!"); 7 } 8 /*其他處理*/ 9 }

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

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

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

總結(jié)

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

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