【问题标题】:Display image in Spring MVC 3.0.2在 Spring MVC 3.0.2 中显示图像
【发布时间】:2016-05-17 19:14:05
【问题描述】:

我正在使用 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" />"/>

这是我的目录结构:

任何关于如何显示我的图像的帮助将不胜感激

【问题讨论】:

    标签: java spring hibernate jsp spring-mvc


    【解决方案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标签的src改为/resources/images/path/to/image

    【讨论】:

    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多