简体   繁体   English

Spring BOOT静态资源映射

[英]Spring BOOT static resources mapping

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. 但是,当我启动应用程序时,浏览器无法访问css文件。
I am using Spring Boot 1.1.9 (also as parent in pom.xml). 我正在使用Spring Boot 1.1.9(也作为pom.xml中的父级)。
My Structure is: 我的结构是:

pom.xml pom.xml
src/main/java src / main / java
src/main/resources src / main / resources
src/main/resources/database src / main / resources / database
src/main/resources/templates src / main / resources / templates
src/main/resources/templates/index.html src / main / resources / templates / index.html
src/main/resources/static src / main / resources / static
src/main/resources/static/resources src / main / resources / static / resources
src/main/resources/static/resources/css src / main / resources / static / resources / css
src/main/resources/static/resources/fonts src / main / resources / static / resources / fonts
src/main/resources/static/resources/img src / main / resources / static / resources / img
src/main/resources/static/resources/js 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文件...

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. 尝试改用resources/css/style.css 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 ),然后使用HTML的路径并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM