有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring引导静态资源映射

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


共 (1) 个答案

  1. # 1 楼答案

    您的索引将从应用程序的根目录/提供。这意味着在样式表的路径上不需要../前缀。试着改用resources/css/style.css。或者,您可以使用一个绝对路径(/resources/css/style.css),然后不管哪个路径为HTML服务