Thymeleaf从入门到出家

目录

1、初体验

2、怎么用

1.导入依赖 

引入命名空间 

2、已经自动注入,可以直接使用

 3、表达式语法

 4、怎么用

3、常用操作

设置属性值-th:attr

thymeleaf内联写法

其他几类基本运算符的用法

1、foreach遍历list,页面展示

2、If/Unless判定条件是否展示

3、Switch判断展示的object

4、内置对象 

4、防止表单重复提交

5、抽取公共页面

 1、创建引用的片段

        1、使用th::fragment标签声明,如上所示

        2、使用css选择器

 2、th::insert,th::replace,th::include的区别


 

1、初体验

thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。类似 JSP,Velocity,FreeMaker 等,它也可以轻易的与 Spring MVC 等 Web 框架进行集成作为 Web 应用的模板引擎。与其它模板引擎相比,

Thymeleaf 最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个 Web 应用。

 

2、怎么用

1.导入依赖 

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

引入命名空间 

<html lang="en" xmlns:th="http://www.thymeleaf.org"><!-- 要加这玩意thymeleaf才能用 -->

2、已经自动注入,可以直接使用

具体bean的自动注入,可参阅下面一篇大佬的博客,了解为什么用的bean能直接@Autowired

 SpringBoot 为什么能够自动的注入一些常用的Bean ?详细分析SpringBoot 自动配置的实现_小火箭Coding的博客-CSDN博客有一个问题一直让我好奇,为什么在SpringBoot中有的bean 我们都没有配置却能够自动注入,这个比如说我们使用JdbcTemplate 的时候那个对象明明我们都没有配置但是却能获取到。再比如SpringBoot在结合Redis的时候,我们也没有注入redisTemplate的bean结果却能使用@Autowired 获取到具体的对象值。这一切究竟是怎么实现的???@Autowiredpr...https://blog.csdn.net/qq_29941401/article/details/79605388

 3、表达式语法

 4、怎么用

前端的thymeleaf怎么取数据?
1、springboot中可以使用model,modelandview,session,request都可以进行传值,其中model和modelandview其本质就是request

@Controller
public class ViewTestController {
    @GetMapping("/hello")
    public String hello(Model model){
        //model中的数据会被放在请求域中 request.setAttribute("a",aa)
        model.addAttribute("msg","一定要大力发展工业文化");
        model.addAttribute("link","http://www.baidu.com");
        return "success";
    }
}

2、使用ajax和json传数据

package com.atqh.Servlet.huishouzhanServlet;

import com.atqh.been.MingPian;
import com.atqh.dao.IDeldao;
import com.atqh.dao.impl.IDelmpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetDel extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        IDeldao iDeldao = new IDelmpl();
        PrintWriter writer = resp.getWriter();
        String username = (String) req.getSession().getAttribute("username");
        List<MingPian> del = iDeldao.getdel(username);
//        System.out.println("!!!!!!!!!!!!!!!!!d调用了!!");
        if (del.size() != 0) {
//            req.setAttribute("del", del);
           req.setCharacterEncoding("utf-8");
            Gson gson = new Gson();
            String data = gson.toJson(del);
            System.out.println("我是string!!!!!" + data);
            writer.write(data);

        }
    }
}

3、常用操作

设置属性值-th:attr

  • 设置单个值
<form action="subscribe.html" th:attr="action=@{/subscribe}">
  <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
  </fieldset>
</form>
  • 设置多个值
<img src="../../images/gtvglogo.png"  
     th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />

thymeleaf内联写法

使用        [[${}]]

<p>Hello, [[${session.user.name}]]!</p>

其他几类基本运算符的用法

1、foreach遍历list,页面展示

  <tr th:each="prod : ${prods}">

      <td th:text="${prod.name}">Onions</td>

      <td th:text="${prod.price}">2.41</td>

      <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>

    </tr>

2、If/Unless判定条件是否展示

Thymeleaf 中使用th:ifth:unless属性进行条件判断,下面的例子中,<a>标签只有在th:if中条件成立时才显示,unless与其相反:

<a th:href="@{/login}" th:if=${session.user==null}>Login</a>

3、Switch判断展示的object

<div th:switch="${user.role}">
      <p th:case="'admin'">User is an administrator</p>
      <p th:case="#{roles.manager}">User is a manager</p>
      <p th:case="*">User is some other thing</p>
</div>

4、内置对象 

Thymeleaf中的内置对象有:

#dates:日期格式化内置对象,具体方法可以参照java.util.Date

#calendars:类似于#dates,但是是java.util.Calendar类的方法;

#numbers: 数字格式化;

#strings:字符串格式化,具体方法可以参照java.lang.String,如startsWithcontains等;

#objects:参照java.lang.Object

#bools:判断boolean类型的工具;

#arrays:数组操作的工具;

#lists:列表操作的工具,参照java.util.List

#sets:Set操作工具,参照java.util.Set

#maps:Map操作工具,参照java.util.Map

#aggregates:操作数组或集合的工具;

#messages:操作消息的工具。

thymeleaf+bootstrap,onclick传参

4、防止表单重复提交

参考另一篇博客

springboot2部分细节(持续更新)_阿狗哲哲的博客-CSDN博客1.防止表单重复提交数据:重定向网址2.抽取公共页面时的几种方式抽取后结果如下所示https://blog.csdn.net/qq_52438590/article/details/120981683?spm=1001.2014.3001.5501 

5、抽取公共页面

 1、创建引用的片段

        1、使用th::fragment标签声明,如上所示

        2、使用css选择器

 2、th::insert,th::replace,th::include的区别

 

 

官方文档永远是最好的学习工具

Tutorial: Using Thymeleaficon-default.png?t=M276https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#using-thtext-and-externalizing-text 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值