0

I have searched on Google for serveral hours , but I can figure out this . When I try to access css/js file both inside and outside WEB-INF . But Tomcat just shows me 404 error .

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: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">

    <context:component-scan base-package="controller" />
    <mvc:annotation-driven />
    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
        <mvc:resources mapping="/resources/**"  location="/resources/" />
</beans>

In my index.jsp file , I try all of these

<link rel="stylesheet" href="<c:url value="/resources/css/site.css"/>"/>
<link rel="stylesheet" href="<c:url value="${pageContext.request.contextPath}/resources/css/site.css"/>"/>
<link href="/resources/css/site.css" rel="stylesheet">

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="false">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>

</web-app>
  • your directories confuses me: is there a folder: "....Web Pages/resources/css/" containing the "site.css"? BTW: `"/>` is right – Ralph Jan 31 '16 at 08:59
  • Yes. That's right. But I have configed it in my servlet. – Bùi Trần Thanh Đức Jan 31 '16 at 09:37
  • can you try by creating the resource folder under WebPages not inside Web-INF.. See [Map Resouces in spring](http://crunchify.com/spring-mvc-4-2-2-best-way-to-integrate-js-and-css-file-in-jsp-file-using-mvcresources-mapping/) – mehere Jan 31 '16 at 09:40
  • I created both under Webpages and under web-inf. But just 404 error – Bùi Trần Thanh Đức Jan 31 '16 at 09:43
  • what do you mean by: "But I have configed it in my servlet" ?? – Ralph Jan 31 '16 at 09:55
  • @Roman C: how is this a duplicate of "http://stackoverflow.com/questions/8195213/spring-3-mvc-resources-and-tag-mvcresources"? - he does the things described in "Spring 3 MVC resources and tag " but it does not work - so I think this an other question! – Ralph Jan 31 '16 at 09:59
  • Yes. This is an other question. I can't config follow them. – Bùi Trần Thanh Đức Jan 31 '16 at 10:01
  • @Ralph i meant I config resource folder in servlet. But it can't point to my css folder – Bùi Trần Thanh Đức Jan 31 '16 at 10:02
  • 1
    I do not see such a kind of configuration in your web.xml, but one for spring in the `dispatcher-servlet.xml`: ` ` – Ralph Jan 31 '16 at 10:04
  • @Ralph I thought the problem in this question it's already solved in the linked answer, isn't it? – Roman C Jan 31 '16 at 10:26
  • @Roman C: but where is the difference in the OPs code to the answers given in "Spring 3 MVC resources and tag "? - the problem in "Spring 3 MVC resources and tag " was that the resource path and url was not well aligned (see my answer), but this is not the problem here, here it is something else. – Ralph Jan 31 '16 at 11:10
  • My code works now . But I don't know why . I compared code above and my code . It isn't different . I don't know why – Bùi Trần Thanh Đức Jan 31 '16 at 17:19
  • @RomanC Found it , it's not duplicated . I forgot dependency spring-context – Bùi Trần Thanh Đức Jan 31 '16 at 17:40

1 Answers1

0

Your configuration files map resources in webapp/resources, while you put it into WEB-INF. To maintain your directory tree you should use

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
David
  • 2,987
  • 1
  • 29
  • 35