1

I'm working on a spring-mvc application which required to load static location from webapp folder and file system. I followed documentation, this answer and many more tutorials and configured following.

    <mvc:resources mapping="/images/**" location="/img/" cache-period="10000"/>
    <mvc:resources mapping="/resources/**" location="file://D:/my-apps/sample-mvc/images/" cache-period="10000"/>

    <mvc:annotation-driven/>

Directory structure is as follows.

enter image description here

web.xml ->

<web-app 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_2_5.xsd"
     version="2.5">

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

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name> <!-- Configuration location for servlet context -->
        <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</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>*.css</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>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.woff</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.woff2</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name> <!-- Configuration location for main application context -->
    <param-value>
        classpath:application-context.xml
    </param-value>
</context-param>

But it's not serving any file in the given two URLs. (localhost:8080/sample-mvc-app/images/a1-img-001.jpg, or localhost:8080/sample-mvc-app/resources/test-uploaded-img-1.jpg)

Testing further I found out that, if I would use the URL with original directory name (localhost:8080/sample-mvc-app/img/a1-img-001.jpg) the image is loading in the browser.

Could anyone please help me to find out whats wrong here?

Additionally, Servlet container: tomcat

1 Answer 1

1

Try removing default servlet mappings from web.xml. Default servlet is an servlet which is created in every web app. In tomcat it serves static resources.

Sign up to request clarification or add additional context in comments.

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.