1、问题由来css
在学习SpringMVC的过程当中,对于URL的拦截,使用了RESTful形式,由于使用了RESTful因此,在将Servlet做为Controller中的时候,web.xml中配置拦截的url-pattern就写成了 / ,以下所示:html
<servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此处能够能够配置成 *.do ,对应struts的后缀习惯 --> <url-pattern>/</url-pattern> </servlet-mapping>
若是配置成这样,对于静态资源(js,css等)也会被拦截,由于没有Controller处理器和其对应,很显然会报404的错误。java
2、问题处理方式web
对于此静态资源的问题,我采用了RT所示的<mvc:resources … />这种进行处理:spring
<mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/" />
<mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/" />
<mvc:resources mapping="/images/**" location="/WEB-INF/statics/images/" />
<mvc:resources mapping="/*.html" location="/" />
项目目录结构以下:浏览器
然而当把全局的Formatter<Date> 集成进去,想将字符串转成Date的转换器配置进处理器适配器时候出现问题,此时对于SpringMVC的配置是:spring-mvc
<mvc:annotation-driven conversion-service="conversionService" /> <!-- 静态资源映射 --> <!-- 好比${basePath}/css/ht.css 会自动请求location中的css --> <mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/" /> <mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/" /> <mvc:resources mapping="/images/**" location="/WEB-INF/statics/images/" /> <mvc:resources mapping="/*.html" location="/" /> <!-- 日期统一转换 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <set> <bean class="com.mc.bsframe.formatter.DateFormatter"> <constructor-arg name="datePattern" value="yyyy-MM-dd HH:ss:mm" /> </bean> </set> </property> </bean>
错误提示主要是:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.util.ArrayList<?>] to type [java.util.List<org.springframework.core.io.Resource>] for value '[/WEB-INF/statics/js/]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.ArrayList<?>] to type [org.springframework.core.io.Resource]
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.ArrayList<?>] to type [org.springframework.core.io.Resource]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
at org.springframework.core.convert.support.CollectionToCollectionConverter.convert(CollectionToCollectionConverter.java:87)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
... 36 moremvc
错误提示中最核心的部分是:Failed to convert from type [java.util.ArrayList<?>] to type [java.util.List<org.springframework.core.io.Resource>] for value '[/WEB-INF/statics/js/]'翻译出来就是:app
尝试将'[/WEB-INF/statics/js/]从java.util.ArrayList<?> 转换到java.util.List<org.springframework.core.io.Resource>的时候失败了。async
在用Spring3.X的时候没有这个问题出现,换成Spring4.X的时候就出现这个问题了,具体的缘由不作解释,大致上就是以前的是对于资源的存储使用的是String[]如今改为了ArrayList<>形成的问题。
3、解决方法
但通常都会使用第2个,由于转换器是必须的,而Spring版本通常不会更换,可能会使用到高版本中的一些特性或功能,具体处理步骤以下:
<mvc:default-servlet-handler />
最终的关键配置以下:
<mvc:annotation-driven conversion-service="conversionService" /> <!-- 若是使用了RESTful形式的拦截,那么对于静态资源的处理上,就须要加上此句,静态资源(没有映射的)就会 --> <mvc:default-servlet-handler /> <!-- 日期统一转换 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <set> <bean class="com.mc.bsframe.formatter.DateFormatter"> <constructor-arg name="datePattern" value="yyyy-MM-dd HH:ss:mm" /> </bean> </set> </property> </bean>