您的位置:首页 > 编程语言 > Java开发

解决Spring3.0 MVC @ResponseBody Ajax返回中文乱码

2012-09-25 10:49 429 查看
主要是要注意spring-mvc.xml(spring 的 controller配置文件)中的相关配置项<!-- 只扫描@Controller --><context:component-scan base-package="cn.com.sunnyrock.vimes.portal" use-default-filters="true"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--编码转换,其默认为ISO-8859-1--><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="cacheSeconds" value="0" /><property name="messageConverters"><list><bean class = "org.springframework.http.converter.StringHttpMessageConverter"><property name = "supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean></list></property></bean>
<!--annotation自动注入,这个配置很重要-->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!--必须去掉这个配置项 <mvc:annotation-driven /> -->
Controller中的方法@RequestMapping(value = "/feedback", method = RequestMethod.POST)public @ResponseBodyString feedback(HttpServletRequest request, HttpServletResponse response, FeedbackModel feedback) throws Exception{//TODOreturn "非常感谢您对我们提出的宝贵意见或建议";}JSP中的Ajax调用(这里使用了jquery.form.js这个plugin)var feedbackAdviceForm = "#feedbackAdviceForm";
$(feedbackAdviceForm).submit(function() {
if($.trim($("#advice",feedbackAdviceForm).val())==""){
alert("请输入您的宝贵意见或建议!");
$("#advice",feedbackAdviceForm).focus();
return false;
}
$(this).ajaxSubmit({
success: function(msg) {
alert(msg);
},
error: function(context, xhr) {
alert(context.responseText);
}
});
});
<form id="feedbackAdviceForm" name="feedbackAdviceForm" action="<%=rootPath %>/member/feedback.html" method="post" onSubmit="return false;"><p><label for="advice">请输入您的宝贵意见或建议</label><br> <textarea id="advice" name="advice" rows="3" cols="20"	style="width: 200px; height: 120px;"></textarea></p><p><input type="submit" value="提交"></p></form>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息