說明
springMvc配置國際化攔截器失敗,點擊頁面按鈕切換中英文無效,排查發現沒有進入
LocaleChangeInterceptor 類中,判斷攔截器沒有起作用,那么是什么原因導致攔截器無效,通過查看官網配置及查找其他碼友分析,
判斷應該是annotation-driven 和 interceptors配置問題,調整兩者順序,果然解決問題,如下
<!-- if you use annotation you must configure following setting, 2.這塊還能調整自定義跳轉正常 3.配置自定義轉換器 4. 配置validator--> <mvc:annotation-driven conversion-service="conversionService" validator="validator" /> <!--該攔截器通過名為”lang”的參數來攔截HTTP請求,使其重新設置頁面的區域化信息, #####特別提醒,interceptors必須在annotation-driven之后 ######--> <mvc:interceptors> <!-- 該攔截器通過名為”locale”的參數來攔截HTTP請求,使其重新設置頁面的區域化信息 【攔截所有請求】--> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> </bean> </mvc:interceptors>
- springMvc 版本
4.3.12.RELEASE
- springMvc 所用jar(參照別的碼友的整理)
IOC
core:資源訪問,類型轉換
beans:bean工廠
expression:${}獲取屬性
context:核心接口ApplicationContext
AOP
aop:面向切面編程的實現
aspects:對AspectJ的整合
DAO
jdbc:通過jdbc模板類訪問數據庫
tx:事務的實現
orm:與hibernate,mybatis的集成
oxm:對象與xml數據之間的相互轉換
jms:系統之間發送消息,異步通信
Web
web:與web項目的整合
webmvc:子模塊springMVC
Test test:測試
3. 相關配置文件及依賴
pom文件(除了上面的基礎依賴之外的依賴jar包)
<!--springMvc DispatcherServlet depend on --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- utils --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> <!-- 日志 logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>0.9.28</version> <type>jar</type> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>0.9.28</version> <type>jar</type> </dependency> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>compile</scope> </dependency> <!--jsp依賴包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <!--springMvc validation --> <!-- https://mvnrepository.com/artifact/javax.validation/validation-api --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.FINAL</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.17.Final</version> </dependency> <!-- =================== BEGIN OF 后端傳遞集合數據到前端 =================== --> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.11.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.2</version> </dependency> <!-- END OF 后端傳遞集合數據到前端 -->
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <!-- 國際化資源文件 messageSource配置的是國際化資源文件的路徑, classpath:messages指的是classpath路徑下的 messages_zh_CN.properties文件和messages_en_US.properties文件 設置“useCodeAsDefaultMessage”,默認為false ,這樣當Spring在ResourceBundle中找不到messageKey的話,就拋出NoSuchMessageException, 把它設置為True,則找不到不會拋出異常,而是使用messageKey作為返回值。 ResourceBundleMessageSource 改為 --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="defaultEncoding" value="UTF-8" /> <property name="useCodeAsDefaultMessage" value="true" /> <property name="basename" value="classpath:message/i18n" /> </bean> <!-- 存儲區域設置信息 SessionLocaleResolver類通過一個預定義會話名將區域化信息存儲在會話中 從session判斷用戶語言defaultLocale :默認語言 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <!-- 設置session attribute的key --> <property name="localeAttributeName" value="locale"/> <!-- 設置默認的Locale --> <property name="defaultLocale" value="zh_CN" /> </bean> </beans>
spring-mvc.mxl
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 允許對靜態資源文件的訪問 --> <mvc:default-servlet-handler /> <!-- 掃描控制器類 --> <context:component-scan base-package="com.daisy" /> <!-- if you use annotation you must configure following setting, 2.這塊還能調整自定義跳轉正常 3.配置自定義轉換器 4. 配置validator--> <mvc:annotation-driven conversion-service="conversionService" validator="validator" /> <!--該攔截器通過名為”lang”的參數來攔截HTTP請求,使其重新設置頁面的區域化信息, #####特別提醒,interceptors必須在annotation-driven之后 ######--> <mvc:interceptors> <!-- 該攔截器通過名為”locale”的參數來攔截HTTP請求,使其重新設置頁面的區域化信息 【攔截所有請求】--> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> </bean> </mvc:interceptors> <!--ConversionServiceFactoryBean:只能轉換數據,FormattingConversionServiceFactoryBean:既能轉換數據也能對數據進行格式化--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set><!--tip Class Name first Charactor Lowercase--> <ref bean="myEmployeeConvert"/> </set> </property> </bean> <!--validation 驗證數據是否合法 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <!--不設置則默認為classpath下的 ValidationMessages.properties --> <property name="validationMessageSource" ref="messageSource"/> </bean> <!-- 定義跳轉的文件的前后綴 --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> <!--配置處理返回數據集合--> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/> </list> </property> </bean> </beans>