当一个方法需要返回带中文的结果集给前台页面处理时,必须要对此结果集编码。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>