Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaSpringMVC显示图像_Java_Html_Spring_Image_Spring Mvc - Fatal编程技术网

如何使用JavaSpringMVC显示图像

如何使用JavaSpringMVC显示图像,java,html,spring,image,spring-mvc,Java,Html,Spring,Image,Spring Mvc,我试图在maven项目中用SpringMVC显示一个图像 这是我的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">

我试图在maven项目中用SpringMVC显示一个图像

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

但无法显示它,只能显示备用文本。谢谢你帮助我

如果您希望能够引用图像的绝对路径,无论调用JSP使用的URL是什么,都必须使用abolute路径src=${pageContext.request.contextPath}/resources/images/image.jpg,这样src将从maven项目的上下文根开始运行。

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

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

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

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

我刚刚尝试了这个/resources/images/image.jpg alt=image/>,但没有结果。嗯,这不是我发布的内容。应该是这样的:。我有以下错误:javax.servlet.jsp.JspException无法解析为类型,无法将类型javax.servlet.jsp.PageContext解析为某个类型,并且无论出现何种错误,图像都不会显示。我已通过添加一些依赖项解决了这些错误,图像仍然没有显示。resources文件夹是否位于src/main/resources下?如果是这样,请尝试将images文件夹移动到webapp文件夹中,并将路径更改为${pageContext.request.contextPath}/images/ima‌​通用电气公司