spring,web,java 開發中亂碼解決方案

來源:互聯網
上載者:User

標籤:java web spring 亂碼

  1. 修改HTML/JSP頁面編碼格式:

    <meta charset="UTF-8">或<%@ page contentType="text/html; charset=UTF-8"%> 

  2. web.xml中的編碼設定(  需要設定forceEncoding參數值為true,強制以目標編碼為編碼格式 )

       

<filter>
       <filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

但這個設定是針對POST請求的,tomacat對GET和POST請求處理方式是不同的,要處理針對GET請求的編碼問題,則需要改tomcat的server.xml設定檔,如下:



3.tomcat的server.xml設定檔

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />


4.資料庫亂碼

  配置資料來源編碼格式

<property name="url" 

value="jdbc:mysql://121.40.90.125/test?useUnicode=true&characterEncoding=utf-8"></property>   


然後去修改資料庫、表中列的編碼格式為utf8


讓MySQL支援Emoji表情

解決方案:將Mysql的編碼從utf8轉換成utf8mb4。


修改資料庫字元集:

ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

 

修改表的字元集:

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

 

修改欄位的字元集:

ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;



spring,web,java 開發中亂碼解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.