SSM框架:解決后台傳數據到前台中文亂碼問題,使用@ResponseBody返回json 中文亂碼


解決方法一:@RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8")

/**輸入手機號碼后判斷手機號是否存在*/
@RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8")
@ResponseBody
public String getphone(String phone,HttpSession session){
      Users u=service.selectPhoneService(phone);
      if(u==null){//如果為空,則需要注冊
            String str="請您先注冊,再登錄。";
            session.setAttribute("str", str);
            return "請您先注冊,再登錄。";
      }
      return "true";
}

解決方法二,在spring-mvc.xml中添加:

<!-- 處理請求返回json字符串的中文亂碼問題 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

以上兩種方式經過驗證都沒有問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM