首页 文章

Spring BOOT静态资源映射

提问于
浏览
0

我知道这个问题经常被问到,但对我来说没什么用 . 我想访问静态资源,所以我有一个模板,我在其中加载“../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 /资源/静态/资源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自动映射/静态/和/资源文件......

1 回答

  • 0

    您的索引是从 / (应用程序的根目录)提供的 . 这意味着您在样式表的路径上不需要 ../ 前缀 . 请尝试使用 resources/css/style.css . 或者,您可以使用绝对路径( /resources/css/style.css ),然后为HTML提供什么路径无关紧要 .

相关问题