Spring 3 MVC资源和标记<mvc:resources> </mvc:resources>

时间:2011-11-19 15:58:06

标签: spring spring-mvc resources

我在使用标签时遇到了一些问题(Spring 3.0.5)。 我想将图像添加到我的Web应用程序中,但它不起作用。

这是我的bean配置的一部分:

<mvc:annotation-driven/>
<mvc:default-servlet-handler default-servlet-name="ideafactory"/>
<mvc:resources mapping="/resources/**" location="/, classpath:/WEB-INF/public-resources/" cache-period="10000" />

尝试在jsp文件中添加图像:

<img src="<c:url value="/resources/logo.png" />" alt="Idea Factory" />

首先,我不知道在哪里存储资源(src / main / resources / public-resources?src / main / webapp / WEB-INF / public-resources?)。 其次,这个配置不起作用,我看不到图像。怎么了?

谢谢!

编辑:这里给出的解决方案:Spring Tomcat and static resources and mvc:resources也不起作用...... 添加没有成功。

编辑2:我试图删除mvc:resource标签,只允许使用mvc:default-servlet-handler&gt;一,给了我无限循环和stackoverflow ... o_O(Serving static content with Spring 3

10 个答案:

答案 0 :(得分:26)

发现错误:

最终的xxx-servlet.xml配置:

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

src / webapp / resources / logo.png中的图片

作品!

答案 1 :(得分:24)

<mvc:resources mapping="/resources/**"
               location="/, classpath:/WEB-INF/public-resources/"
               cache-period="10000" />

将资源放在:src/main/webapp/images/logo.png下,然后通过/resources/images/logo.png访问它们。

war中,他们将位于images/logo.png。因此,第一个位置(/)形式mvc:resources将会接收它们。

classpath:/WEB-INF/public-resources/中的第二个位置(mvc:resources)(看起来你使用了一些基于roo的模板)可以公开资源(例如js-files)形式的jar,如果它们位于jar中的目录WEB-INF/public-resources

答案 2 :(得分:5)

通过在$ {webappRoot} / resources目录中提供静态资源来处理对资源/ **的HTTP GET请求的资源建议只需在配置文件中添加以下行:

<resources mapping="/resources/**" location="/resources/" />

它对我有用。

来源(Spring in Action book and http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

答案 3 :(得分:3)

不同的顺序使其有效:)

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

答案 4 :(得分:3)

它对我有用:

<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven />

答案 5 :(得分:3)

正如@Nancom所说

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

为了清晰起见,我们的图片就在

resources/images/logo.png"

标记的位置属性定义了您要提供的静态资源的基本目录位置。它可以是src/main/webapp/resources/images/目录下可用的图像路径;你可能想知道我们为什么只给出 / resources / 作为位置值而不是 src / main / webapp / resources / images / 。这是因为我们将资源目录视为所有资源的基本目录,我们可以在资源目录下有多个子目录来放置我们的图像和其他静态资源文件。

第二个属性映射,仅指示需要映射到此资源目录的请求路径。在我们的例子中,我们已将/resources/**指定为映射值。因此,如果任何web 请求以/resource请求路径启动,则它将映射到资源目录,/**符号表示任何资源文件的递归查找在基础资源目录下。

所以对于url来说 http://localhost:8080/webstore/resource/images/logo.png。因此,在提供此Web请求时,Spring MVC会将/resource/images/logo.png视为请求路径。因此,它会尝试将/resource映射到资源基础目录资源。在此目录中,它将尝试查找URL的剩余路径,即/images/logo.png。由于我们在资源目录下有images目录,因此Spring可以很容易地从images目录中找到图像文件。

所以

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

为我们提供[请求] - &gt; [资源映射]:

http://localhost:8080/webstore/resource/images/logo.png - &gt;在resource/images/logo.png

中搜索

http://localhost:8080/webstore/resource/images/small/picture.png - &gt;在resource/images/small/picture.png

中搜索

http://localhost:8080/webstore/resource/css/main.css - &gt;在resource/css/main.css

中搜索

http://localhost:8080/webstore/resource/pdf/index.pdf - &gt;在resource/pdf/index.pdf

中搜索

答案 6 :(得分:1)

您可以在目录中保留rsouces目录 NetBeans:网页 Eclipse:webapps

文件:dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="controller" />

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <mvc:resources location="/resources/theme_name/" mapping="/resources/**"  cache-period="10000"/>
    <mvc:annotation-driven/>

</beans>

文件:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.css</url-pattern>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

在JSP文件中

<link href="<c:url value="/resources/css/default.css"/>" rel="stylesheet" type="text/css"/>

答案 7 :(得分:0)

我之前也遇到过这个问题。我的情况是我没有将所有62个spring框架jar放入lib文件(spring-framework-4.1.2.RELEASE版本),它确实有效。然后我还将3.0.xsd更改为2.5或3.1进行测试,一切顺利。当然,还有其他因素会影响您的结果。

答案 8 :(得分:0)

@Nanocom的回答对我有用。可能是行必须在最后,或者可能因为必须在某些bean类之后,如下所示:

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler" />    

<mvc:resources mapping="/resources/**" 
               location="/resources/" 
               cache-period="10000" />

答案 9 :(得分:0)

这对我有用

在JSP中,查看图像

offset = sign_ext(imm[11:5] << 5 | imm[4:0])

在dispatcher-servlet.xml中

<img src="${pageContext.request.contextPath}/resources/images/slide-are.jpg">