1。新建spring-boot项目。File-New-Project 选择 Spring Initializr -填写包名项目名-选择Web勾选Spring Web
2.新建完成后pom文件引用thymeleaf模板(用于解析html和静态资源的模板)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
3.新建controller
@Controller
public class IndexConntroller {
@RequestMapping(value = "/index")
public String index(Model model){
model.addAttribute("name","测试传值");
return "index";
}
@RequestMapping(value = "/forms")
public String forms(Model model){
return "forms";
}
@RequestMapping(value = "/charts")
public String charts(Model model){
return "charts";
}
@RequestMapping(value = "/tables")
public String tables(Model model){
return "tables";
}
@RequestMapping(value = "/login")
public String login(Model model){
return "login";
}
@RequestMapping(value = "/register")
public String register(Model model){
return "register";
}
}
5.项目目录结构如下
目录中html和static资源文件为网上模板文件。
测试传值是否正常
<div class="sidenav-header-inner text-center"><img src="img/avatar-7.jpg" alt="person" class="img-fluid rounded-circle">
<h2 class="h5" th:text="${name}"></h2><span>开发管理者</span>
</div>
index.html使用表达式获取
静态资源获取
<link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
<!-- Custom stylesheet - for your changes-->
<link rel="stylesheet" href="css/custom.css">
6.启动项目访问测试
7.访问http://127.0.0.1:8080/index
值获取,与静态文件都可以了。