广告位联系
返回顶部
分享到

SpringBoot中web模版数据渲染展示的介绍

java 来源:互联网 作者:秩名 发布时间:2022-01-26 12:54:07 人浏览
摘要

模板引擎 SpringBoot是通过模版引擎进行页面结果渲染的,官方提供预设配置的模版引擎主要有 Thymeleaf FreeMarker Velocity Groovy Mustache 我们在这里演示使用 Thymeleaf 和 FreeMarker 模板引擎。 Th

模板引擎

SpringBoot是通过模版引擎进行页面结果渲染的,官方提供预设配置的模版引擎主要有

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

我们在这里演示使用ThymeleafFreeMarker模板引擎。

Thymeleaf

Thymeleaf是适用于 Web 和独立环境的现代服务器端 Java 模板引擎。

Thymeleaf 的主要目标是为你的开发工作流程带来优雅的自然模板——HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而加强开发团队的协作。

凭借 Spring Framework 的模块、与你最喜欢的工具的大量集成以及插入你自己的功能的能力,Thymeleaf 是现代 HTML5 JVM Web 开发的理想选择——尽管它还有更多功能。

新建一个模块

选择我们需要的组建

  • Developer Tools中的Spring Boot DevTools
  • Web中的Spring Web
  • Template Engines 中的Thymeleaf

也可以直接在pom.xml中引入thymeleaf依赖

1

2

3

4

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

创建模板页面

src/main/resources/templates/index.html

1

2

3

4

5

6

7

8

9

10

11

12

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>thymeleaf - 入门小站</title>

</head>

<body>

 

<p th:text="'名字:'+${name}"></p>

<p th:text="'网址:'+${url}"></p>

</body>

</html>

创建controller

com.rumenz.lession4.controller.ThymeleafRumenController

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

package com.rumenz.lession4.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

/**

 * @className: ThymeleafRumenController

 * @description: TODO 类描述

 * @author: 入门小站 rumenz.com

 * @date: 2021/11/1

 **/

@Controller

@RequestMapping("/")

public class ThymeleafRumenController {

    @RequestMapping(value = "/index",method= RequestMethod.GET)

    public String index(ModelMap m){

        //数据也可以从数据库查询出来返回

        m.addAttribute("name", "入门小站");

        m.addAttribute("url", "https://rumenz.com");

        //返回是一个页码:src/main/resources/templates/thymeleaf.html

        return "thymeleaf";

    }

}

启动项目

 

浏览器验证

浏览器访问http://127.0.0.1:8080/index

FreeMarker

FreeMarker是一款模板引擎,即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。

引入依赖

1

2

3

4

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

创建模板

src/main/resources/templates/freemarker.html

1

2

3

4

5

6

7

8

9

10

11

12

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>freemarker - 入门小站</title>

</head>

<body>

FreeMarker案例

<p>名字:${name}</p>

<p>网址:${url}</p>

</body>

</html>

配置文件

src/main/resources/application.properties,指定模板文件的后缀。

1

spring.freemarker.suffix=.html

编写controller

com.rumenz.lession4.controller.FreeMarkerRumenController

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package com.rumenz.lession4.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;

/**

 * @className: FreeMarkerRumenController

 * @description: TODO 类描述

 * @author: 入门小站 rumenz.com

 * @date: 2021/11/1

 **/

@Controller

@RequestMapping("/")

public class FreeMarkerRumenController {

    @RequestMapping("/index2")

    public String index2(ModelMap m){

        //数据也可以从数据库查询出来返回

        m.addAttribute("name", "入门小站");

        m.addAttribute("url", "https://rumenz.com");

        //返回是一个页码:src/main/resources/templates/freemarker.html

        return "freemarker";

    }

}

浏览器验证

浏览器访问http://127.0.0.1:8080/index2

本小结源码地址:

GitHub:https://github.com/mifunc/springboot/tree/main/lession4

Gitee:https://gitee.com/rumenz/springboot/tree/master/lession4

https://rumenz.com/rumenbiji/springboot-tpl.html


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.51cto.com/u_12214037/4966364
相关文章
  • SpringBoot自定义错误处理逻辑介绍

    SpringBoot自定义错误处理逻辑介绍
    1. 自定义错误页面 将自定义错误页面放在 templates 的 error 文件夹下,SpringBoot 精确匹配错误信息,使用 4xx.html 或者 5xx.html 页面可以打印错误
  • Java实现手写一个线程池的代码

    Java实现手写一个线程池的代码
    线程池技术想必大家都不陌生把,相信在平时的工作中没有少用,而且这也是面试频率非常高的一个知识点,那么大家知道它的实现原理和
  • Java实现断点续传功能的代码

    Java实现断点续传功能的代码
    题目实现:网络资源的断点续传功能。 二、解题思路 获取要下载的资源网址 显示网络资源的大小 上次读取到的字节位置以及未读取的字节
  • 你可知HashMap为什么是线程不安全的
    HashMap 的线程不安全 HashMap 的线程不安全主要体现在下面两个方面 在 jdk 1.7 中,当并发执行扩容操作时会造成环形链和数据丢失的情况 在
  • ArrayList的动态扩容机制的介绍

    ArrayList的动态扩容机制的介绍
    对于 ArrayList 的动态扩容机制想必大家都听说过,之前的文章中也谈到过,不过由于时间久远,早已忘却。 所以利用这篇文章做做笔记,加
  • JVM基础之字节码的增强技术介绍

    JVM基础之字节码的增强技术介绍
    字节码增强技术 在上文中,着重介绍了字节码的结构,这为我们了解字节码增强技术的实现打下了基础。字节码增强技术就是一类对现有字
  • Java中的字节码增强技术

    Java中的字节码增强技术
    1.字节码增强技术 字节码增强技术就是一类对现有字节码进行修改或者动态生成全新字节码文件的技术。 参考地址 2.常见技术 技术分类 类
  • Redis BloomFilter布隆过滤器原理与实现

    Redis BloomFilter布隆过滤器原理与实现
    Bloom Filter 概念 布隆过滤器(英语:Bloom Filter)是1970年由一个叫布隆的小伙子提出的。它实际上是一个很长的二进制向量和一系列随机映射
  • Java C++算法题解leetcode801使序列递增的最小交换次

    Java C++算法题解leetcode801使序列递增的最小交换次
    题目要求 思路:状态机DP 实现一:状态机 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public int minSwap(int[] nums1, int[] nums2) { int n
  • Mybatis结果集映射与生命周期介绍

    Mybatis结果集映射与生命周期介绍
    一、ResultMap结果集映射 1、设计思想 对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了 2、resultMap的应用场
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计