1

I am new in spring mvc, i want to store some images as static resources in spring mvc and call them whenever required, I try this:

this is my dispatcher servlet file:

   <?xml version='1.0' encoding='UTF-8' ?>

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

    <context:component-scan base-package="sajjad.htlo"/> 
    <mvc:annotation-driven/>

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
        <property name="prefix"> 
            <value>/WEB-INF/jsp/</value> 
        </property> 
        <property name="suffix"> 
            <value>.jsp</value> 
        </property> 
    </bean>
</beans>

And this is Controller:

@Controller
public class IndexController {

    @RequestMapping(value = "/index.html")
    public ModelAndView indexPage() {
        return new ModelAndView("index");
    }
}

And this is 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">
    <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>
</web-app>

View :

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome</title>
    </head>

    <body>
        this is index page
        <img src="/resources/pics/t3.jpg" />
    </body>
</html>

But when i run app, i can just see the word, and image dot displayed.

This is my project structure:

enter image description here

3 Answers3

1

In your Web Application Context Config (mvc-dispatcher-servlet.xml) add:

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

<mvc:default-servlet-handler/>

Put your files in webapp/static/[some folder]

Package it with Maven

In JSPs, reference resources using <c:url/> like:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<link rel="stylesheet" href="<c:url value='/static/styles/site.css'/>">
Neil McGuigan
  • 43,981
  • 12
  • 119
  • 145
0

The 'resources' folder is not available to the web app.

Add a "static" folder under "WEB-INF" and and "img" folder under that. Place your images there.

Like so: /WEB-INF/static/img

Change the resource mapping in the servlet to:

<mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>

Change "index.html" reference to the image:

<img src="/static/img/t3.jpg" />
RoyBass
  • 26
  • 2
0

There is one quick solution that worked for me: I just put '.' before there 'pic' folder name, that says to my HTML to search for this picture folder in the root of the resources folder configured in the dispatcher servlet file:

<img src="./pics/t3.jpg" />

And resources folder has to be in /WEB-INF/ folder of course.

Renat Gatin
  • 5,402
  • 4
  • 34
  • 55