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

主要是要注意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 @ResponseBody
	String feedback(HttpServletRequest request, HttpServletResponse response, FeedbackModel feedback) throws Exception{
		//TODO
		return "非常感谢您对我们提出的宝贵意见或建议";
	}

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>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值