1. 程式人生 > >springmvc之國際化頁面出現Cannot change HTTP accept header

springmvc之國際化頁面出現Cannot change HTTP accept header

今天在練習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 strategy
	at 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吧。