说明

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>
  1. springMvc 版本

    4.3.12.RELEASE
  2. 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>

springMvc配置拦截器无效的更多相关文章

  1. springmvc 配置拦截器

    package com.aaa.zxf.interceptor; import org.springframework.boot.autoconfigure.SpringBootApplication ...

  2. SpringMvc配置拦截器

    SpringMVC可以通过配置拦截器,进行url过滤等处理. 在spring-mvc.xml的配置文件中,如下示: 其中,在<mvc:interceptors>中可以配置多个拦截器< ...

  3. springMVC配置拦截器、过滤器、前端控制器时遇到的问题总结

    1.业务场景:使用vuejs+springMVC+spring框架搭建一个mis系统,集成SSO单点登录: 2.遇到问题:使用interceptor拦截器配置SSO单点登录,直接敲域名,或者ip+端口 ...

  4. SpringMVC配置拦截器实现登录控制

    SpringMVC读取Cookie判断用户是否登录,对每一个action都要进行判断.之前使用jstl标签在页面上判断session如果没有登录就使用如下代码跳转到登录页面. <c:if tes ...

  5. springmvc中拦截器配置格式

    对于springmvc,有两种方式配置拦截器. 一是实现HandlerInterceptor接口,如 public class MyInterceptor1 implements HandlerInt ...

  6. springmvc中拦截器的定义和配置

    package com.hope.interceptor;import org.springframework.lang.Nullable;import org.springframework.web ...

  7. SpringMVC利用拦截器防止SQL注入

    引言 随着互联网的发展,人们在享受互联网带来的便捷的服务的时候,也面临着个人的隐私泄漏的问题.小到一个拥有用户系统的小型论坛,大到各个大型的银行机构,互联网安全问题都显得格外重要.而这些网站的背后,则 ...

  8. SpringMVC——实现拦截器

    1. SpringMVC拦截器的概念与Struts2相同 2. 实现拦截器 (1) 项目结构 (2) 实现HandlerInterceptor接口 package com.zhengbin.contr ...

  9. SpringMVC经典系列-14自己定义SpringMVC的拦截器---【LinusZhu】

    注意:此文章是个人原创.希望有转载须要的朋友们标明文章出处.假设各位朋友们认为写的还好,就给个赞哈.你的鼓舞是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linusz ...

随机推荐

  1. Solon详解(六)- Solon的校验扩展框架使用与扩展

    Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...

  2. linux基础命令一、

    命令格式:  命令  -选项   参数 uname -r   查看内核版本 uname -m 查看系统版本 alias 别名. 举例: alias grep ='grep --color=auto' ...

  3. 阿里出品Excel工具EasyExcel使用小结

    前提 笔者做小数据和零号提数工具人已经有一段时间,服务的对象是运营和商务的大佬,一般要求导出的数据是Excel文件,考虑到初创团队机器资源十分有限的前提下,选用了阿里出品的Excel工具EasyExc ...

  4. Android 4.X 系统加载 so 失败的原因分析

    1 so 加载过程 so 加载的过程可以参考小米的系统工程师的文章loadLibrary动态库加载过程分析 2 问题分析 2.1 问题 年前项目里新加了一个 so库,但发现native 方法的找不到的 ...

  5. 【原创】Linux虚拟化KVM-Qemu分析(三)之KVM源码(1)

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...

  6. [LeetCode]Mysql小本本

    常用方法 累加型题目,可以考虑使用笛卡尔积进行自表连接,连接后的表进行where条件进行筛选.group by分组操作. union:需要把两列作一列可以用union,union的两张表查询的字段不一 ...

  7. 【深入理解Linux内核架构】第3章:内存管理

    3.1 概述 内存管理涵盖了许多领域: 内存中物理内存页的管理: 分配大块内存的伙伴系统: 分配小块内存的slab.slub.slob分配器: 分配非连续内存块的vmalloc机制: 进程的地址空间. ...

  8. 【LeetCode/LintCode】丨Google面试题:N皇后问题

    n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击(任意两个皇后不能位于同一行,同一列,同一斜线). 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的 ...

  9. Dockerfile构建镜像实战

    目录 一.常见Dockerfile指令 二.编写Centos Dockerfile 2.1.编写Dockerfile 2.2.构建 2.3.查看Docker镜像 2.4.运行镜像 三.CMD和ENTR ...

  10. 关于java基础_数组的学习

    数组的学习 1.数组的概念?作用是什么? 系统中存储多个值, 2.数组的定义? 数据类型[] 数组名; 3.定义好数组以后需要对其进行初始化 数组初始化有两种: 第一种动态初始化,指定数组的长度,长度 ...