Java Spring MVC中未显示图像
我是SpringMVC的新手,我已经成功地设置了一个欢迎页面,但是在阅读了大量文章之后,添加到其中的图像没有显示出来,我已经设法修改了我的spring-dispatcher-servlet.xml 如下Java Spring MVC中未显示图像,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我是SpringMVC的新手,我已经成功地设置了一个欢迎页面,但是在阅读了大量文章之后,添加到其中的图像没有显示出来,我已经设法修改了我的spring-dispatcher-servlet.xml 如下 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.spring
<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>
<%@ 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>
/WEB-INF/
.jsp
我的index.jsp页面如下所示
<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>
<%@ 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>
我的页面标题
${welcomemessage}
它正确地给了我欢迎信息,但没有显示图像。请在这方面帮助我。将图像src属性值更改为以下值:
<img alt="Image" src="${pageContext.request.contextPath}/resources/images/testimage.jpg">
尝试一下。如果您正在使用JSTL,请尝试
<img alt="Image" src="<c:url value="/resources/images/testimage.jpg"/>"/>
“/>
如果你没有使用,那么你可以试试
<img alt="Image" src="${pageContext.request.contextPath}/resources/images/testimage.jpg"/>
您检查过浏览器吗?浏览器中img的src是什么?它说尝试更改代码jsp,如${pageContext.request.contextPath}/参考资料/images/testimage.jpg,然后查看您在浏览器中得到了什么。现在它给了我图像,但仍然没有图像显示。您可以尝试直接将图像url点击到浏览器中。yourDomain/MyWebApp/resources/images/testimage.jpg并检查图像是否已加载。