java 图像未显示在带有弹簧的 jsp 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15299516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:08:52  来源:igfitidea点击:

image not displayed in jsp with spring

javajspurlmavenspring-mvc

提问by Lakshmi

I am new to spring with jsp i am basically trying to display an image in my jsp

我是 jsp 的新手,我基本上是想在我的 jsp 中显示图像

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>JSP-Page</title>
    </head>
    <body>
        <img src="images/top.jpg">
    </body>
</html> 

Spring-servlet xml :

弹簧 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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Enable annotation driven controllers, validation etc... -->
    <mvc:annotation-driven />
 <mvc:resources location="/images/" mapping="/images/**"/>

    <!-- Application controllers package -->
    <context:component-scan base-package="net.ignou.onlinetest.controller" />

    <bean id="viewoseesolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
                          value="jdbc:mysql://localhost:3306/online_test" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="sessionFactory"
              class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>net.ignou.onlinetest.domain.Question</value>
                <value>net.ignou.onlinetest.domain.Student</value>
                <value>net.ignou.onlinetest.domain.Answer</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">none</prop>
            </props>
        </property>
    </bean>

    <bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>

    <bean id="questionDao" class="net.ignou.onlinetest.dao.daoImpl.QuestionDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="loginDao" class="net.ignou.onlinetest.dao.daoImpl.LoginDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="answerDao" class="net.ignou.onlinetest.dao.daoImpl.AnswerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="service" class="net.ignou.onlinetest.service.serviceImpl.ServiceImpl">
        <property name="questionDao" ref="questionDao"/>
    </bean>

</beans>

My web.xml

我的 web.xml

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Person Detail</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
   </web-app>

I am using maven.My image file is located at

我正在使用 maven。我的图像文件位于

online-test\src\main\webapp\images and my jsp page is in

online-test\src\main\webapp\images 和我的 jsp 页面在

online-test\src\main\webapp\web-inf\views

在线测试\src\main\webapp\web-inf\views

I also tried replacing the src as <img src="../../images/top.jpg">but it didnt work i also tried moving my jsp and image to webapp folder directly also but no use. Is there anything im doing wrong how exactly does spring handle img requests?

我也尝试更换 src as <img src="../../images/top.jpg">但它没有用我也尝试将我的 jsp 和图像直接移动到 webapp 文件夹,但没有用。有什么我做错了 spring 如何处理 img 请求?

回答by Sotirios Delimanolis

What you want to do is add this line in your spring servlet-context xml configuration.

您想要做的是在您的 spring servlet-context xml 配置中添加这一行。

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

The mvcxml namespace is at xmlns:mvc="http://www.springframework.org/schema/mvc"

mvcXML命名空间是在xmlns:mvc="http://www.springframework.org/schema/mvc"

The resourcestag basically tells Spring to handle requests to the declared mapping by serving up a named file from the declared location, instead of going through your controller stack. The mapping can also be used to serve up any resource: css, js, pdf, etc.

resources标签基本上告诉 Spring 通过从声明的位置提供命名文件来处理对声明映射的请求,而不是通过控制器堆栈。该映射还可用于提供任何资源:css、js、pdf 等。

You don't need multiple <mvc:resources>tags, just one with a generic mapping, eg. /resources/**, and a comma separated list of locations, eg. /resources/css/, /resources/js/.

您不需要多个<mvc:resources>标签,只需一个具有通用映射的标签,例如。/resources/**,和一个逗号分隔的位置列表,例如。/resources/css/、/resources/js/。

<mvc:resources mapping="/resources/**" location="/resources/images/, /resources/css/" />

The <resources>tag was introduced in spring 3.0.4, so you need at least that version of Spring and of the xsd. You can use

<resources>标签是在 spring 3.0.4 中引入的,因此您至少需要该版本的 Spring 和 xsd。您可以使用

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd`

Also, as JB Nizet says, you should reference your image as

此外,正如 JB Nizet 所说,您应该将您的图像引用为

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

for a relative path.

对于相对路径。

回答by JB Nizet

The location of the JSP file is irrelevant when it comes to URL paths. What matters is the location of the page displayed by your browser, i.e. the address of the page displayed in the address bar of your browser.

JSP 文件的位置与 URL 路径无关。重要的是浏览器显示页面的位置,即浏览器地址栏中显示的页面地址。

So if the address of the page is /webapp/foo/bar/someAction.htmland the image is at /webapp/images/top.jpg, the path should be /webapp/images/top.jpg(absolute path, preferrable and clearer), or ../../images/top.jpg(relative path, harder to refactor if you move files or change URLs).

因此,如果页面的地址是/webapp/foo/bar/someAction.html并且图像在/webapp/images/top.jpg,则路径应该是/webapp/images/top.jpg(绝对路径,首选且更清晰),或../../images/top.jpg(相对路径,如果移动文件或更改 URL,则更难重构)。

My advice: always use absolute paths, and use the JSTL's c:url tag to avoid hard-coding the context path of the webapp:

我的建议:始终使用绝对路径,并使用 JSTL 的 c:url 标记来避免对 webapp 的上下文路径进行硬编码:

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

The above line will always work.

上面的行将始终有效。