如何使用java spring mvc显示图像

时间:2017-07-26 17:18:01

标签: java html spring image spring-mvc

我正在尝试在maven项目中使用spring MVC显示图像。

这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.simslay</groupId>
  <artifactId>tutoriel-web-spring</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>tutoriel-web-spring Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
        <jstl-version>1.2</jstl-version>
  </properties>

  <dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>

    <!-- JSTL dependency -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>${jstl-version}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>tutoriel-web-spring</finalName>
  </build>
</project>

这是我的web.xml:

    <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

        <!-- Declaration de la servlet de Spring et de son mapping -->
    <servlet>
        <servlet-name>servlet-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>servlet-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我的dispacher-servlet.xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="ISO-8859-1" />
    </bean>

    <context:component-scan base-package="com.developpez.rpouiller" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/vues/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <mvc:annotation-driven />

    <!-- Specifying the Resource location to load JS, CSS, Images etc -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
</beans>

我在该文件夹中有一张图片:src / main / resources / images / image.jpg

我尝试以不同的方式在src / main / webapp / vues / bonjour.jsp中显示上一张图片:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" isELIgnored="false"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>bonjour</title>
    </head>
    <body>
        <div>
            <img alt="image" src="/resources/images/image.jpg" />
            <img src="/tutoriel-web-spring/resources/images/image.jpg" alt="image" />
            <img src="<c:url value="/resources/images/image.jpg" />" alt="image" />
            <img src="<c:url value="/images/image.jpg" />" alt="image" />
        </div>

    </body>
</html>

但无法显示它(仅显示替代文字)。谢谢你的帮助!

4 个答案:

答案 0 :(得分:0)

如果您希望能够引用图像的绝对路径,无论用于调用JSP的URL如何,您都必须使用绝对路径src="${pageContext.request.contextPath}/resources/images/image.jpg",因此src来自你的maven项目的上下文根。

答案 1 :(得分:0)

在Google Chrome中运行程序,按F12,导航到图像并查看图像的路径。如果图像路径正确且图像仍未显示,请将图像再次复制到图像文件夹。

答案 2 :(得分:0)

尝试像这样设置
$( '#标识')ATTR( 'SRC', '/资源/图像/' + imageName)。 //动态

答案 3 :(得分:0)

我通过将我的图片文件夹放在webapp / resources中解决了我的问题。图片标记为:

<img src="<c:url value="/resources/images/image.jpg" />" alt="image" />