0

https://i.stack.imgur.com/fAgnx.pngv (I edited post so have to swap back the url link, sorry).

Above is my directory layout in Eclipse. My master.jsp (in /view/layouts/master.jsp can't find the css file with this path:

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

nor <link rel="stylesheet" type="text/css" href="../css/styles.css" />

dispatcher-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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

    <mvc:annotation-driven />

    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    </mvc:interceptors>

    <mvc:view-controller path="/" view-name="index" />

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

</beans>

web.xml

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

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

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

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>dispatcher</welcome-file>
    </welcome-file-list>
</web-app>
Michael
  • 2,088
  • 6
  • 29
  • 47
  • I don't do Sitemesh, but are you sure that the Sitemesh filter should/could be applied on CSS files as well? – BalusC Nov 02 '11 at 11:38
  • I use my placement project as reference, it is like that there and it was ok then, but it was using GAE not Tomcat, but I doubt that is relevant. Also that project was using urlrewrite which might have something to do with this... can't quite figure it out though. – Michael Nov 02 '11 at 11:43
  • Fixed it. http://stackoverflow.com/questions/2129876/using-spring-mapping-to-root-in-web-xml-static-resources-arent-found – Michael Nov 02 '11 at 11:46

2 Answers2

1

You need either to insert the context path to your webapp (e.g. /test-app/css/styles.css) or use a relative path. The relative path is not relative to your Eclipse directory structure, but instead is relative to your HTTP request. e.g. if you request:

/test-app/MyServlet

Then your relative path to your CSS is

css/styles.css

Mick Sear
  • 1,549
  • 15
  • 25
0
<link rel="stylesheet" type="text/css" href="../../../../css/styles.css" />
David Horák
  • 5,535
  • 10
  • 53
  • 77