当前位置: 代码迷 >> 综合 >> springmvc之国际化页面出现Cannot change HTTP accept header - use a different locale resolution strategy
  详细解决方案

springmvc之国际化页面出现Cannot change HTTP accept header - use a different locale resolution strategy

热度:82   发布时间:2023-11-27 02:08:29.0

今天在练习springmvc的国际化页面的时候,遇到了一个问题:

Cannot change HTTP accept header - use a different locale resolution strategy.....

十一月 12, 2018 12:39:42 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springDispatcherServlet] in context with path [/springMVC_crud] threw exception [Request processing failed; nested exception is java.lang.UnsupportedOperationException: Cannot change HTTP accept header - use a different locale resolution strategy] with root cause
java.lang.UnsupportedOperationException: Cannot change HTTP accept header - use a different locale resolution strategyat org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver.setLocale(AcceptHeaderLocaleResolver.java:47)at org.springframework.web.servlet.i18n.LocaleChangeInterceptor.preHandle(LocaleChangeInterceptor.java:73)at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:130)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1152)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Unknown Source)

在国内的技术论坛csdn上面找了大半天都没有解决,后来无意中点进了一个老外的论坛里面,终于把问题解决了:

点击这里进入

如果网站进不了,请看下面:

其中的:Change the bean id from "localResolver"是解决问题的关键:其中的localResolver是老外拼错了,少了一个字母,这不是最重要的,重要的是 to "localeResolver" 。一下是我按照这个修改以后的配置文件:

	<!-- 国际化页面配置(通过超链接选择语言那种)配置SessionLocaleResolver用于将Locale对象存储于Session中供后续使用(这里的Id只能用这个:localeResolver,否则会出问题)   第一步: --><bean id="localeResolver"class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean><!-- 配置国际化页面拦截器(通过超链接选择语言那种)配置LocaleChangeInterceptor 主要用于获取请求中的locale信息,将期转为Locale对像,获取LocaleResolver对象    第二步: --><bean id="localeChangeInterceptor"class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean><!-- 不能直接访问globle.jsp文件,要配置映射 ,   第三步--><mvc:view-controller path="/myGlobal" view-name="global" />

修改以后就没有任何问题了。

也就是说:第一步当中的bean的id只能是:localeResolver

如果是其他的,大概率都会出错。那么,如果你也遇到这样的问题,请立马回去检查你的bean的id吧。

  相关解决方案