Problem Description
When developing applications with SPRINGMVC. For beginners like me, but also self-taught people, there is a very headache problem. That is, the data has been found, but the style of the page is still very primitive, loading css.js, pictures and other resource files, if you enter a CSS file in the browser to get a direct 404 error (such as http://localhost:8080/iMax_view/ CSS/CLASSIFY.CSS), indicating that the path is definitely not wrong, because you configured the Springservlet in Web. xml
]<!--SPRINGMVC Front Controller, rest configuration - <servlet> <Servlet-name>Springmvc_rest</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <!--contextconfiglocation Configuring SPRINGMVC Loaded configuration files (configuring processor mapper, adapters, and so on) if Contextconfiglocation is not configured, the default load is/web-inf/ Servlet name-serlvet.xml (springmvc-servlet.xml) - <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:spring/springmvc.xml</Param-value> </Init-param> </servlet> <servlet-mapping> <Servlet-name>Springmvc_rest</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping></span>
This configuration/indicates that the servlet has intercepted all requests, including requests for css,js and so on, so it cannot request static resources such as CSS,JS.
Workaround 1
Use Spring MVC resource to read static files
Add the following code to your Mvc-dispatcher-servlet.xml (Springmvc.xml personal name is not the same) file
<!--static resource parsing includes: JS, CSS, img 、.. - <mvc:resources Location= "/js/"Mapping= "/js/**"/> <mvc:resources Location= "/img/"Mapping= "/img/**"/>` <mvc:resources Location= "/css/"Mapping= "/css/**"/>`
This configuration will then be able to access your CSS file, when you enter (for example, HTTP://LOCALHOST:8080/IMAX_VIEW/CSS/CLASSIFY.CSS) can see your CSS code
However, this configuration may encounter a problem with spring loaded resources encountering the conversionfailedexception exception (note Debug), although this problem does not affect the use of the program but it is always not good (I have not solved it yet)
Later on the internet to find out that is due to the configuration of
<!--Custom parameter Bindings - <BeanID= "Conversionservice"class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!--Converters - < Propertyname= "Converters"> <List> <!--Date Type Conversions - <Beanclass= "Cn.itcast.ssm.controller.converter.CustomDateConverter"/> </List> </ Property> </Bean>
This is the cause of which problem occurred and the conversion failed. The following problems occur but do not affect the use of the program (currently I know)
[code]org.springframework.core.convert.conversionfailedexception:failed to convert from type Java.util.ArrayList <?> to type java.util.list<org.springframework.core.io.resource> forValue ' [/css/] '; nested exception is Org.springframework.core.convert.ConverterNotFoundException:No Converter found capable of Converting from type java.lang.String to type Org.springframework.core.io.Resource at Org.springframework.core.convert . Support. Conversionutils.invokeconverter (Conversionutils.java:41) at Org.springframework.core.convert.support.GenericConversionService.convert (Genericconversionservice.java :169) at Org.springframework.beans.TypeConverterDelegate.convertIfNecessary (Typeconverterdelegate.java:161) at Org.springframework.beans.BeanWrapperImpl.convertIfNecessary (Beanwrapperimpl.java:450) at Org.springframework.beans.BeanWrapperImpl.convertForProperty (Beanwrapperimpl.java:496) at Org.springframework.beans.BeanWrapperImpl.convertForProperty (Beanwrapperimpl.java:490) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty ( Abstractautowirecapablebeanfactory.java:1437) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues ( Abstractautowirecapablebeanfactory.java:1396) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean ( Abstractautowirecapablebeanfactory.java:1132) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( Abstractautowirecapablebeanfactory.java:522) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( Abstractautowirecapablebeanfactory.java:461) at org.springframework.beans.factory.support.abstractbeanfactory$1.getObject (abstractbeanfactory.java:295) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( Defaultsingletonbeanregistry.java:223) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (Abstractbeanfactory.java:292) at Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (Abstractbeanfactory.java:194) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons ( Defaultlistablebeanfactory.java:607) at Org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization ( Abstractapplicationcontext.java:932) at Org.springframework.context.support.AbstractApplicationContext.refresh (Abstractapplicationcontext.java: 479) at Org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext ( Frameworkservlet.java:647) at Org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext (Frameworkservlet.java: 598) at Org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext (Frameworkservlet.java: 661) at Org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext (Frameworkservlet.java:517) at Org.springframework.web.servlet.FrameworkServlet.initServletBean (Frameworkservlet.java:458) at Org.springframework.web.servlet.HttpServletBean.init (Httpservletbean.java:138) at Javax.servlet.GenericServlet.init (Genericservlet.java:158) at Org.apache.catalina.core.StandardWrapper.initServlet (Standardwrapper.java:1231) at Org.apache.catalina.core.StandardWrapper.loadServlet (Standardwrapper.java:1144) at Org.apache.catalina.core.StandardWrapper.allocate (Standardwrapper.java:817) at Org.apache.catalina.core.StandardWrapperValve.invoke (Standardwrappervalve.java:135) at Org.apache.catalina.core.StandardContextValve.invoke (Standardcontextvalve.java:106) at Org.apache.catalina.authenticator.AuthenticatorBase.invoke (Authenticatorbase.java:502) at Org.apache.catalina.core.StandardHostValve.invoke (Standardhostvalve.java:142) at Org.apache.catalina.valves.ErrorReportValve.invoke (Errorreportvalve.java:79) at Org.apache.catalina.valves.AbstractAccessLogValve.invoke (Abstractaccesslogvalve.java:610) at Org.apache.catalina.core.StandardEngineValve.invoke (Standardenginevalve.java:88) at Org.apache.catalina.connector.CoyoteAdapter.service (Coyoteadapter.java:518) at Org.apache.coyote.http11.AbstractHttp11Processor.process (Abstracthttp11processor.java:1091) at Org.apache.coyote.abstractprotocol$abstractconnectionhandler.process (Abstractprotocol.java:36]) at Org.apache.coyote.http11.http11aprprotocol$http11connectionhandler.process (Http11aprprotocol.java:283) at Org.apache.tomcat.util.net.aprendpoint$socketprocessor.dorun (Aprendpoint.java:2463) at Org.apache.tomcat.util.net.aprendpoint$socketprocessor.run (Aprendpoint.java:2452) at Java.util.concurrent.ThreadPoolExecutor.runWorker (Threadpoolexecutor.java:1145) at Java.util.concurrent.threadpoolexecutor$worker.run (Threadpoolexecutor.java:615) at Org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run (Taskthread.java:61) at Java.lang.Thread.run (Thread.java:744)
Solution 2 with Defaultservlet
Configure in Web. xml
< servlet-mapping > < Servlet-name >default</servlet-name> <Url-pattern >/static/*</url-pattern></ Servlet-mapping>
Once this is done, all requests prefixed with CSS are given to the default servlet for processing. This allows access to the CSS file
Workaround 3 Processing by file name extension
configuring in Web. xml
< servlet-mapping > < Servlet-name >default</servlet-name> <Url-pattern >*.css</url-pattern></ Servlet-mapping>
Workaround 4 to Springservlet do not use restful style, prefix the URL
Modifying the configuration in Web. xml
< servlet-mapping > < Servlet-name >mvc-dispatcher</servlet-name> < Url-pattern>/app</url-pattern></ Servlet-mapping>
The purpose of this is to have the interceptor value intercept URLs that begin with/app, and not intercept other resources.
Workaround 5 for Springservlet do not use restful style, add suffix to URL
Modifying the configuration in Web. xml
< servlet-mapping > < Servlet-name >mvc-dispatcher</servlet-name> < Url-pattern>*.do</url-pattern></ Servlet-mapping>
The purpose of this is to allow the interceptor to intercept only *.do URLs and not intercept resources such as CSS,JS.
The five methods above have their own strengths and are suitable for different scenarios. What kind of scene do you want to see?
How to get static resource file Css,js, path problem of picture file in Spring MVC program