前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第四节:SpringBoot中web模版数据渲染展示

第四节:SpringBoot中web模版数据渲染展示

作者头像
入门笔记
发布2022-06-02 18:42:56
3380
发布2022-06-02 18:42:56
举报
文章被收录于专栏:入门小站入门小站

在第一节我们演示通过接口返回数据,数据没有渲染展示在页面上 。在这里我们演示一下从后台返回数据渲染到前端页面的项目案例.

模板引擎

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

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

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

Thymeleaf

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

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

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

新建一个模块

image-20211101134319666

选择我们需要的组建

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

image-20211101134417582

image-20211101134453469

image-20211101134528353

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

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
创建com.rumenz.lession4.controller

image-20211101141735424

创建模板页面

src/main/resources/templates/index.html

代码语言:javascript
复制
<!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

代码语言:javascript
复制
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";
    }
}
启动项目

image-20211101145319108

浏览器验证

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

image-20211101145433041

FreeMarker

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

引入依赖
代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
创建模板

src/main/resources/templates/freemarker.html

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>freemarker - 入门小站</title>
</head>
<body>
<h1>FreeMarker案例</h1>
<p>名字:${name}</p>
<p>网址:${url}</p>
</body>
</html>
配置文件

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

代码语言:javascript
复制
spring.freemarker.suffix=.html
编写controller

com.rumenz.lession4.controller.FreeMarkerRumenController

代码语言:javascript
复制
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";

    }
}
启动项目

image-20211101154819152

浏览器验证

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

image-20211101155125936

系列文章

第一节:创建SpringBoot项目并运行HelloWorld

第二节:SpingBoot单元测试

第三节:SpringBoot中web项目推荐目录结构

本小结源码地址:

  • 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

介绍

  • 我的博客 https://rumenz.com/
  • 我的工具箱 https://tooltt.com/
  • 微信公众号:【入门小站】
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-01-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 入门小站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 模板引擎
  • Thymeleaf
    • 新建一个模块
    • 选择我们需要的组建
      • 创建com.rumenz.lession4.controller包
        • 创建模板页面
          • 创建controller
            • 启动项目
              • 浏览器验证
              • FreeMarker
                • 引入依赖
                  • 创建模板
                    • 配置文件
                      • 编写controller
                        • 启动项目
                          • 浏览器验证
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档