導航:首頁 > 編程語言 > springmvc返回json亂碼

springmvc返回json亂碼

發布時間:2023-03-18 23:28:54

㈠ 如何解決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。如: 或者同樣要用jackson的jar包。x0dx0a2、第二種使用JSON工具將對象序列化成json,常用工具Jackson,fastjson,gson。x0dx0a利用HttpServletResponse,然後獲取response.getOutputStream()或response.getWriter()x0dx0a直接輸出。x0dx0a示例:x0dx0a[java] view plain print?x0dx0apublic class JsonUtil x0dx0a{ x0dx0a private static Gson gson=new Gson(); x0dx0a /** x0dx0a * @MethodName : toJson x0dx0a * @Description : 將對象轉為JSON串,此方法能夠滿足大部分需求 x0dx0a * @param src x0dx0a * :將要被轉化的對象 x0dx0a * @return :轉化後的JSON串 x0dx0a */ x0dx0a public static String toJson(Object src) { x0dx0a if (src == null) { x0dx0a return gson.toJson(JsonNull.INSTANCE); x0dx0a } x0dx0a return gson.toJson(src); x0dx0a } x0dx0a} x0dx0a3、第三種利用spring mvc3的註解@ResponseBodyx0dx0a例如:x0dx0ax0dx0a[java] view plain print?x0dx0a@ResponseBody x0dx0a @RequestMapping("/list") x0dx0a public List list(ModelMap modelMap) { x0dx0a String hql = "select c from Clothing c "; x0dx0a Page page = new Page(); x0dx0a page.setPageSize(6); x0dx0a page = clothingServiceImpl.queryForPageByHql(page, hql); x0dx0a x0dx0a return page.getResult(); x0dx0a } x0dx0ax0dx0a然後使用spring mvc的默認配置就可以返回json了,不過需要jackson的jar包

閱讀全文

與springmvc返回json亂碼相關的資料

熱點內容
港版蘋果用的插排 瀏覽:1000
雕刻機編程去哪裡學 瀏覽:436
編程怎麼與steam教育融合 瀏覽:697
js製作滑鼠拖拽小塊 瀏覽:310
將圖紙拆分為多個CAD文件 瀏覽:779
如何鑒別dsd文件 瀏覽:902
thinkphp不能用js 瀏覽:664
蘋果11粘膩app是什麼意思 瀏覽:670
安卓手機中木馬了怎麼辦 瀏覽:964
java組建模型 瀏覽:53
wifi萬能密碼安全嗎 瀏覽:785
紅色系圓圈是什麼app 瀏覽:714
迷你編程開始的教程怎麼過 瀏覽:216
上海國衡網站有什麼用 瀏覽:29
掃描文件如何全選 瀏覽:363
directx一鍵修復工具 瀏覽:620
如何恢復谷歌同步中刪除的文件夾 瀏覽:215
安卓51轉換為系統應用 瀏覽:789
哪裡看雙11數據 瀏覽:783
文件變成exe如何恢復 瀏覽:49

友情鏈接