javaspringspring-mvcspring-bootstatic-resource

Spring 4 - unable to deliver static content (bootstrap)


I'm trying to serve some static content (css and js files) in a Spring Boot application. Accordingly to this guide (http://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot), all the contents in the src/main/resources/[static-public] are automatically added to the classpath, and so available. Unfortunately, I manage to load the html page, but I don't understand how to load the css files.

My directory structure is

Project structure

My main Application class is

@Configuration
@EnableWebMvc
@ComponentScan(basePackages={"controller"})
@EnableAutoConfiguration
public class Main extends WebMvcConfigurerAdapter {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

}

and my index.html header looks like

<!-- JQuery -->
<script src="lib/jquery.min.js"></script>
<script src="lib/d3.js" charset="utf-8"></script>
<script src="lib/d3.min.js" charset="utf-8"></script>
<script src="lib/jsnetworkx.js"></script>
<link rel="stylesheet" type="text/css" th:href="@{/lib/bootstrap/css/bootstrap.min.css}"  href="../lib/bootstrap/css/bootstrap.min.css" />
<!-- <link rel="stylesheet" href="lib/bootstrap/css/bootstrap.min.css" /> -->
<link rel="stylesheet" type="text/css" th:href="@{/lib/bootstrap/css/bootstrap-theme.min.css}"  href="../lib/bootstrap/css/bootstrap-theme.min.css" />
<!-- <link rel="stylesheet" href="lib/bootstrap/css/bootstrap-theme.min.css" /> -->
<script src="lib/bootstrap/js/bootstrap.min.js"></script>

<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

Thanks a lot


Solution

  • If you @EnableWebMvc then you switch off all the Boot default MVC configuration. Just remove it.