SpringBoot模板

Thymeleaf模板

关于Thymeleaf的优点,只说一条:它就是html页面

导入pom依赖

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

Spring Boot官方文档建议在开发时将缓存关闭

为了代码有层次 application.properties 的后缀改成了application.yml
在这里插入图片描述
那就在 application.yml 文件中加入下面这行

spring.thymeleaf.cache=false

在这里插入图片描述
正式环境还是要将缓存开启的

后台相应代码

User.java

package com.xwt.springboot1.entity;

import lombok.Data;

@Data
public class User {
    private Integer uid;
    private String uname;
    private Integer pwd;

    public User(){

    }

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

UserController.java

package com.xwt.springboot1.controller;

import com.xwt.springboot1.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 介绍thymeleaf模块引擎的应用
 */
@Controller
@RequestMapping("/thymeleaf")
public class UserController {

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView m=new ModelAndView();
        m.addObject("title","学生列表");
        List<User> list=new ArrayList<>();
        list.add(new User(1,"小肖",429));
        list.add(new User(2,"小李",978));
        list.add(new User(3,"小王",865));
        m.addObject("users",list);
        // 为什么需要讲解 html 转义
        m.addObject("msg","<span style='color:red'>优秀学生是小王</span>");
        m.setViewName("list");
        return  m;
    }

}

前台HTML页面

在这里插入图片描述

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf知识点</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" th:width="600px">
    <caption th:text="${title}"></caption>
        <tr>
            <th>ID</th>
            <th>名字</th>
            <th>密码</th>
        </tr>
        <tr th:each="u : ${users}">
            <td th:text="${u.uid}"></td>
            <td th:text="${u.uname}"></td>
            <td th:text="${u.pwd}"></td>
        </tr>
</table>
<p th:utext="${msg}"></p>
</body>
</html>

运行如下:
在这里插入图片描述

Freemarker模板

学习网站: http://freemarker.foofun.cn/

导入pom依赖

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

   <!--可以不加,但是做项目的时候可能会用-->
     <resources>
          <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
          <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--freemarker模板也读取需要注释标红地方-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <!--<include>*.properties</include>-->
                    <!--<include>*.xml</include>-->
                    <!--<include>*.yml</include>-->
                </includes>
            </resource>
        </resources>

右键new一开始是没有freemarker这个类

有两种方法
一是创建一个html文件直接改后缀名为ftl
二如下图
在这里插入图片描述
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/**

在这里插入图片描述
list.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>讲解freemarker知识</title>
</head>
<body>

<h2>取值</h2>
<h3>提供默认值</h3>
welcome 【${name!'未知'}】 to freemarker!

<h3>exists用在逻辑判断</h3>
<#if name?exists>
    ${name}
</#if>

<h2>条件</h2>
<#if sex=='girl'><#elseif sex=='boy'><#else>
		保密
</#if>

<h2>循环</h2>
<table border="1px" width="600px">
    <thead>
    <tr>
        <td>ID</td>
        <td>角色名</td>
        <td>描述</td>
    </tr>
    </thead>
    <tbody>
    <#list roles as role>
    <tr>
        <td>${role.rid}</td>
        <td>${role.rname}</td>
        <td>${role.rdemo}</td>
    </tr>
    </#list>
    </tbody>
</table>

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

<h2>局部变量(assign)/全局变量(global)</h2>
 <#assign ctx1>
     ${springMacroRequestContext.contextPath}
 </#assign>

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

${ctx1}${ctx2}

</body>
</html>

foot.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>调用界面</title>
</head>
<body>
今天下雨了 记得保暖打伞
</body>
</html>

RoleController.java

package com.xwt.springboot1.controller;

import com.xwt.springboot1.entity.Role;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 介绍freemarker模块引擎的应用
 */
@Controller
@RequestMapping("/freemarker")
public class RoleController {

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView m=new ModelAndView();
        m.setViewName("list");
        m.addObject("name","学生列表");
        m.addObject("sex","boy");

        List<Role> roles=new ArrayList<>();
        roles.add(new Role(1,"网管","网吧总管"));
        roles.add(new Role(2,"包租婆","管水管电管房租"));
        roles.add(new Role(3,"总管","公司老总"));
        m.addObject("roles",roles);

        return  m;
    }

}

运行如下:
在这里插入图片描述
注意点:

1、application.yml中可以配置模板存放位置的根路径、以及静态资源文件存放位置的根路径
2、${springMacroRequestContext.contextPath}:SpringBoot中获取项目名
3、不推荐使用全局变量。即便它们属于不同的命名空间, 全局变量也被所有模板共享,因为它们是被 import进来的。

thymeleaf中替代jsp:include的写法

两种情况
一种是应用一段html代码

在这里插入图片描述

在需要应用的地方添加

<!-- thymeleaf 包含公共区域的方案-->
<div th:replace="common :: h2"></div>

一种是包含整个html页面

<div th:include="freemarker/common/head"></div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统根据B/S,即所谓的电脑浏览器/网络服务器方式,运用Java技术性,挑选MySQL作为后台系统。系统主要包含对客服聊天管理、字典表管理、公告信息管理、金融工具管理、金融工具收藏管理、金融工具银行卡管理、借款管理、理财产品管理、理财产品收藏管理、理财产品银行卡管理、理财银行卡信息管理、银行卡管理、存款管理、银行卡记录管理、取款管理、转账管理、用户管理、员工管理等功能模块。 文中重点介绍了银行管理的专业技术发展背景和发展状况,随后遵照软件传统式研发流程,最先挑选适用思维和语言软件开发平台,依据需求分析报告模块和设计数据库结构,再根据系统功能模块的设计制作系统功能模块图、流程表和E-R图。随后设计架构以及编写代码,并实现系统能模块。最终基本完成系统检测和功能测试。结果显示,该系统能够实现所需要的作用,工作状态没有明显缺陷。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。进入银行卡列表,管理员可以进行查看列表、模糊搜索以及相关维护等操作。用户进入系统可以查看公告和模糊搜索公告信息、也可以进行公告维护操作。理财产品管理页面,管理员可以进行查看列表、模糊搜索以及相关维护等操作。产品类型管理页面,此页面提供给管理员的功能有:新增产品类型,修改产品类型,删除产品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值