HarshitG February 2016

Display image in Spring MVC 3.0.2

I'm using Spring 3.0.2 When I try to display an image, all I get is a box with a red cross. I have also tried using

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

but i get an error
"The matching wildcard is strict, but no declaration can be found for element 'mvc:resources' "

I have also tried adding the Spring V4 jar but they are now conflicting with the previous jars.

Here is my spring-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd " >

<context:component        

Answers


Soeun Park February 2016

Your mvc:resources setting does not match with your img src tag. First of all, change

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

to

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

and then move your static files(or folders) to under the /WEB-INF/resources directory.

And then correct the img tag's src as starting with /resources/path/to/image.

But if your images do not need to be secured, you can just let the static files as them be and then change mvc:resources to

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

and change img tag's src to /resources/images/path/to/image

Post Status

Asked in February 2016
Viewed 1,182 times
Voted 12
Answered 1 times

Search




Leave an answer