为了账号安全,请及时绑定邮箱和手机立即绑定

【SpringBoot2.0系列03】SpringBoot之使用freemark视图模板

标签:
SpringBoot

前言

freemarker介绍;
FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据,   并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。       它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
前面我介绍了如何整合thymeleaf,那么现在我们再来了解一下SpringBoot中如何使用freemark

一、目标

使用freemark视图模板,并且于SpringBoot进行整合。 使用freemark显示用户(user)的信息

二、实现

首先创建一个SpringBoot项目,添加如下依赖

<dependencies>
        <!-- freemark -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

添加完依赖以后,就需要编写对应的ControllerviewUser.java
src/main/java/com/yukong/chapter22目录下新建User

package com.yukong.chapter22;import java.util.Date;/**
 * @Auther: xiongping22369
 * @Date: 2018/8/13 17:53
 * @Description: user类
 */public class User {    /**
     * 用户名
     */
    private String username;    /**
     * 密码
     */
    private String password;    /**
     * 年龄
     */
    private Integer age;    /**
     * 性别 1=男 2=女 其他=保密
     */
    private Integer sex;    public String getUsername() {        return username;
    }    public void setUsername(String username) {        this.username = username;
    }    public String getPassword() {        return password;
    }    public void setPassword(String password) {        this.password = password;
    }    public Integer getAge() {        return age;
    }    public void setAge(Integer age) {        this.age = age;
    }    public Integer getSex() {        return sex;
    }    public void setSex(Integer sex) {        this.sex = sex;
    }

}

编写IndexController 实现将User信息传递给前台ftl页面。

@Controllerpublic class IndexController {    @GetMapping("/aboutMe")    public String index(Model model) throws ParseException {
        User user = new User();
        user.setUsername("yukong");
        user.setPassword("abc123");
        user.setAge(18);
        user.setSex(1);
        model.addAttribute("user", user);        return "index";
    }

}

注意这里使用的是@Controller
resource目录下新建templates文件夹并且在该目录下新建文件index.ftl
记住是ftl freemark文件的后缀名是ftl

index.ftl代码

<!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>freemark</title></head><body>
    <p> 你好 ${user.username}</p>
    <p> 密码 ${user.password}</p>
    <p> 性别:           <#if user.sex==1>
              男           <#elseif user.sex==2>
              女           <#else>
              保密           </#if>
    </p>
    <p> 年龄 ${user.age}</p></body></html>

然后在src/resource/application.yml配置一下thymeleaf相关配置

server:
  port: 8989
spring:
  freemarker:
    request-context-attribute: req  #req访问request
    suffix: .ftl  #后缀名
    content-type: text/html
    enabled: true
    cache: false #缓存配置
    template-loader-path: classpath:/templates/ #模板加载路径 按需配置
    charset: UTF-8 #编码格式
    settings:
      number_format: '0.##'   #数字格式化,无小数点

启动Chapter22Application.java并且访问http://localhost:8989/aboutMe

结果如图


webp

image.png

由上图可知,freemark成功接受到了后台传递的数据。并且渲染到页面显示。

三、总结

此致我们SpringBoot整合freemark就完毕了。



作者:余空啊
链接:https://www.jianshu.com/p/ede0d698b0ae


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
50
获赞与收藏
175

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消