0

I'm going crazy why my css file isn´t getting loaded. I'm using spring mvc 4 + apache tomcat 8

I already use the same code for other projects and it works normally.
Here's my project structure

enter image description here

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>AO</display-name>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>hibernate.xml</param-value>
    </context-param>


    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>mvc-dispatcher-servlet.xml</param-value>
        </init-param> 
        <load-on-startup>1</load-on-startup>
    </servlet>

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



</web-app>

my servlet context :

<context:component-scan base-package="controller"/>
<mvc:annotation-driven />

<mvc:resources mapping="/RESOURCES/**" location="/RESOURCES/" />  

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

when he try to load :

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/RESOURCES/css/welcome.css" > 

or

<link rel="stylesheet" type="text/css" href="/RESOURCES/css/welcome.css">

or

<link rel="stylesheet" type="text/css" href="/AO/RESOURCES/css/welcome.css">

i got this error:

GET http://localhost:8080/AO/RESOURCES/css/welcome.css 404 (Introuvable) 
Roberto Bonvallet
  • 31,943
  • 5
  • 40
  • 57
Hayi
  • 6,972
  • 26
  • 80
  • 139

4 Answers4

0

Try adding <mvc:default-servlet-handler /> to your mvc configuration file.

Jukka
  • 4,583
  • 18
  • 14
  • It makes it possible to map the spring mvc dispatcher servlet to "/" and yet have the container serve static resources. It is actually an alternative to using as far as I can tell. Now that it did not help, is the RESOURCES folder actually being copied over to Tomcat upon deployment? – Jukka Jun 27 '14 at 17:00
  • How can i know if it is deployed ? – Hayi Jun 27 '14 at 17:20
  • Go and have a look at the WAR file or deployment directory and see if the RESOURCES directory is there. – Jukka Jun 27 '14 at 17:21
  • I Delete the Apache tomcat server and I add it again and it finally my CSS has been loaded :) Can you explain me why ? – Hayi Jun 27 '14 at 21:11
0

change the mapping for RESOURCES to LOWERCASE resources

Except the name of your Application,which can contain Uppercase elements, the path for resources must contain only lowercase

<mvc:resources mapping="/resources/**" location="/RESOURCES/" />

Then ref :

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/welcome.css" >
Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38
  • I Delete the Apache tomcat server and I add it again and it finally my CSS has been loaded :) Can you explain me why ? – Hayi Jun 27 '14 at 21:16
  • it is working for me and for sure nothing is wrong with the tomcat server... maybe you don't know how to use it properly – Rafik BELDI Jun 29 '14 at 23:38
0

I Delete the Apache tomcat server and I add it again and finally my CSS has been loaded :) Can somebody explain me why ?

Hayi
  • 6,972
  • 26
  • 80
  • 139
0

Use c:url tag of JSP Standard Tag Library to make the URL relative.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<link rel=stylesheet href="<c:url value='/RESOURCES/css/welcome.css'/>" 
      type="text/css" />
Braj
  • 46,415
  • 5
  • 60
  • 76