Spring MVC程式中怎麼得到靜態資源檔案css,js,圖片檔案的路徑問題

來源:互聯網
上載者:User

標籤:localhost   ada   副檔名   頁面   解決方案   ping   使用   錯誤   bean   

問題描述
在用springmvc開發應用程式的時候。對於像我一樣的初學者,而且還是自學的人,有一個很頭疼的問題。那就是資料都已經查出來了,但是頁面的樣式仍然十分簡陋,載入不了css.js,圖片等資源檔,如果你輸入某個css的檔案在瀏覽器中得到的直接是404錯誤(例如http://localhost:8080/iMax_view/css/classify.css),表示路徑肯定沒有錯,這是因為你在web.xml中配置的springservlet

]<!-- springmvc前端控制器,rest配置 -->  <servlet>      <servlet-name>springmvc_rest</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <!-- contextConfigLocation配置springmvc載入的設定檔(配置處理器映射器、適配器等等) 如果不配置contextConfigLocation,預設載入的是/WEB-INF/servlet名稱-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>

這裡的這個配置/表示servlet已經攔截了所有的請求,包括css,js等的請求所以請求不到css,js等靜態資源
解決方案 1
使用spring mvc resource去讀取靜態檔案
在你的mvc-dispatcher-servlet.xml(springmvc.xml個人的命名不一樣)檔案裡加入如下代碼

<!-- 靜態資源解析    包括 :js、css、img、..     -->     <mvc:resources location="/js/" mapping="/js/**"/>     <mvc:resources location="/img/" mapping="/img/**"/>`     <mvc:resources location="/css/" mapping="/css/**"/>`

這樣配置以後就可以訪問你的css檔案了,這時候你輸入(例如http://localhost:8080/iMax_view/css/classify.css)就可以看到你的css代碼了
但是這樣配置對於新手來說可能會遇到spring載入Resources遇到ConversionFailedException異常這個問題(注意是debug)雖然這個問題不影響程式的使用但是總是不好看的(目前我還沒有解決)
後來在網上查才知道是由於配置了

 <!-- 自訂參數綁定 -->    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <!-- 轉換器 -->        <property name="converters">            <list>                <!-- 日期類型轉換 -->                <bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>            </list>        </property>    </bean>

這就是導致哪個問題出現的原因,轉換失敗。出現以下問題但是不影響程式使用(目前我知道的)

[code]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 ‘[/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:668)    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)

解決方案 2 採用defaultservlet
在web.xml裡面配置

<servlet-mapping>    <servlet-name>default</servlet-name>    <url-pattern>/static/*</url-pattern></servlet-mapping>

這樣做了之後,所有首碼為css 的請求都交給default servlet 去處理。這樣訪問css檔案就可以訪問了
解決方案 3 通過副檔名進行處理
在web.xml中配置

<servlet-mapping>    <servlet-name>default</servlet-name>    <url-pattern>*.css</url-pattern></servlet-mapping>

解決方案 4 對springservlet 不用restful風格,在url上加上首碼
在web.xml中修改配置

<servlet-mapping>     <servlet-name>mvc-dispatcher</servlet-name>     <url-pattern>/app</url-pattern> </servlet-mapping>

這樣做的目的就是讓攔截器值攔截以/app開頭的url,不去攔截其它的一些資源
解決方案 5 對springservlet不用restful風格 ,在url上加上尾碼
在web.xml中修改配置

<servlet-mapping>     <servlet-name>mvc-dispatcher</servlet-name>     <url-pattern>*.do</url-pattern> </servlet-mapping>

這樣做的目的是讓攔截器只攔截*.do 的url,不攔截css,js等資源
上面的五種方法各有所長,適用於不同的情境。具體用哪種看自己所需要的情境

Spring MVC程式中怎麼得到靜態資源檔案css,js,圖片檔案的路徑問題

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.