Java基础之Thymeleaf的简单使用

Java代码

package com.zzx.controller;

import com.zzx.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;

/**
 * @date: 2021/04/25/ 10:07
 * @author: ZhengZiXuan
 * @description: 由于Spring Boot 不推荐我们使用.jsp,所以我们就使用html配合thymeleaf来进行数据的传输
 * @title: Thymeleaf简单使用
 */
@Controller
@RequestMapping("/thyme")
public class ThymeleafController {

    @RequestMapping("data")
    public String ShowData(Model model){
        model.addAttribute("text","<a href='#'>点击1</a>");
        model.addAttribute("utext","<a href='#'>点击1</a>");
        model.addAttribute("value","input值");
        model.addAttribute("user",new User(1,"张三"));
        model.addAttribute("num",100);
        model.addAttribute("flag",true);
        model.addAttribute("list", Arrays.asList("Java","WEB","UI"));
        return "data";
    }
}

前端代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--此处需要配置thymeleaf,可以不配置,但是会爆红,不会影响最终效果-->
<head>
    <meta charset="UTF-8">
    <title>thymeleaf的简单使用</title>
</head>
<body>

<!--取出后的值,填充到p标签中间,将字符串的标签解析字符串-->
<p th:text="${text}"></p><br/><hr>

<!--取出后的值,填充到p标签中间,utext会将字符串的标签解析为html标签-->
<p th:text="${utext}"></p><br/>

<!--th:value,相当于是给原value属性赋值-->
<input th:value="${value}"/><br/><hr/>

<!--thymeleaf支持属性导航, 对象.属性-->
id:<p th:text="${user.id}"></p><br>
name:<p th:text="${user.name}"></p><br>
<br><hr/>
<p th:text="${num}"></p>
<br/><hr/>

<!--th:if 判断,如果判断成功,该标签内的内容会展示,否则不展示-->
<p th:if="${flag}== true">
    看这里看这里
</p>
<hr>
<ol>
    <!--th:each 变量
        1. th:each 属性在哪个标签,哪个标签循环出现
        2. th:each= "遍历得到结果变量 :${key}"
        3. 在当前标签,或者内部标签就可以使用"遍历得到结果变量"
    -->
    <li th:text="${str}" th:each="str : ${list}"></li>
</ol>
</body>
</html>

最终效果

到此这篇关于Java基础之Thymeleaf的简单使用的文章就介绍到这了,更多相关Java Thymeleaf的使用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • springBoot加入thymeleaf模板的方式

    1.新建springBoot项目 在前面有两种方式 2.加入thymeleaf模板引擎 SpringBoot推荐使用thymeleaf模板引擎 语法简单,功能更强大 要想引入thymeleaf,只需要在pom,xml文件中加入如下依赖就可以了 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifa

  • springboot+thymeleaf 文件上传功能的实现代码

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

  • Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码

    项目介绍 基于Layui的后台管理系统模板,扩展Layui原生UI样式,整合第三方开源组件,提供便捷快速的开发方式,延续LayuiAdmin的设计风格,持续完善的样式与组件的维护,基于异步Ajax的菜单构建,相对完善的多标签页,单标签页的共存,为使用者提供相对完善的开发方案,只为成为更好的轮子,项目不定时更新,建议 Star watch 一份 如果你需要无组件整合 与 示例页面 的基础框架,请前往 Pear Admin Layui 基础版本 项目结构 Pear Admin Layui │ ├─a

  • Spring Boot thymeleaf模板引擎的使用详解

    在早期开发的时候,我们完成的都是静态页面也就是html页面,随着时间轴的发展,慢慢的引入了jsp页面,当在后端服务查询到数据之后可以转发到jsp页面,可以轻松的使用jsp页面来实现数据的显示及交互,jsp有非常强大的功能,但是,在使用springboot的时候,整个项目是以jar包的方式运行而不是war包,而且还嵌入了tomcat容器,因此,在默认情况下是不支持jsp页面的.如果直接以纯静态页面的方式会给我们的开发带来很大的麻烦,springboot推荐使用模板引擎. 模板引擎有很多种,jsp,

  • SpringBoot Security安装配置及Thymeleaf整合

    功能:解决web站点的登录,权限验证,授权等功能 优点:在不影响站点业务代码,可以权限的授权与验证横切到业务中 1.要添加的依赖 <!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!

  • Thymeleaf 3.0 自定义标签方言属性的实例讲解

    此篇文章内容仅限于 描述 thy3.0 自定义标签的说明,所以你在看之前,请先会使用它. 直奔主题,以下代码是如何引用 第三方标签的.说明: shrioDialect 是Shiro 官方为thy开发的自定义标签工具.和jsp的一样 RiskDialect 是我写的自定义标签 <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> <property

  • SpringBoot使用Thymeleaf模板引擎访问静态html的过程

    最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的傻瓜教程..... 一:创建spring boot的web项目,过程略: 二:依赖如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <

  • 极为简易的Qno SmartLink IPSec VPN设定

    VPN--Virtual Private Network,是针对在互联网技术及应用不断发展中,企业及特定用户对信息交流.传递.交换的安全性.可靠性及成本上的需求,在公共互联网的基础上,通过遂道和加密技术,构建虚拟专用网的解决方案. VPN构建中的一项重要的核心任务是遂道技术,而IPSec--因特网安全协议(即遂道协议)是VPN技术中较为成熟的遂道协议.相对于PPTP.SSL这两种协议来说, IPSec虽然公认是最值得信赖和安全的VPN协议,但同时也是出名的难以设置,安装配置和管理都比较复杂,尤其

  • SpringBoot 利用thymeleaf自定义错误页面

    导入thymeleaf <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 自定义异常类 建立监听异常类 MyException.class package com.example.demo.domain; public class My

  • SpringBoot使用Thymeleaf自定义标签的实例代码

    此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf!! 之前写过一篇是springMVC与thymeleaf 的自定义标签(属于自定义方言的属性一块,类似thy的th:if和th:text等)文章,如果你想了解,以下是地址: 点击>>Thymeleaf3.0自定义标签属性 这篇例子可以实现你的分页标签实现等功能,不会讲一堆的废话和底层的原理(自行百度),属于快速上手教程,请认真看以下内容! PS: 请允许

  • Spring Boot和Thymeleaf整合结合JPA实现分页效果(实例代码)

    在项目里,我需要做一个Spring Boot结合Thymeleaf前端模版,结合JPA实现分页的演示效果.做的时候发现有些问题,也查了现有网上的不少文档,发现能全栈实现的不多,所以这里我就把我的做法,全部代码和步骤贴出来供大家参考. 1 创建项目,用pom.xml引入依赖 这里将创建名为ThymeleafWithDB的Maven,在pom.xml里引入如下的依赖包. <dependencies> <dependency> <groupId>org.springframe

随机推荐