更改Spring-boot静态Web资源的位置? [英] Change Spring-boot static web resources location?

查看:80
本文介绍了更改Spring-boot静态Web资源的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于本教程:

http://spring.io/guides/gs/serving-web-content /

我可以使用百里香叶在该位置提供视图

I can use the thymeleaf to serve the view in the location

/src/main/resources/templates/

但是,我必须将静态Web内容(css,js)放在另一个位置:

However, I have to put the static web contents (css, js) in another location:

/src/main/webapp/resources

并像这样链接 hello.html 中的资源:

And linking the resources in the hello.html like that:

<link href="resources/hello.css" />
<script src="resources/hello.js"></script>

目录结构为:

└── src
    └── main
        └── java
            └── hello.java
        └──resources
            └──templates
               └──hello.html
        └──webapp
            └──resources
               └──hello.js
               └──hello.css

问题是,当我运行Web服务器时,静态文件的链接有效.但是,如果我以离线模式打开 html 文件,则链接会断开.

The problem is that the links of the static files are work when I run the web server. But if I open the html files in offline mode, the links are broken.

我可以从中移动静态资源吗?

Could I move the static resource from

/src/main/webapp/resources

收件人:

/src/main/resources/templates/resources

新的目录结构如下:

└── src
    └── main
        └── java
            └── hello.java
        └──resources
            └──templates
               └──hello.html
               └──resources
                  └──hello.js
                  └──hello.css

我尝试过,但是不起作用.

I tried it but is doesn't work.

推荐答案

尝试src/main/resources/static(或src/main/resources/publicsrc/main/resources/resources).所有这些都由Spring Boot自动配置注册.

Try src/main/resources/static (or src/main/resources/public or src/main/resources/resources). All those are registered by Spring Boot autoconfig.

这篇关于更改Spring-boot静态Web资源的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆