图像没有出现在Spring MVC中

时间:2014-09-15 12:33:33

标签: java spring jsp spring-mvc

我刚开始使用spring mvc,我已经成功设置了一个欢迎页面但是我添加的图片没有显示,在阅读了很多文章后我设法修改了我的spring-dispatcher-servlet.xml 如下所示

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



    <context:component-scan base-package="com.diluks.controller" />

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


    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />

</beans>

我的index.jsp页面如下所示

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Page Title</title>
</head>
<body>
<h1>${welcomemessage}</h1>
<img alt="Image" src="/resources/images/testimage.jpg">
</body>
</html>

它正确地给了我欢迎消息,但它不显示图像。请帮帮我。

2 个答案:

答案 0 :(得分:1)

将image src属性值更改为以下内容:

<img alt="Image" src="${pageContext.request.contextPath}/resources/images/testimage.jpg">

试一试。

答案 1 :(得分:1)

如果您使用的是JSTL,请尝试

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

如果您不使用,则可以尝试

<img alt="Image" src="${pageContext.request.contextPath}/resources/images/testimage.jpg"/>