2012-10-11 77 views
8

我知道这个问题已被多次询问,但我无法弄清楚问题所在。我有src/main/webapp文件夹下的images文件夹(这是一个maven web项目)。我在src/main/webapp/WEBINF/views文件夹中有index.jsp。在Spring MVC中不显示图像

我试图访问图像和其他资源,如CSS和JS是这样的:

<img src="/images/left_arrow.png" alt="" />

但却没有显示的图像。

这里是web.xml文件

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

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
</web-app> 

这里是WEB-INF/MVC-调度-servlet.xml文件

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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"> 

<context:component-scan base-package="com.ravi.WebApp" /> 

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

</beans> 

这里是控制器 包com.ravi。 Web应用程序;

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 

@Controller 
public class HelloController { 

@RequestMapping("/") 
public String printWelcome(Model model) { 
    return "index"; 

} 

} 

回答

11

尝试添加以下资源声明Spring配置:

<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory --> 
<resources mapping="/images/**" location="/images/" />  

或者,更常见的,是有一个包含所有资源(图片,CSS,JS等一resources文件夹...),通过子目录分解。

那么你的配置是这样的:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

和你的资源将如下参考:

<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" /> 
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script> 
<img src="<c:url value="/resources/images/left_arrow.png" />" alt="" /> 
+0

我在Spring配置中添加资源声明之后得到这个错误“没有找到映射与URI [/ Web应用程序/]在DispatcherServlet的HTTP请求名为‘MVC-调度’ “ – Ravi

+0

它的工作。我所做的更改是我在jsp中使用c:url标记“alt =”“/>引用图像。感谢您的帮助。 – Ravi

0

如果你想保持WEB-INF文件夹外的静态资源在web根目录并希望容器处理静态资源请求,则应将其添加到您的应用程序上下文文件中:

<mvc:default-servlet-handler /> 

@BeauGrantham添加资源映射的建议也可行。

1

你只需要添加一个引用上Spring MVC的配置文件

WEB-INF /弹簧context.xml的图像文件夹:

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

如果使用注释,然后确保用户

<mvc:annotation-driven/> 

与资源

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

其他注释控制器不会工作

0

上述建议对我也有效。但是,如果任何人有相关的名称空间的麻烦,我不得不MVC的部分添加到mvc-dispatcher-serlvet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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 "> 

... 

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

请遵循在这张照片的步骤.. :)

步骤1:创建Web应用程序中的一个文件夹但不在WEB-INF中

创建资源然后图像然后存储您的图像。 的webapp /资源/图像/ fileName.jpg

第2步:现在你已经创建了文件夹

让我们映射你servlet的配置文件中创建,我们处理的映射路径路径 添加以下代码: <mvc:resources mapping="/resources/*" location="/resources/" />

第3步: 添加代码,从你在ST创建的位置访问图像资源EP 1: <img src="/attendance/resources/images/logo.png" width="100px" height="100px">

Spring MVC access Image in JSP

+0

如果您可以简要描述每个步骤,它可能会很有用。 – lmiguelvargasf