在Spring MVC 3.0.2中显示图像

时间:2016-02-08 07:27:03

标签: java spring hibernate jsp spring-mvc

我正在使用Spring 3.0.2 当我尝试显示图像时,我得到的只是一个带红叉的盒子。我也尝试过使用

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

但是我收到了错误 “匹配的通配符是严格的,但是没有找到元素'mvc:resources'”

的声明

我还尝试添加Spring V4 jar,但它们现在与以前的jar冲突了。

这是我的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>

这是我的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>

这是jsp代码:

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

这是我的目录结构:

Directory Structure

对于如何显示我的图像,我们将非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

您的mvc:resources设置与您的img src标记不匹配。 首先,改变

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

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

然后将静态文件(或文件夹)移动到/ WEB-INF / resources目录下。

然后将img标记的src更正为/resources/path/to/image

但是如果您的图像不需要保护,您可以让静态文件保持原样,然后将mvc:resources更改为

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

并将img tag的src更改为/resources/images/path/to/image