【问题标题】:Getting a Spring resource获取 Spring 资源
【发布时间】:2011-01-17 19:39:53
【问题描述】:

我正在尝试使用 Spring 提供的资源读取 css 文件。

我的应用程序如下所示:

    • src/com 这里是我在包中的类
  • 网页内容
    • WebContent/resources/style/myCSS.css --> 我想读的css
    • WebContent/WEB-INF --> 这是我的 application-context.xml

我可以通过执行以下操作获取 css 并阅读它:

UrlResource file = new UrlResource("http://localhost:8080/myApp/resources/style/myCSS.css");

但这取决于服务器和应用程序名称。 我试图通过资源接口的其他实现来做到这一点,但找不到该文件,因为我不知道如何写路径。我试过这个:

FileSystemResource file = new FileSystemResource("/WebContent/resources/style/myCSS.css");

我也尝试过使用通配符,但它也找不到该文件。

ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath*:/WEB-INF/application-context-core.xml");
Resource file = ctx.getResource("file:**/myCSS.css");

获取css的路径应该怎么写。

谢谢。

【问题讨论】:

  • 谢谢,它使用了 ServletContextResource file = new ServletContextResource(servletContext, "/resources/style/myCSS.css");

标签: spring resources path relative-path


【解决方案1】:

ServletContextResource。您可以通过ServletContext 和相对路径来构造它。

【讨论】:

    【解决方案2】:

    new ClassPathResource("/resources/style/myCSS.css") 呢?

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 2019-03-02
      相关资源
      最近更新 更多