重写stringhttpmessageconverter了为什么还有乱码
1个回答
推荐于2018-04-05
展开全部
在Spring MVC的配置文件中配置以下:
Xml代码
<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源码,例如我的修改后:
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;
},
注意里面的条件判断函数:
Js代码
if(!options.failure){
options.failure = function(result,request){
Ext.MessageBox.show({
title : "出错了",
multiline: true,
value : response.responseText,
msg : "操作失败,失败原因:",
: 300,
icon : Ext.MessageBox.ERROR,
buttons : Ext.MessageBox.YES
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询