22、springboot集成freemarker

简介: springboot 中自带的页面渲染工具为thymeleaf ,freemarker这种模板引擎用的也比较多。

springboot 中自带的页面渲染工具为thymeleaf freemarker种模板引擎用的也比较多。


、在springspringMVC代表着view层组件


、为什么使用freemarker:简单容易学、逻辑分明


freemarker优点:不依赖servlet、网络或者web环境


微信图片_20220501212721.png


1、 新建maven项目,对应的pom.xml文件如下


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud</groupId>
<artifactId>sc-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-freemarker</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>


2、 新建配置文件application.yml


server:
  port: 8081
spring:
  application:
    name: sc-freemarker
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset: UTF-8
    content-type: text/html
    expose-request-attributes: false
    expose-session-attributes: false
    suffix: .ftl
    templateEncoding: UTF-8
    templateLoaderPath: classpath:/templates/
expose-spring-macro-helpers: false


备注:freemarker有哪些配置项可以参考类

org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties

 

3、 新建一个controller


package sc.freemarker.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import sc.freemarker.model.User;
@Controller
public class FreemarkerController {
@RequestMapping("/getUser")
public String getUser(Integer id, Model model){
User u = new User();
u.setId(1);
u.setAge(13);
u.setSex(1);
u.setUserName("黄金");
User l = new User();
l.setId(1);
l.setAge(23);
l.setSex(0);
l.setUserName("白银");
List<User> friends = new ArrayList<User>();
friends.add(l);
u.setFriends(friends);
model.addAttribute("user",u);
return "getUser";
}
}


备注:注意最外层的控制层注解只能用@Controller

 

4、 新建模板文件getUser.ftl


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<center>
<p>welcome ${user.userName} to freemarker!</p>
<p>
           年龄: ${user.age}</br>
性别:
<#if user.sex==0> 女 <#elseif user.sex==1> 男 <#else> 保密
</#if>
</p>
<h4>我的好友:</h4>
<#list user.friends as f> 姓名:${f.userName} , 年龄${f.age} <br>
</#list>
</center>
</body>
</html>


5、 其他项目文件如下图


微信图片_20220501212731.png


6、 运行FreemarkerApplication.java类,启动项目;并检查启动信息,确认freemarker配置是否生效


微信图片_20220501212735.png


7、 访问页面http://127.0.0.1:8081/getUser


微信图片_20220501212738.png


相关文章
|
4天前
|
消息中间件 Java Kafka
springboot集成kafka
springboot集成kafka
11 2
|
4天前
|
网络协议 前端开发 JavaScript
springboot-集成WebSockets广播消息
springboot-集成WebSockets广播消息
|
11天前
|
消息中间件 Java Kafka
集成Kafka到Spring Boot项目中的步骤和配置
集成Kafka到Spring Boot项目中的步骤和配置
43 7
|
11天前
|
druid Java 关系型数据库
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
77 5
|
11天前
|
Java 数据库连接 mybatis
在Spring Boot应用中集成MyBatis与MyBatis-Plus
在Spring Boot应用中集成MyBatis与MyBatis-Plus
46 5
|
11天前
|
前端开发 JavaScript 安全
集成WebSocket在Spring Boot中可以用于实现实时的双向通信
集成WebSocket在Spring Boot中可以用于实现实时的双向通信
28 4
|
11天前
|
安全 Java Maven
在 Spring Boot 中实现邮件发送功能可以通过集成 Spring Boot 提供的邮件发送支持来完成
在 Spring Boot 中实现邮件发送功能可以通过集成 Spring Boot 提供的邮件发送支持来完成
20 2
|
11天前
|
XML 搜索推荐 Java
Elasticsearch集成到Spring Boot项目
将Elasticsearch集成到Spring Boot项目中,可以方便地实现数据的搜索、分析等功能。
35 2
|
11天前
|
监控 前端开发 Java
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
|
11天前
|
Java API Spring
集成EasyPoi(一个基于POI的Excel导入导出工具)到Spring Boot项目中
集成EasyPoi(一个基于POI的Excel导入导出工具)到Spring Boot项目中
49 1