SpringMVC 返回值的字符轉換



當一個方法需要返回帶中文的結果集給前臺頁面處理時,必須要對此結果集編碼。SpringMVC可以通過在後臺方法上加上@ResponseBody註解,以及在xml中配置統一結果集編碼來便捷地解決此問題。


方法示例:

@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> order(HttpServletRequest request, HttpServletResponse reponse) {
       
        Map<String, Object> map = new HashMap<String, Object>();

return map;

}


xml配置:


<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" lazy-init="false">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>


<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章