Spring <mvc:resources mapping="/css/**" location="/css/" /> doesn't work

19,238

Your <mvc:resources mapping="/css/**" location="/css/" /> seems fine to me, but are you sure your application is running in the root? Shouldn't your URLs start with some other context, like:

href="/myapp/css/common.css"

Or, to avoid hardcoded references:

href="<c:url value="/css/common.css" />"

If your application is running in the root, then you might need the correct namespace declarations? Something like:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      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">
Share:
19,238

Related videos on Youtube

Simcha
Author by

Simcha

Updated on September 15, 2022

Comments

  • Simcha
    Simcha over 1 year

    Hi mapping css doesn't work.

    This is my config:

    spring-servlet.xml:

    <?xml version="1.0" encoding="UTF-8"?>
        
        <context:component-scan base-package="spring.controllers" />
        
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
           <property name="prefix" value="/WEB-INF/jsp/content/" />
           <property name="suffix" value=".jsp" />
        </bean>
        
        <context:component-scan base-package="it.jsoftware.jacciseweb.controllers"></context:component-scan>
        <mvc:annotation-driven />
    
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
        <mvc:resources mapping="/css/**" location="/css/" />
        <mvc:resources mapping="/images/**" location="/images/" />
        <mvc:resources mapping="/js/**" location="/js/" />  
        
    

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    
      <display-name>JewishProjectT</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    

    and my heder file:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <c:choose>
                <c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18en'}">
                    <c:set var="approach" value="left" scope="page"></c:set>
                </c:when>
                <c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18ru'}">
                    <c:set var="approach" value="left" scope="page"></c:set>
                </c:when>
                <c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18he'}">
                    <c:set var="approach" value="right" scope="page"></c:set>
                </c:when>
                <c:otherwise>
                    <c:set var="approach" value="left"></c:set>
                </c:otherwise>
            </c:choose>
            <link rel="stylesheet" media="all" type="text/css" href="/css/common.css">
            <link rel="stylesheet" media="all" type="text/css" href="/css/lavamenu.css">
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
            <script type="text/javascript" src="/js/<c:out value="${approach}"/>/lavalamp.js"></script>
            <title>${title}</title>
        </head>
        <body>
            <div id="main">
    

    in chrome it shows:

    /js/left/lavalamp.js 404 (Not Found) 0:19 GET

    /css/common.css 404 (Not Found) 0:17 GET

    /css/lavamenu.css 404 (Not Found)

    How to correctly configure paths to css/images/js?

    P.S. I excluded all links which starts with http:\\ , because rules of stackoverflow do not permit me post them as I am beginner.

  • Simcha
    Simcha over 11 years
    Thanks a lot, href="<c:url value="/css/common.css" />" - this resolved my issue