SpringBoot很少使用jsp作为Web页面,框架内部支持模板引擎。开发中,模板引擎作为Web开发页面
SpringBoot提供了默认配置的模板引擎主要有以下几种:
· Thymeleaf
· FreeMarker
· Velocity
· Groovy
· Mustache
默认配置为src/main/resources下的templates下
下面以FreeMarker为例,演示渲染Web页面
1、添加pom.xml依赖
<!-- 引入freeMarker的依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、新建IndexController类:
@RequestMapping("/index")
publicString index(ModelMap map) {
map.put("name","美丽的天使...");
return"index";
}3、在resources下新建index.ftl
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
${name}
</body>
</html>
4、运行项目
访问localhost:8080/index
页面展示
美丽的天使...

本文介绍如何在SpringBoot项目中使用FreeMarker模板引擎渲染Web页面,包括添加依赖、创建控制器和视图文件。

被折叠的 条评论
为什么被折叠?



