Abdul
Abdul

Reputation: 1208

How to display images in a jsp file in Spring MVC

I am very new to spring. I am using spring 3.29 version, tomcat 7. I need to display an image in .jsp file. I searched a lot. There are plenty of post about this problem. But still i am unable to solve this problem. Please help.

The following is my application structure

The following is my application structure

The following is my spring-servlet.xml file 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: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.2.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.wipro.controller" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
</beans>

I have a page footer.jsp. I need to add an image in this file The following is code

<!-- Here I need to add an image -->
<hr/>  
<p>Copyright  2010-2014 javatpoint.com.</p>  

Upvotes: 1

Views: 9043

Answers (3)

Abdul
Abdul

Reputation: 1208

I got the image in my page. @Pankaj Saboo comments helped a lot. Thanks for all of you.

I have added the below code in spring-servlet.xml

<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="WEB-INF/resources/images/" />

And in footer.jsp I have added the below code

<img src="<c:url value="/images/wipLogo.png" />"/>

Upvotes: 5

Pankaj Saboo
Pankaj Saboo

Reputation: 1185

Try adding the following resources declaration to your Spring configuration:

<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory -->
<resources mapping="/images/**" location="/images/" />   

Alternatively, and more common, is to have a resources folder which contains all your resources (images, css, js, etc...), broken out by sub-directories.

Your configuration would then look like:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

And your resources would be referenced as follows:

<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" />
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script>
<img src="<c:url value="/resources/images/left_arrow.png" />"

Upvotes: 1

Pankaj Saboo
Pankaj Saboo

Reputation: 1185

Add following line in your spring-servlet.xml

<resources mapping="/images/**" location="/images/" />

and on jsp

<link rel="icon"
href="http://<hostname>/projectname/images/imageName"/>

Upvotes: 2

Related Questions