【问题标题】:How to display an image with java spring mvc如何使用 java spring mvc 显示图像
【发布时间】:2018-01-02 03:59:19
【问题描述】:

我正在尝试在 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>

我的调度器-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>

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

【问题讨论】:

    标签: java html spring image spring-mvc


    【解决方案1】:

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

    【讨论】:

    • 我刚试过这个(/resources/images/image.jpg" alt="image" />)但没有结果.
    • 好吧,这不是我发布的。你应该有这样的:&lt;img src="${pageContext.request.contextPath}/resources/images/image.jpg" alt = "image" /&gt;.
    • 我遇到了这些错误:javax.servlet.jsp.JspException 无法解析为类型,类型 javax.servlet.jsp.PageContext 无法解析为类型,无论此错误如何,图像不会显示。
    • 我已经通过添加一些依赖解决了错误,但图像仍然没有显示。
    • src/main/resources 下是资源文件夹吗?如果是这样,请尝试将图像文件夹移动到 webapp 文件夹并将路径更改为 ${pageContext.request.contextPath}/images/ima‌​ge.jpg
    【解决方案2】:

    在 Google Chrome 中运行程序,按 F12,导航到图像并查看图像的路径。如果图片路径正确,但图片仍然没有显示,请重新将图片复制到图片文件夹中。

    【讨论】:

      【解决方案3】:

      尝试这样设置
      $('#id').attr('src','/resources/images/'+imageName); //动态

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-20
          • 2011-08-28
          • 2014-12-11
          • 2022-01-08
          • 2016-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多