java Spring BOOT 静态资源映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26891186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring BOOT static resources mapping
提问by laserbeamer
I know this question is ask often, but nothing of it works for me. I want to access the static resources, so I have a template in which I load "../resources/css/style.css".
我知道这个问题经常被问到,但对我来说没有任何作用。我想访问静态资源,所以我有一个模板,我在其中加载“../resources/css/style.css”。
But when I start the application the browser can not access the css file.
I am using Spring Boot 1.1.9 (also as parent in pom.xml).
My Structure is:
但是当我启动应用程序时,浏览器无法访问 css 文件。
我正在使用 Spring Boot 1.1.9(也在 pom.xml 中作为父级)。
我的结构是:
pom.xml
src/main/java
src/main/resources
src/main/resources/database
src/main/resources/templates
src/main/resources/templates/index.html
src/main/resources/static
src/main/resources/static/resources
src/main/resources/static/resources/css
src/main/resources/static/resources/fonts
src/main/resources/static/resources/img
src/main/resources/static/resources/js
pom.xml
src/main/java
src/main/resources
src/main/resources/database
src/main/resources/templates
src/main/resources/templates/index.html
src/main/resources/static
src/main/resources /static/resources
src/main/resources/static/resources/css
src/main/resources/static/resources/fonts
src/main/resources/static/resources/img
src/main/resources/static/resources/js
(see http://i62.tinypic.com/of84jt.png)
(见http://i62.tinypic.com/of84jt.png)
My Controller:
我的控制器:
package prototype.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class IndexController {
@RequestMapping("/")
public String stdRedirect(){
return "redirect:/index";
}
@RequestMapping(value="/index", method=RequestMethod.GET)
public String index(){
return "index";
}
}
Main:
主要的:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Prototype {
public static void main(String[] args) throws Exception {
SpringApplication.run(Prototype.class, args);
}
}
I read that springboot automatic maps /static/ and /resources files...
我读到 springboot 自动映射 /static/ 和 /resources 文件...
采纳答案by Andy Wilkinson
Your index is being served from /
– the root of the app. This means that you don't need the ../
prefix on the stylesheet's path. Try using resources/css/style.css
instead. Alternatively you could use an absolute path (/resources/css/style.css
), then it doesn't matter what path is serving the HTML.
您的索引是从/
应用程序的根目录提供的。这意味着您不需要../
样式表路径上的前缀。尝试使用resources/css/style.css
。或者,您可以使用绝对路径 ( /resources/css/style.css
),那么提供 HTML 的路径无关紧要。