关于转载Spring 的MVC I18N-国际化相关配置出现的问题
2008-02-24 14:04
696 查看
在前几天引用“Spring 的MVC I18N-国际化相关配置 ”并做了测试,发现 有一问题。程序运行会抛出异常
“Cannot change HTTP accept header - use a different locale resolution strategy”,根本原因是spring source 做了限制,源码如下
public class AcceptHeaderLocaleResolver implements LocaleResolver {
public Locale resolveLocale(HttpServletRequest request) {
return request.getLocale();
}
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
throw new UnsupportedOperationException(
"Cannot change HTTP accept header - use a different locale resolution strategy");
}
}
请注意上面的类,该类允许继承,所以需要改写setLocale方法,源码示范如下
package org.springframework.web.servlet.i18n;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
public class MyAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver {
private Locale myLocal;
public Locale resolveLocale(HttpServletRequest request) {
return myLocal;
}
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
myLocal = locale;
}
}
然后在action-servlet.xml里的设置为
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean>
<bean id="myViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
<bean id="filenameController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors" ref="localeChangeInterceptor"/>
<property name="mappings">
<value>
chinese.do=filenameController
us.do=filenameController
</value>
</property>
</bean>
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.MyAcceptHeaderLocaleResolver"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
“Cannot change HTTP accept header - use a different locale resolution strategy”,根本原因是spring source 做了限制,源码如下
public class AcceptHeaderLocaleResolver implements LocaleResolver {
public Locale resolveLocale(HttpServletRequest request) {
return request.getLocale();
}
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
throw new UnsupportedOperationException(
"Cannot change HTTP accept header - use a different locale resolution strategy");
}
}
请注意上面的类,该类允许继承,所以需要改写setLocale方法,源码示范如下
package org.springframework.web.servlet.i18n;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
public class MyAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver {
private Locale myLocal;
public Locale resolveLocale(HttpServletRequest request) {
return myLocal;
}
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
myLocal = locale;
}
}
然后在action-servlet.xml里的设置为
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean>
<bean id="myViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
<bean id="filenameController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors" ref="localeChangeInterceptor"/>
<property name="mappings">
<value>
chinese.do=filenameController
us.do=filenameController
</value>
</property>
</bean>
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.MyAcceptHeaderLocaleResolver"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
相关文章推荐
- Spring 的MVC I18N-国际化相关配置出现的问题
- Spring 的MVC I18N-国际化相关配置
- Spring 的MVC I18N-国际化相关配置
- Spring 的MVC I18N-国际化相关配置
- 关于hibernate+Springle+springMVC配置问题
- spring项目中dubbo相关的配置文件出现红叉的问题
- 关于spring-mvc中使用ajax调用后台接口出现400 bad request的问题解决方案
- [转载]关于flex与blazeDS和spring配置的问题
- spring mvc <mvc:annotation-driven>配置使用出现问题
- 关于Spring boot中读取属性配置文件出现中文乱码的问题的解决
- spring mvc <mvc:annotation-driven>配置使用出现问题
- 关于SpringMVC*.xml配置文件加载问题
- 关于Myeclipse10中建立web工程添加spring配置文件出现红叉错误提示的问题
- spring-mvc配置异常问题FormattingConversionServiceFactoryBean
- 关于使用VS.Net2003调试器出现的问题及相关解决方法 (转)
- 有关于git访问bitbacket的ssh key配置以及出现的小问题
- spring 动态数据源配置以及相关问题
- 关于配置spring AOP中的前置通知MethodBeforeAdvice出现的异常
- 关于spring配置中the prefix "tx" for element "tx:annotation-driven" is not bound 问题的处理
- SpringMVC项目中配置xml时一些和命名空间相关的问题,如mvc:annotation-driven的前缀 "mvc"未绑定