【问题标题】:Not displaying images in Spring MVC在 Spring MVC 中不显示图像
【发布时间】:2012-10-11 19:41:38
【问题描述】:

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

我正在尝试像这样访问图像和其他资源,如 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-dispatcher-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.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";

}

}

【问题讨论】:

    标签: jsp spring-mvc


    【解决方案1】:

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

    <!-- 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 等),并按子目录进行细分。

    您的配置将如下所示:

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

    【讨论】:

    • 在 Spring 配置中添加资源声明后出现此错误“在名为 'mvc-dispatcher' 的 DispatcherServlet 中找不到具有 URI [/WebApp/] 的 HTTP 请求的映射”
    • 成功了。我所做的更改是我使用 c:url 标签 " alt="" /> 引用了 jsp 中的图像。谢谢帮助。
    【解决方案2】:

    如果使用注解,那么请确保用户

    <mvc:annotation-driven/>
    

    有资源

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

    否则注释控制器将不起作用

    【讨论】:

      【解决方案3】:

      您只需要在 Spring MVC 配置文件中添加一个引用您的图像文件夹

      WEB-INF/spring-context.xml:

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

      【讨论】:

        【解决方案4】:

        请按照图片中的步骤操作.. :)

        第一步:在webapp中创建一个文件夹,而不是在WEB-INF中

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

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

        让我们映射您在 servlet 配置文件中创建的路径,我们在其中处理路径映射 添加此代码: &lt;mvc:resources mapping="/resources/*" location="/resources/" /&gt;

        第 3 步: 添加用于从您在步骤 1 中创建的位置访问图像资源的代码: &lt;img src="/attendance/resources/images/logo.png" width="100px" height="100px"&gt;

        【讨论】:

        • 如果您能简要描述每个步骤,这可能会很有用。
        【解决方案5】:

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

        <mvc:default-servlet-handler />
        

        @BeauGrantham 添加资源映射的建议也将起作用。

        【讨论】:

          【解决方案6】:

          上述建议也对我有用。但如果其他人对关联的命名空间有疑问,我不得不将 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/" />
          

          【讨论】:

            猜你喜欢
            • 2011-08-28
            • 2016-05-17
            • 1970-01-01
            • 1970-01-01
            • 2013-12-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多