1

I need to access static resources with Spring MVC like images, JavaScript and CSS. After some research I found that I can access those by putting following entries in web.xml:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>

But by this I am not able to access resources under WEB-INF/ folder. I want all my resources to be under WEB-INF/ folder only to control access. How can I do this?

2 Answers 2

2

You can put this in application-servlet.xml:

     <!-- static resource mapping for style sheets, etc. -->
    <mvc:resources mapping="/styles/**"  location="/WEB-INF/skins/" />
    <mvc:resources mapping="/scripts/**" location="/WEB-INF/scripts/" />

And on the page you could have:

 <script type="text/javascript" src="${context}/scripts/jquery-1.7.js"></script>
 <link rel="stylesheet" type="text/css" href="${context}/styles/css/superfish.css">
Sign up to request clarification or add additional context in comments.

Comments

1

You need to tell spring where the static resources are

<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />

This would make all files that are under WEB-INF/ and all files that are in jars in the META-INF/web-resources accessible at

http://example.org/app/resources/

See also Spring: DispatcherServlet and static content

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.