图像没有弹簧显示在jsp中

Lak*_*hmi 6 java url jsp spring-mvc maven

我是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> 
Run Code Online (Sandbox Code Playgroud)

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: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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

我正在使用maven.My图像文件位于

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

在线测试的\ src \主\ web应用\ WEB-INF \意见

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

Sot*_*lis 12

您要做的是在spring servlet-context xml配置中添加此行.

<mvc:resources mapping="/images/**" location="/images/" />
Run Code Online (Sandbox Code Playgroud)

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

resources标签基本上告诉Spring从申报位置服务了一个名为文件,而不是通过你的控制器栈去处理请求的声明映射.映射还可用于提供任何资源:css,js,pdf等.

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

<mvc:resources mapping="/resources/**" location="/resources/images/, /resources/css/" />
Run Code Online (Sandbox Code Playgroud)

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

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd`
Run Code Online (Sandbox Code Playgroud)

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

<img src="<c:url value='/images/top.jpg'/>"/>
Run Code Online (Sandbox Code Playgroud)

对于相对路径.