-1

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-scan base-package="com.pack1" />

<mvc:annotation-driven />
   <mvc:resources mapping="/resources/**" location="/resources/" />    
<context:annotation-config />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/display/"/>
    <property name="suffix" value=".jsp" />
</bean>
 <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  p:locations="/WEB-INF/jdbc.properties" />
 <bean id="dataSource"   class="org.apache.commons.dbcp.BasicDataSource"    destroy-method="close"
  p:driverClassName="${jdbc.driverClassName}"   p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"  p:password="${jdbc.password}" />

  <bean id="sessionFactory"    
             class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
             <property name="dataSource" ref="dataSource" />
             <property name="configLocation">     
                  <value>classpath:hibernate.cfg.xml</value>     
             </property>
            <property name="hibernateProperties">
            <props>
                   <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                   <prop key="hibernate.show_sql">true</prop>
            </props>
    </property>
</bean>

<tx:annotation-driven />

<bean id="transactionManager"    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
</bean>

Here is my web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>Project</display-name>
  <welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>

  </welcome-file-list>
  <servlet>
          <servlet-name>spring</servlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
     </web-app>

And here is the jsp code:

    <img src="<c:url value="/images/ciscoimg.jpg" />"/>

Here is my directory structure:

Directory Structure

Any help would be highly appreciated as to how I can display my image

HarshitG
  • 53
  • 2
  • 10

1 Answers1

1

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

Soeun Park
  • 349
  • 3
  • 14