SpringBoot之模板

本文介绍了SpringBoot中的模板引擎,重点讲述了Thymeleaf和Freemarker的配置与使用。在Thymeleaf部分,提到了其作为HTML页面的优点,并提供了相关依赖、配置及代码示例。对于Freemarker,文章列出了配置步骤、依赖及模板文件的示例。此外,还指导了如何在IDE中配置.ftl文件模板。
摘要由CSDN通过智能技术生成

重点:
0、回顾;
1、springboot之thymeleaf模板
2、springboot之freemarker模板

重点解析:
0、回顾加补充
0.1、springboot入门中新建spring boot项目时三个坑:
(0.1.1)、必须联网,不然无法填写项目名,无法完成项目创建。
(0.1.2)、创建springboot项目名的时候是需要小写的,不然无法进行下一步。
(0.1.3)、在进行项目创建最后一步的时候不要掉以轻心。因为第一遍可能必须创建成功。将项目删除之后重新创建一次基本上就没有多大的问题。如果在pom.xml里面还是报错,就在本地仓库找到相关依赖删除,然后重新下载一遍也可以。

0.2、application回顾
0.2.1、内置属性,直接在applocation里面添加
server.port
server.servlet.context-path:
0.2.2、自定义属性在application声明,但是必须是 key=value
mysql.url
mysql.driver
在java里面必须要通过@value("${key}"),获取里面的值
0.2.3、属性封装类 注意必须要在实体类做添加@component和
@configuationProperties(“prefix=mysql”)

springboot模板

spring boot模块共4个:Thymeleaf、Freemarker、Mustache、Groovy Templates。 前面两个使用率比较高,基本后面两个没什么人使用,就不进行进解了。如果要使用springboot模块请点击Template Engines如下面图中进行选择模块。
在这里插入图片描述

1、springboot之thymeleaf模板
关于Thymeleaf的优点,我只说一条:它就是html页面。下面直接上代码
相关pom依赖

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

Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行

spring.thymeleaf.cache=false

正式环境还是要将缓存开启的

   <html xmlns:th="http://www.thymeleaf.org">

实体类方法:user.java

public class User {
    private Integer  uid;
    private String uname;
    private String upwd;

    public User(){

    }

    public User(Integer uid, String uname, String upwd) {
        this.uid = uid;
        this.uname = uname;
        this.upwd = upwd;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }
}

代码分享:IndexController.java

 @RequestMapping("/user/list")
    public ModelAndView userlist(){
        ModelAndView modelAndView=new ModelAndView();
         modelAndView.setViewName("/user/list");
         modelAndView.addObject("title","用户列表");
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张晨光","6789"));
        list.add(new User(2,"韩伊婷","5675678"));
        modelAndView.addObject("users",list);
        return  modelAndView;
    }

代码分享:list.html

<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}"></title>
</head>
<body>
<!-- text 用于展示-->
<h1 th:text="${title}"></h1>

<!-- each 类似于foreach ,用来循环输出 -->
<table border="1px" width="600px">
    <thead>
    <tr>
        <td>id</td>
        <td>用户名</td>
        <td>密码</td>
    </tr>
    </thead>
    <tbody>
    <tr th:each="user :${users}">
        <td th:text="${user.uid}">id</td>
        <td th:text="${user.uname}">用户名</td>
        <td th:text="${user.upwd}">密码</td>
    </tr>
    </tbody>
</table>

<select>
    <option th:each="user:${users}" th:value="${user.uid}" th:text="${user.uname}" ></option>
</select>

</body>
</html>
运行效果图:

在这里插入图片描述

2、springboot之freemarker模板
导入pom依赖

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

application.yml文件的默认配置

    spring:
      thymeleaf:
        cache: false
      freemarker:
        # 设置模板后缀名
        suffix: .ftl
        # 设置文档类型
        content-type: text/html
        # 设置页面编码格式
        charset: UTF-8
        # 设置页面缓存
        cache: false
        # 设置ftl文件路径,默认是/templates,为演示效果添加role
        template-loader-path: classpath:/templates/role
      mvc:
        static-path-pattern: /static/**

IndexController.java代码分享:

  @RequestMapping("/role/list")
    public ModelAndView rolelist() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("list");
        modelAndView.addObject("name",“嘿嘿”);
        modelAndView.addObject("sex","boy");
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张晨光","6789"));
        list.add(new User(2,"韩伊婷","5675678"));
        modelAndView.addObject("users",list);
        return  modelAndView;
    }
    @RequestMapping("/role/login")
    public String rolelogin() {
        return "login";
    }

common.ftl代码分享:

<#-- assign 设置全局变量-->
<#assign ctx>
    ${springMacroRequestContext.contextPath}
</#assign>
<base href="${ctx}/">
<script type="text/javascript" src="${ctx}/static/js/xx.js"></script>

foot.ftl代码分享:

版本号
<#-- 第一个无法点击进去,因为springboot不允许直接点击,必须进行跳转才可以跳转进去 -->
<a href="login.ftl">登陆1</a>
<a href="${springMacroRequestContext.contextPath}/role/login">登陆2</a>

login.ftl代码分享:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
登陆
</body>
</html>

list.ftl 代码 :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色列表</title>
    <#-- 静态页面的调用-->
    <#include 'common.ftl'>
</head>
<body>
<h1>取值</h1>
<#-- 取值里面必须有值,如果没有或者是null就直接显示500。解决方法 ${值 ! '空'} ,这里的!就代表判断,后面的空是进行替换-->
welcome 【${name ! '空'}】 to page
<h1>非空判断</h1>
<#if name?exists>
    里面有值
</#if>
<h1>条件表达式</h1>
<#if sex=='boy'>
    男
<#elseif sex=='girl'>
女
<#else>
</#if>

<h1>循环</h1>
<table border="1px" width="600px">
    <thead>
    <tr>
        <td>id</td>
        <td>用户名</td>
        <td>密码</td>
    </tr>
    </thead>
    <tbody>
    <#list users as user>
    <tr>
        <td>${user.uid}</td>
        <td>${user.uname}</td>
        <td>${user.upwd}</td>
    </tr>
    </#list>
    </tbody>
</table>

<h1>include</h1>
<#include 'foot.ftl'>


<h1>变量(局部,全局)</h1>
<#assign ctxl>
    ${springMacroRequestContext.contextPath}
</#assign>

<#global ctx2>
    ${springMacroRequestContext.contextPath}
</#global>
${ctxl}.${ctx2}

</body>
</html>

结果效果图:

在这里插入图片描述

ps:如何配置 .ftl的界面

第一步:点击setting,进入设置然后点击 file and code Templates ,注意左边出来的是includes,要点击files。在这里插入图片描述
第二步:将html里面的内容进行复制,然后点击 +,将在html里面复制的内容,粘贴在最大空白里。注意要填写name,就是创建 .ftl的项目名称。e’xtension里面填写项目的后缀名。
在这里插入图片描述
第三步:点击Apply,就可以在新建里面了在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值