获取 Spring 资源

发布于 2024-08-21 10:51:32 字数 867 浏览 1 评论 0原文

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

我的应用程序如下所示:

  • src
    • src/com 这里是我的包内的类
  • WebContent 包中的类
    • 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的路径应该怎么写呢?

谢谢。

I'm trying to read a css file with the Resources provided by Spring.

My application looks like this:

  • src
    • src/com herer my classes inside packages
  • WebContent
    • WebContent/resources/style/myCSS.css --> the css I want to read
    • WebContent/WEB-INF --> here is my application-context.xml

I can get the css and read it by doing something like this:

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

but it depends on the server and aplication names.
I've tried to do it by other implementations of Resource Interface, but the file is not found cause I can't find out how to wite the path. I've tried with this:

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

I also tried with wildcards, but it doesn't find the file either.

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

How should I write the path to get the css.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子是时光的心 2024-08-28 10:51:32

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

There is ServletContextResource. You can construct it passing the ServletContext and the relative path.

篱下浅笙歌 2024-08-28 10:51:32

new ClassPathResource("/resources/style/myCSS.css") 怎么样?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文