SpringBoot学习六:渲染Web页面

SpringBoot很少使用jsp作为Web页面,框架内部支持模板引擎。开发中,模板引擎作为Web开发页面

SpringBoot提供了默认配置的模板引擎主要有以下几种:

·        Thymeleaf

·        FreeMarker

·        Velocity

·        Groovy

·        Mustache

默认配置为src/main/resources下的templates下


扫描二维码关注公众号,回复: 868719 查看本文章

下面以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

页面展示

美丽的天使...

猜你喜欢

转载自blog.csdn.net/cuipeng1019/article/details/80302176
今日推荐