StringHttpMessageConverter乱码问题的解决(Spring 3.2)

特别标注了是Spring 3.2,在网上搜了半天,很多配置应该是老版本的缘故,导致我配置了StringHttpMessageConverter转换编码始终不起作用(也有可能是我个人的错误)。后来在Stackoverflow找到了应该算是正确的配置方式,至少我的是可以运行了:


在Spring MVC的配置文件中配置以下:

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8</value>
<value>application/x-www-form-urlencoded; charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>



第一个bean就是配置StringHttpMessageConverter的。

顺带说一下Ext JS的异常处理:

Ext.data.Store的请求结束会调用Connection的onComplete函数,因此如果要对后台自定义异常的统一前台处理,可以考虑修改该JS源码,例如我的修改后:


onComplete : function(request) {
var me = this,
options = request.options,
result,
success,
response;
try {
result = me.parseStatus(request.xhr.status);
} catch (e) {
// in some browsers we can't access the status if the readyState is not 4, so the request has failed
result = {
success : false,
isException : false
};
}
success = result.success;

if (success) {
response = me.createResponse(request);
me.fireEvent('requestcomplete', me, response, options);
Ext.callback(options.success, options.scope, [response, options]);
} else {
if (result.isException || request.aborted || request.timedout) {
response = me.createException(request);
} else {
response = me.createResponse(request);
}
me.fireEvent('requestexception', me, response, options);
if(!options.failure){
options.failure = function(result,request){
Ext.MessageBox.show({
title : "出错了",
multiline: true,
value : response.responseText,
msg : "操作失败,失败原因:",
width : 300,
icon : Ext.MessageBox.ERROR,
buttons : Ext.MessageBox.YES
});
}
}
Ext.callback(options.failure, options.scope, [response, options]);
}
Ext.callback(options.callback, options.scope, [options, success, response]);
delete me.requests[request.id];
return response;
},


注意里面的条件判断函数:

if(!options.failure){
options.failure = function(result,request){
Ext.MessageBox.show({
title : "出错了",
multiline: true,
value : response.responseText,
msg : "操作失败,失败原因:",
width : 300,
icon : Ext.MessageBox.ERROR,
buttons : Ext.MessageBox.YES
});
}
}


当然,这里只是我简单的处理了一下,在实际开发中还要进一步的进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值