6

I'm trying to use tag <mvc:resources>

this is my project structure

enter image description here

what I want is to access to javascript in js folder and css in styles folder
however, whenever I add this to my dispatcher-servlet.xml

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

I got error 404, when run the project (the project will redirect to login.htm which is login.jsp page)

this is my dispatcher-servlet.xml code

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:component-scan base-package="com.swcommodities.wsmill.controller"/>

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

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/"
          p:suffix=".jsp" />
    <tx:annotation-driven/>
    .
    .
    .
</beans>

this is web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.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>redirect.jsp</welcome-file>
    </welcome-file-list>

I don't know why I delete the line <mvc:resources... the project run correctly, but when add this one to the project, I get 404 error immediately. I think this one make some conflict with the current project.

3
  • You are to precise for an valid answer. You wrote "I got error 404, when run the project" -- Which URL are you requestion?! and which file (location) do you expect?
    – Ralph
    Aug 22, 2013 at 6:33
  • what url did you hit in browser and we will need your bean configuration file Aug 22, 2013 at 6:38
  • sorry for that, when running the project, it will redirect to login.htm which means login.jsp page. Aug 22, 2013 at 7:53

3 Answers 3

14

The answer is here:

Using <mvc:resources .../> in spring 3 causes all other views to stop working

Quoting the author: "<mvc:resources> requires <mvc:annotation-driven> (or explicitly declared handler mappings, etc)."

That did the trick for me.

1

You need to set the full path in the location attribute, like this:

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

One a separate note, I would recommend you put your resources all in one folder all together, for example "assets", because in your current setup a malicious user can access anything under WEB-INF/ which is obviously not what you want.

Let me know if this works.

Ayman

1
  • 1
    The note about the WEB-INF folder is wrong!, because Springs ResourceHttpRequestHandler does not return a resource if the requested path contains WEB-INF, META-INF or the effective path starts with ".."! (implemented in ResourceHttpRequestHandler.isValidPath(String))
    – Ralph
    Aug 22, 2013 at 7:11
-1

since resource files are in fold js and styles, you need to change mapping to:

<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/styles/**" location="/styles/" />

and load them in jsp:

<script type="text/javascript" src="/js/file.js"></script>
<link type="text/css" href="/styles/file.css" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.