获取 Spring 资源
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有
ServletContextResource
。您可以通过 ServletContext 和相对路径来构造它。There is
ServletContextResource
. You can construct it passing theServletContext
and the relative path.new ClassPathResource("/resources/style/myCSS.css")
怎么样?What about
new ClassPathResource("/resources/style/myCSS.css")
?