springMVC解決ajax請(qǐng)求亂碼的三種方法
springMVC解決ajax請(qǐng)求亂碼的問題
前言:
最近在項(xiàng)目的使用過程中發(fā)現(xiàn)在springmvc的項(xiàng)目中,使用返回頁面的請(qǐng)求方式,數(shù)據(jù)都能正常顯示,但是對(duì)于ajax的請(qǐng)求,始終顯示亂碼。首先第一種是因?yàn)槲覀冊(cè)趙eb.xml中配置了spring的字符編碼過濾器,那么使用ajax請(qǐng)求為什么就不行了呢?下面簡(jiǎn)單的分析一下,僅供參考。
先列出簡(jiǎn)單的請(qǐng)求代碼:
瀏覽器端:
<script type="text/javascript"> $.ajax({ type: "POST", url: "/pages/ajax", data: "name=garfield&age=18", success: function(data){ console.log(data); } }) </script>
服務(wù)器端:
@ResponseBody @RequestMapping("/ajax") public String ajaxCharacter(){ return "測(cè)試"; }
web.xml:
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param><!-- 針對(duì)request --> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param><!-- 針對(duì)response --> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
首先,當(dāng)一個(gè)請(qǐng)求到來時(shí),會(huì)先經(jīng)過spring的這個(gè)過濾器CharacterEncodingFilter,過濾器設(shè)置好編碼之后會(huì)進(jìn)入springmvc的這個(gè)DispatcherServlet,通過springmvc的一系列轉(zhuǎn)化(此處省略。。。),到達(dá)我們的控制層,并且?guī)臀覀兎庋b好了參數(shù)。在springmvc中配置這個(gè)配置項(xiàng)之后<mvc:annotation-driven>,會(huì)默認(rèn)配置RequestMappingHandlerAdapter和HttpMessageConverter,當(dāng)我們使用@ResponseBody時(shí),那么數(shù)據(jù)返回時(shí)會(huì)調(diào)用這個(gè)數(shù)據(jù)轉(zhuǎn)換器。經(jīng)過查看源碼可知,默認(rèn)情況下會(huì)轉(zhuǎn)換成ISO-8859-1格式。簡(jiǎn)單源碼附上:
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1"); private final Charset defaultCharset; private final List<Charset> availableCharsets; private boolean writeAcceptCharset; protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); } Charset charset = getContentTypeCharset(outputMessage.getHeaders() .getContentType()); StreamUtils.copy(s, charset, outputMessage.getBody()); } ... } public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConverter<T> { protected final Log logger = LogFactory.getLog(super.getClass()); private List<MediaType> supportedMediaTypes = Collections.emptyList(); ... }
通過上面的源碼可以看出,對(duì)于字符串會(huì)自動(dòng)編碼成默認(rèn)格式ISO-8859-1,所以對(duì)應(yīng)的就有解決方案。
方案一:
@RequestMapping(value = “/test”,produces=”text/html;charset=UTF-8;”)
方案二:
此注解需要注意的是一定要使用spring 3.1.x 以上。
<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>
方案三:
不使用@ResponseBody,將請(qǐng)求處理改成如下:
@RequestMapping("/ajax") public void ajaxCharacter(HttpServletResponse response) throws IOException{ PrintWriter out = response.getWriter(); out.print("測(cè)試"); out.close(); }
以上就是解決springMVC的亂碼問題,如有疑問請(qǐng)留言或到本站社區(qū)交流,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- SpringMVC中處理Ajax請(qǐng)求的示例
- SpringMVC ajax請(qǐng)求的處理方法介紹
- Springmvc處理ajax請(qǐng)求并返回json數(shù)據(jù)
- Spring MVC中處理ajax請(qǐng)求的跨域問題與注意事項(xiàng)詳解
- Springmvc ajax跨域請(qǐng)求處理方法實(shí)例詳解
- Spring MVC 中 AJAX請(qǐng)求并返回JSON的示例
- SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)
- Spring MVC 處理Ajax請(qǐng)求的方式詳解
相關(guān)文章
jsp利用application統(tǒng)計(jì)在線人數(shù)的方法
這篇文章主要介紹了jsp利用application統(tǒng)計(jì)在線人數(shù)的方法,代碼中備有較為詳盡的注釋便于理解,是比較實(shí)用的技巧,需要的朋友可以參考下2015-01-01jsp登錄注冊(cè)完整實(shí)現(xiàn)代碼(增刪改查+網(wǎng)頁+數(shù)據(jù)庫)
這篇文章主要給大家介紹了關(guān)于jsp登錄注冊(cè)完整實(shí)現(xiàn)代碼的相關(guān)資料,其中包括增刪改查+網(wǎng)頁+數(shù)據(jù)庫,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用jsp具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01