Spring and tag mvc resources, can't reach .css, and .js files

11,138

Solution 1

FYI

For all that would have same problem as I have, I resolved my issue and shared with you:

test-servlet.xml is same without modification as in my above post.

  <mvc:annotation-driven/>
  <mvc:resources mapping="/resources/**" location="/static-resource/"/>

in the index.jsp page will be:

<spring:url value="/resources/css/screen.css" var="resourceUrl"/>
<link media="screen" rel="stylesheet" href="${resourceUrl}" type="text/css" />

Main problem was in my web.xml because I mapped spring servlet with

<url-pattern>/*.htm</url-pattern>

instead of that you must mapping only

<url-pattern>/</url-pattern>

web.xml will look:

    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Thanks for all!

Solution 2

I ran into the same issue and this is how i resolved the issue: In spring configuration file Replace

<mvc:resources mapping="/resources/**" location="/resources/"/>
With
<mvc:default-servlet-handler/>

And provide the below path in the JSP to load static contents

<link rel="stylesheet" href="resources/css/Mass.css" type="text/css"/>

Hope this will help !

Share:
11,138
Vladisav Milosavljevic
Author by

Vladisav Milosavljevic

Updated on June 05, 2022

Comments

  • Vladisav Milosavljevic
    Vladisav Milosavljevic almost 2 years

    My application use Spring 3.0.4(this is first version where tag mvc:resources work fine). The current issue is that my app can not reach .css and .js files from the mapped resources.

    Structure test.war:

    /test -root
       |
       /static-resource
                |
                /css
                   |
                   /screen.css
                |
                /js
       |
       /WEB-INF
       |
       /index.jsp
    

    My test-servlet.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/jee       http://www.springframework.org/schema/jee/spring-jee.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
            <mvc:annotation-driven/>
        <mvc:resources mapping="/resources/**" location="/static-resource/"/>
    
        <context:annotation-config/>
        <context:component-scan base-package="org.web"/>
        <tx:annotation-driven/>
    
            <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
                <property name="definitions">
                    <list>
                        <value>/WEB-INF/tiles-defs.xml</value>
                    </list>
                </property>
            </bean>
    
            <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
            </bean>
    
     <bean id="authenticationInterceptor" class="org.util.AuthenticationInterceptor"/>
    
        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basename" value="classpath:messages"/>
            <property name="defaultEncoding" value="UTF-8"/>
            <property name="cacheSeconds" value="-1"/>
        </bean>
    
        <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
        </bean>
    
        <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
            <property name="defaultLocale" value="en"/>
            <property name="cookieName" value="bovalta_language"/>
        </bean>
    
        <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="authenticationInterceptor"/>
                <ref bean="localeChangeInterceptor"/>
           </list>
       </property>
        </bean>
    
    
    
    </beans>
    

    In the index.jsp I try access to the resources on two ways with spring url and with JSTL url like below

    <%-- With JSTL url --%>
    <link media="screen" rel="stylesheet" href="<c:url value="/resources/css/screen.css"/>" type="text/css"/>
    <%-- With Spring url --%>
    <spring:url value="/resources" var="resourceUrl"/>
    <link media="screen" rel="stylesheet" href="${resourceUrl}/css/screen.css" type="text/css" />
    

    When I undeploy war file in the Tomcat AS, application work fine without any exception, but Tomcat server can not find my css and js files from resources. I try to reach css file through url http://localhost:8080/test/resources/css/screen.css but Tomcat not found it. Any suggestion will be useful.

    Thanks in advance

  • Andrew Kozak
    Andrew Kozak about 12 years
    Congrats on the fix! When you are able, please make sure to mark your answer as 'accepted' so other will see your question has been answered and be able to learn from your solution. Cheers~
  • beinghuman
    beinghuman over 10 years
    @plijg whats the difference between / and /*