在Spring MVC中不显示图像 [英] Not displaying images in Spring MVC

查看:95
本文介绍了在Spring MVC中不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过很多遍了,但是我无法弄清楚问题出在哪里.我在src/main/webapp文件夹(这是一个Maven Web项目)下有images文件夹.我在src/main/webapp/WEBINF/views文件夹中有index.jsp.

I know this question has been asked many times, but I'm not able to figure out what the problem is. I have the images folder under the src/main/webapp folder (this a maven web project). I have the index.jsp in the src/main/webapp/WEBINF/views folder.

我试图像这样访问图像和其他资源,例如css和js:

I'm trying to access the images and other resources like css and js like this:

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

但是图像无法显示.

这是web.xml文件

Here is the web.xml file

<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-dispatcher-servlet.xml文件

Here is the WEB-INF/mvc-dispatcher-servlet.xml file

<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.WebApp;

Here is the Controller package com.ravi.WebApp;

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";

}

}

推荐答案

尝试将以下资源声明添加到您的Spring配置中:

Try adding the following resources declaration to your Spring configuration:

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

或者更常见的是,有一个resources文件夹,其中包含您的所有资源(图像,css,js等),并按子目录划分.

Alternatively, and more common, is to have a resources folder which contains all your resources (images, css, js, etc...), broken out by sub-directories.

您的配置将如下所示:

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

您的资源将被引用如下:

And your resources would be referenced as follows:

<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="" />

这篇关于在Spring MVC中不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆