如何在Spring MVC应用程序中看到静态图像?

时间:2015-12-04 19:31:36

标签: java spring spring-mvc resources

我无法在Spring MVC应用程序中看到静态图像。

如果我添加

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

进入dispatcher-servlet.xmlapplicationContext.xml然后我失去了执行控制器的能力。

我当前的dispatcher-servlet.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.inthemoon.snippets.springmvc" />

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

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

当我打开/hello网址时,我在日志中收到错误:

org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/hello] in DispatcherServlet with name 'dispatcher'

如果我删除resources指令,我会在日志中看到以下消息:

04-Dec-2015 22:30:15.687 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
04-Dec-2015 22:30:16.012 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.registerHandler Mapped URL path [/hello] onto handler 'helloWorldController'
04-Dec-2015 22:30:16.013 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.registerHandler Mapped URL path [/hello.*] onto handler 'helloWorldController'
04-Dec-2015 22:30:16.013 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.registerHandler Mapped URL path [/hello/] onto handler 'helloWorldController'

如果存在resources指令,则不存在这些消息。

1 个答案:

答案 0 :(得分:0)

如果要使用<mvc:annotation-driven>语句,则必须插入<mvc:resources ...>

有关详细信息,请参阅以下问题&amp;回答:Using mvc:resources problem