spring MVC中文亂碼相關總結

總結幾種方式,都使用的話能解決大多數亂碼的狀況html

1.全部頁面使用java

<%@page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 

  

2.服務器添加過濾器web

<!-- 編碼過濾器 -->
  <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>*.action</url-pattern>
    </filter-mapping>

這是爲了解決post表單中的中文能順利提交到後臺spring

3.GET請求中文亂碼,修改tomcat中的server.xml配置文件tomcat

tomacat對GET和POST請求處理方式是不一樣的,要處理針對GET請求的編碼問題,則須要改tomcat的server.xml配置文件,以下:服務器

把這個app

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

修改爲eclipse

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

或者post

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" uRIEncoding="utf-8"/>

 

最關鍵的點在這裏:若是你是更改的tomcat安裝目錄的server.xml配置文件,那麼在用eclipse運行項目時會發現配置沒起做用,實際上是由於eclipse在運行項目時是用的eclipse中配置的tomcat,那麼問題就好解決了,打開eclipse中的tomcat配置文件。測試

更多的能夠參考 http://blog.csdn.net/xuechongyang/article/details/8283924

 

如下是我的備註,未測試

對於AJAX中文傳參能夠這樣

get

js端

var url = 'policy.do?word=encodeURI(encodeURI(‘商品'))'

var url = 'policy.do?word=escape(‘商品')'

 

服務器端

String word = request.getParameter("word"); 

word = java.net.URLDecoder.decode(word,"UTF-8");  

 post

request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); 

相關文章
相關標籤/搜索