㈠ 如何解決jsON返回的中文亂碼
使用jQuery ajax調用的返回json,中文亂碼問題
Js代碼如下:
$.ajax({ url: '/test/testAction.do?method=test', type: 'POST', dataType: 'json', timeout: 5000, async: false, error: function(){ alert('獲取數據失敗!'); }, success: function(json){ jsObject = eval(json); } }); return jsObject;
Js代碼
<span style="font-size: x-small;">$.ajax({ url: '/test/testAction.do?method=test', type: 'POST', dataType: 'json', timeout: 5000, async: false, error: function(){ alert('獲取數據失敗!'); }, success: function(json){ jsObject = eval(json); } }); return jsObject;</span> action:
java代碼
JSONArray json = JSONArray.fromObject(SysList);//SysList是一個List // 設置response的ContentType解決中文亂碼 response.setContentType("text/html;charset=UTF-8"); response.getWriter().print(json.toString()); return null; Java代碼 <span style="font-size: x-small;">JSONArray json = JSONArray.fromObject(SysList);//SysList是一個List // 設置response的ContentType解決中文亂碼 response.setContentType("text/html;charset=UTF-8"); response.getWriter().print(json.toString()); return null;</span>
㈡ spring MVC接收中文亂碼問題
1:表單提交controller獲得中文參數後亂碼解決方案
注意:jsp頁面編碼設置為UTF-8
form表單提交方式為必須為post,get方式下面spring編碼過濾器不起效果
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<formaction="${ctx}/user/addUser"name="userForm"method="post">
修改web.xml,增加編碼過濾器,如下(注意,需要設置forceEncoding參數值為true)
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2:表單提交controller獲得中文參數後正常顯示控制台,然後保存資料庫出現亂碼
注意:資料庫編碼是否支持中文
資料庫表和表欄位是否正確
在配置連接資料庫的參數設置修改:
<propertyname="url"value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8"></property>第一種情況:
jsp頁面中文輸入,到controller亂碼,這時候需要設置的是在web.xml文件中添加一個編碼的過濾器(filter)將編碼統一為UTF-8,代碼如下:
Web.xml配置文件:
view
sourceprint?
01.<filter>
02.<filter-name>CharacterEncodingFilter</filter-name>
03.<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
04.<init-param>
05.<param-name>encoding</param-name>
06.<param-value>utf-8</param-value>
07.</init-param>
08.</filter>
09.<filter-mapping>
10.<filter-name>CharacterEncodingFilter</filter-name>
11.<url-pattern>/*</url-pattern>
12.</filter-mapping>
這里需要注意的是,最好把這段代碼放在web.xml中開頭的位置,因為攔截有順序,如果放在後面的話容易攔截不到。
第二種情況:
資料庫中文數據,jsp頁面顯示亂碼(不是嚴格意義上的亂碼,而是以問號的形式呈現)
由於我們前後台的數據交互使用的是json數據,出現這種情況的原因我也不太清楚,之前也沒遇到過,只能怪自己做過的項目太少,解決起來也不困難,只需要在轉json的時候設置一下編碼格式就可以了,代碼如下:
view
sourceprint?
1.response.setContentType("application/json;charset=UTF-8");//防止數據傳遞亂碼
寫上這句話就不會再出現亂碼了。
第三種情況:
頁面中文,傳遞到controller也是正確的,但是保存到資料庫之後就是亂碼(也不是嚴格意義的亂碼,跟上面一樣全是問號)
這個問題困擾了我一段時間,開始覺得資料庫的編碼格式不正確,重新創建了編碼格式為utf-8的資料庫也還是不可以,最後覺得是jboss的問題,我們的伺服器用的是jboss,上網查了資料在連接數據源的時候加上編碼格式就可以了
㈢ 如何徹底解決SpringMVC4.0下使用解決@ResponseBody 中文亂碼問題
SpringMVC的@ResponseBody返回中文亂碼的原因是SpringMVC默認處理的字元集是ISO-8859-1,在Spring的org.springframework.http.converter.StringHttpMessageConverter類中可以看到如下代碼:
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
解決返回中文亂碼的問題有兩種,第一種是局部的,只針對於某個方法的返回進行處理,第二種是全局的,針對於整個項目,如下:
第一種:在@RequestMapping中添加proces="text/html;charset=UTF-8,如:
[html] view plain
@RequestMapping(value="/login.do",method=RequestMethod.POST,proces="text/html;charset=UTF-8")
@ResponseBody
public String login(@RequestParam(value="username") String userName,@RequestParam(value="password") String password){
return JSONMessageUtil.getSuccessJSON("登錄成功");
}
第二種:在配置文件中的mvc:annotation-driven中添加如下代碼:
[html] view plain
<mvc:annotation-driven >
<!-- 消息轉換器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:resources location="/resources/" mapping="/resources/**" />
㈣ springmvc怎麼返回json
SpringMVC返回json數據有三種方式x0dx0a1、第一種方式是spring2時代的產物,也就是每個json視圖controller配置一個Jsoniew。如: