html放入到templates文件夹下
dao和entities
css,js等静态资源放到static文件夹下
把index定为首页的方法:
方法一:在Controller中添加方法定位到首页,{"/","/index.html"}
代表多种请求
@RequestMapping({"/","/index.html"})
public String index(){
return "index";
}
第二种方法:添加ViewController
视图解析器
MyMvcConfig 继承 WebMvcConfigurer
重写addViewControllers方法
package com.nyh.springboot.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author nyh
* @create 2018-11-05 20:11
**/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
}
引用webjars
先导入webjars
maven依赖到webjars官网去复制
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>
</dependency>
然后到html
用thymeleaf模板引擎的语法
<html lang="en" xmlns:th="http://www.thymeleaf.org">
然后通过th:href引进来
/
代表当前项目下
<link th:href="@{/webjars/bootstrap/4.1.3/css/bootstrap.css}" rel="stylesheet">
里面的路径可以参照jar包中的文件夹路径