繁体   English   中英

Spring BOOT静态资源映射

[英]Spring BOOT static resources mapping

我知道这个问题经常被问到,但是对我来说没有任何作用。 我想访问静态资源,因此我有一个模板,其中加载了“ ../resources/css/style.css”。

但是,当我启动应用程序时,浏览器无法访问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

(请参阅http://i62.tinypic.com/of84jt.png

我的控制器:

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";
  }
}

主要:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Prototype {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(Prototype.class, args);
  }
}    

我读过springboot自动映射/ static /和/ resources文件...

您的索引是通过/ –应用程序的根目录提供的。 这意味着您不需要样式表路径中的../前缀。 尝试改用resources/css/style.css 另外,您可以使用绝对路径( /resources/css/style.css ),然后使用HTML的路径并不重要。

暂无
暂无

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

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