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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8195213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:45:23  来源:igfitidea点击:

Spring 3 MVC resources and tag <mvc:resources />

springspring-mvcresources

提问by Nanocom

I'm having some problems with the tag (Spring 3.0.5). I want to add images to my web application, but it doesnt work.

我的标签有一些问题(Spring 3.0.5)。我想向我的 Web 应用程序添加图像,但它不起作用。

Here is part of my beans config:

这是我的 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" />

Trying to add an image in a jsp file:

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

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

First of all, I don't know really where to store the resources (src/main/resources/public-resources? src/main/webapp/WEB-INF/public-resources?). Secondly, this config does not work, I can't see the image. What's wrong?

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

Thanks!

谢谢!

EDIT: the solution given here: Spring Tomcat and static resources and mvc:resourcesdoesn't work either... Added without success.

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

EDIT 2: I tried to remove the mvc:resource tag and let only the mvc:default-servlet-handler> one, gave me infinite loop and stackoverflow... o_O (Serving static content with Spring 3)

编辑 2:我试图删除 mvc:resource 标签,只让 mvc:default-servlet-handler> 一个,给了我无限循环和 stackoverflow...o_O(使用 Spring 3 提供静态内容

回答by Nanocom

Found the error:

发现错误:

Final xxx-servlet.xml config:

最终的 xxx-servlet.xml 配置:

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

Image in src/webapp/resources/logo.png

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

Works!

作品!

回答by Ralph

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

Put the resources under: src/main/webapp/images/logo.pngand then access them via /resources/images/logo.png.

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

In the warthey will be then located at images/logo.png. So the first location (/) form mvc:resourceswill pick them up.

在 中,war它们将位于images/logo.png。因此,第一个位置 ( /) 表单mvc:resources将拾取它们。

The second location (classpath:/WEB-INF/public-resources/) in mvc:resources(looks like you used some roo based template) can be to expose resources (for example js-files) form jars, if they are located in the directory WEB-INF/public-resourcesin the jar.

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

回答by Weslor

Recommendations for resources in order to handle HTTP GET requests for /resources/** by offering static resources in the ${webappRoot}/resources directory is to simply add the following line in the configuration file:

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

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

It has worked for me.

它对我有用。

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

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

回答by Xelian

As said by @Nancom

正如@Nancom所说

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

So for clarity lets our image is in

所以为了清楚起见,让我们的图像在

resources/images/logo.png"

The locationattribute of the tag defines the base directory location of static resources that you want to serve. It can be images path that are available under the src/main/webapp/resources/images/directory; you may wonder why we have given only /resources/as the location value instead of src/main/webapp/resources/images/. This is because we consider the resources directory as the base directory for all resources, we can have multiple subdirectories under resources directory to put our images and other static resource files.

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

The second attribute, mapping, just indicates the request path that needs to be mapped to this resource directory. In our case, we have assigned /resources/**as the mapping value. So, if any web request startswith the /resourcerequest path, then it will be mapped to the resources directory, and the /**symbol indicates the recursive look for any resource files underneath the base resource directory.

第二个属性mapping只是表示需要映射到这个资源目录的请求路径。在我们的例子中,我们已分配/resources/**为映射值。因此,如果任何 web请求/resource请求路径开头,那么它将被映射到资源目录,并且该/**符号表示递归查找基本资源目录下的任何资源文件。

So for url like http://localhost:8080/webstore/resource/images/logo.png. So, while serving this web request, Spring MVC will consider /resource/images/logo.pngas the request path. So, it will try to map /resourceto the resource base directory, resources. From this directory, it will try to look for the remaining path of the URL, which is /images/logo.png. Since we have the images directory under the resources directory, Spring can easily locate the image file from the images directory.

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

So

所以

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

gives us for given [requests] -> [resource mapping]:

为我们提供给定的 [请求] -> [资源映射]:

http://localhost:8080/webstore/resource/images/logo.png-> searches in resource/images/logo.png

http://localhost:8080/webstore/resource/images/logo.png-> 搜索 resource/images/logo.png

http://localhost:8080/webstore/resource/images/small/picture.png-> searches in resource/images/small/picture.png

http://localhost:8080/webstore/resource/images/small/picture.png-> 搜索 resource/images/small/picture.png

http://localhost:8080/webstore/resource/css/main.css-> searches in resource/css/main.css

http://localhost:8080/webstore/resource/css/main.css-> 搜索 resource/css/main.css

http://localhost:8080/webstore/resource/pdf/index.pdf-> searches in resource/pdf/index.pdf

http://localhost:8080/webstore/resource/pdf/index.pdf-> 搜索 resource/pdf/index.pdf

回答by user2077326

Different order make it works :)

不同的顺序使它起作用:)

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

回答by angel.lopezrial

It works for me:

这个对我有用:

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

回答by Shailesh Sonare

You can keep rsouces directory in Directory NetBeans: Web Pages Eclipse: webapps

您可以在目录中保留 rsouces 目录 NetBeans: Web Pages Eclipse: webapps

File: dispatcher-servlet.xml

文件:调度程序-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>

File: web.xml

文件: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>

In JSP File

在 JSP 文件中

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

回答by Itumeleng

This worked for me

这对我有用

In JSP, to view the image

在 JSP 中,查看图像

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

In dispatcher-servlet.xml

在 dispatcher-servlet.xml 中

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

回答by Kyle

I also met this problem before. My situation was I didn't put all the 62 spring framework jars into the lib file(spring-framework-4.1.2.RELEASE edition), it did work. And then I also changed the 3.0.xsd into 2.5 or 3.1 for test, it all worked out. Of course, there are also other factors to affect your result.

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

回答by omaryahir

@Nanocom's answer works for me. It may be that lines have to be at the end, or could be because has to be after of some the beanclass like this:

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