【问题标题】:Spring 3 MVC resources and tag <mvc:resources />Spring 3 MVC 资源和标签 <mvc:resources />
【发布时间】:2012-01-01 23:19:54
【问题描述】:

我在使用标签时遇到了一些问题(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> 一个,给了我无限循环和 stackoverflow... o_O (Serving static content with Spring 3)

【问题讨论】:

    标签: spring spring-mvc resources


    【解决方案1】:

    正如@Nancom 所说

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

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

    resources/images/logo.png"
    

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

    第二个属性ma​​pping,只是表示需要映射到这个resources目录的请求路径。在我们的例子中,我们指定了/resource/** 作为映射值。因此,如果任何网络请求以/resource 请求路径开始,那么它将映射到resources 目录,而/** 符号表示递归查找目录下的任何资源文件基本resources 目录。

    所以对于像这样的网址 http://localhost:8080/webstore/resource/images/logo.png。因此,在服务这个 Web 请求时,Spring MVC 会将/resource/images/logo.png 视为请求路径。因此,它会尝试将/resource 映射到location 属性指定的基本目录resources。从该目录中,它将尝试查找 URL 的剩余路径,即/images/logo.png。由于我们在resources目录下有images目录,所以Spring可以很容易地从images目录下找到图片文件。

    所以

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

    为我们提供给定的 [requests] -> [resource mapping]:

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

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

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

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

    【讨论】:

    • 你让我浪费了一个小时思考为什么你的答案不起作用..,它的mapping="/resources/**"'不是mapping="/resource/**"
    • 我不同意你的看法。在这种情况下在guthub中搜索:github.com/…
    • 我的意思是您将其声明为 location="/resources/" 然后 mapping="/resource/**"/。不见了s哈哈。
    【解决方案2】:

    这对我有用

    在JSP中,查看图片

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

    在 dispatcher-servlet.xml 中

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

    【讨论】:

      【解决方案3】:

      您可以在 Directory 中保留 rsouces 目录 NetBeans:网页 Eclipse:网络应用程序

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

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

          它对我有用。

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:
              <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 的第一个位置 (/) 将接收它们。

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

              【讨论】:

              • 会帮助我,即使它实际上是 src/main/webapp/resources/images/logo.png
              • @Nanocom:我引入了 images 文件夹以使其更清晰。但是url的“resources”路径是被mvc-tag--mapping“消耗”的,所以一定不能再在文件夹的路径中了!除非您按照您在自己的答案中描述的方式进行操作。
              【解决方案8】:

              它对我有用:

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

              【讨论】:

                【解决方案9】:

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

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

                【讨论】:

                  【解决方案10】:

                  发现错误:

                  最终的 xxx-servlet.xml 配置:

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

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

                  有效!

                  【讨论】:

                    猜你喜欢
                    • 2012-10-16
                    • 1970-01-01
                    • 2011-06-14
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多