Search code examples
javajsptagsjstlstylesheet

JSP can't find stylesheets, images


Possible Duplicate:
JSP can't find stylesheet

Tomcat7, spring framework3, jstl 1.2. Hierarchy: WEB-INF/jsp WEB-INF/styles

I link stylesheet in my JSP file, which is located in WEB-INF/jsp: But it doesn't work! When i open my application there is no styles, and writter by Tomcat:

Apache Tomcat/7.0.14 - Error report
HTTP Status 404 -

type Status report

message

description The requested resource () is not available.
Apache Tomcat/7.0.14

So i put my styles folder out of WEB-INF and it still doesn't work! Also, images too don't work, but my images folder not in WEB-INF and their path is corect... What is the problem?


Solution

  • In Spring I put my resources in a folder outside of web-inf. Like this:

    enter image description here

    Web.xml

    <!-- Processes application requests -->
        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    

    Then in my servlet-context.xml (configuration file specified in web.xml) file I exclude the resources directory from being managed by the dispatcher, so URLs prefixed with resources/ are not picked up by the dispatcher and attempted to be routed to an appropriate controller.

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    

    In my jsp I can then access resources as normal:

    <link rel="stylesheet" type="text/css" href="/skillsmanager-ui/resources/css/reset.css" />