一、Webjars与静态资源
- SpringBoot静态资源
- 静态资源目录与配置信息
- /static -> classpath:/static/
- /public -> classpath:/public/
- /resources -> classpath:/resources/
- /META-INF/resources -> classpath:/META-INF/resources
- 自定义静态资源目录方法
- favicon.ico图标
- 静态资源目录与配置信息
- 使用Webjars管理CSS和JS
- 为什么使用WebJars
- 清晰的管理web依赖
- 通过Maven,Gradel等项目管理工具就可以下载web依赖
- 解决web组件中传递的问题以及版本问题
- 页面依赖的版本自动检测功能
- 为什么使用WebJars
二、整合FreeMarker
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
编写配置文件
spring:
freemarker:
cache: false #缓存配置 开发阶段应该配置为false,因为经常会改动
suffix: .ftl #模板文件后缀名
charset: UTF-8 #文件编码
template-loader-path: classpath:/templates/
编写Controller
- 接口顶层注解使用@Controller,因为接口要返回一个页面,使用@RestController只能返回数据,接口的入参时候Model ```java @Controller @RequestMapping(“/tempalte”) public class TemplateController {
@GetMapping("/freemarker")
public String index(Model model) {
List<Article> articles = Arrays.asList(
new Article(1, "张三", "Java从入门精通"),
new Article(2, "张三丰", "Spring"),
new Article(3, "张三风", "SpringBoot")
);
model.addAttribute("articles", articles);
System.out.println(articles);
// 模板名称
return "freemarker-demo";
}
}
- 页面显示
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Freemarker页面</title>
<link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<table class="table table-primary table-hover">
<tr>
<td>id</td>
<td>作者</td>
<td>标题</td>
</tr>
<#list articles as article>
<tr>
<td>${article.id}</td>
<td>${article.author}</td>
<td>${article.title}</td>
</tr>
</#list>
</table>
</div>
<script src="/webjars/jquery/jquery.min.js"></script>
<script src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
三、整合template
- 添加依赖