`
andy_ghg
  • 浏览: 290192 次
  • 性别: Icon_minigender_1
  • 来自: 扬州
社区版块
存档分类
最新评论

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
            		});
            	}
            }


当然,这里只是我简单的处理了一下,在实际开发中还要进一步的进行调整。
分享到:
评论
1 楼 我飞我是飞飞 2014-05-11  
StringHttpMessageConverter,我是3.1.1配置;用@responsebody注解返回的是字符串;如果不配置转换UTF-8后的是json对象,比如配置supportedMediaTypes utf-8后;"{Rows:[]}";如果按照默认的返回的是{Rows:[]};不知道新版本是不是这样的

相关推荐

Global site tag (gtag.js) - Google Analytics