Spring Boot整理——Thymeleaf模板(七)

Spring Boot 整合 Thymeleaf 实例 给大家送一个小福利附高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、准备面试跳槽、自身职业规划迷茫的朋友们。《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!给大家送一个小福利[外链图片转存中…(img-sD8d2gnA-1713293503664)]附高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、准备面试跳槽、自身职业规划迷茫的朋友们。 阅读详情

打个广告,帮朋友卖点东西,东西超便宜的哟【衣服鞋子等】,厂家直接出货,绝对低于市场价!!! 一般都比市场价便宜3—7折【都是牌子货】,如果您感兴趣,可以扫描屏幕下方的二维码,感谢关注!!!

微信

一、基本介绍

        Thymeleaf是一个Java库。它是一个XML / XHTML / HTML5模板引擎,能够应用于转换模板文件,以显示您的应用程序产生的数据和文本。它尤其适合于基于XHTML / HTML5的web服务应用程序,同时它可以处理任何XML文件,作为web或独立的应用程序。

        Thymeleaf的主要目的是提供一个优雅和格式良好的方式创建模板。为了实现这一目标,它把预定义的逻辑放在XML的标记和属性上,而不是显式放在XML标记的内容上。依靠智能缓存去解析文件,致使其执行期间的I / O操作达到了最少数量,因此其处理的模板的能力实非常快速的。

        Thymeleaf的标准方言为:

<span th:text="..."><!--需要引入命名空间-->
<span data-th-text="..."><!--可以不引入命名空间-->

二、语法介绍

1、表达式

        Thymeleaf支持的表达式有ognl表达式和springEL表达式。

变量表达式:${...}

<span th:text="${book.author.name}">

消息表达式:#{...} 

        将key映射成对应的value,也称为文本外部化、国际化或i18n。

<table>
    ...
    <th th:text="#{header.address.city}">...</th>
    <th th:text="#{header.address.country}">...</th>
    ...
</table>

选择表达式:*{...}

        它们是在当前选择的对象而不是整个上下文变量映射上执行,效率上会快很多。

<div th:object="${book}">
    ...
    <span th:text="*{title}">...</span><!--取book的title的属性-->
    ...
</div>

链接表达式:@{...}

        链接表达式可以是相对的,在这种情况下,应用程序上下文将不会作为URL的前缀:

<a th:href="@{../documents/report}">...</a>

也可以是服务器相对(同样,没有应用程序上下文前缀),

<a th:href="@{~/contents/main}">...</a>

和协议相对(就像绝对URL,但是浏览器将使用在显示的页面中使用的相同的HTTP或HTTPS协议),

<a th:href="@{//static.mycompany.com/res/initial}">...</a>

当然,Link表达式可以是绝对的:

<a th:href="@{http://www.mycompany.com/main}">...</a>  

分段表达式:th:insert(整个片段插入div)或th:replace(把自己替换掉)

<!--页面一-->
<body>
    <div th:fragment="copy" id="copyId"><!--自定义片段,如页面头等-->
        ...
    </div>
<body>
​
<!--页面二-->
<div th:insert="~{footer :: copy}"></div><!--表示将copy的内容插入到这里来,重用-->
<div th:insert="~{footer :: #copyId}"></div><!--也可以不用fragment,直接根据id-->

2、字面量

文本

<span th:text="${book.author.name}">

布尔

<div th:if="${user.isAdmin()} == false">...

无操作

<span th:text="${user.name}? : _">no user name</span><!--即如果user.name不存在就显示“no user name”-->

3、设置属性值

设置任意属性值 th:attr:

<form action="subscribe.html" th:attr="action=@{/subscribe}">
    <input type="submit" th:attr="value=#{subscribe.submit}"/>
</form>

设置指定属性:

<form action="subscribe.html" th:action="@{/subscribe}">
    <input type="submit" th:value="#{subscribe.submit}"/>
</form>

4、迭代器

基本的迭代 th:each :

<li th:each="book : ${books}" th:text = "${book.title}">En las del Sar</li>

状态变量:index、count、size、current、even(奇数)/odd(偶数)、first、last

下面是一个例子

  <table>
    <tr>
      <th>NAME</th>
      <th>PRICE</th>
      <th>IN STOCK</th>
    </tr>
    <tr th:each="prod,iterStat: ${prods}" th:class="${iterStat.odd}? 'odd'">
      <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>
  </table>

5、条件语句

if/unless

        Thymeleaf中使用th:if和th:unless属性进行条件判断,下面的例子中,标签只有在th:if中条件成立时才显示: 

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

th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。

Switch

Thymeleaf同样支持多路选择Switch结构:

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

默认属性default可以用*表示: 

<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>

6、注释

解析器级别注释:静态时候会被显示,当模板执行(Thymeleaf解析)时,这些会被注释

<!--/*-->
    <div>
        you can see me !
    </div>
<!--*/-->

原型注释:当模板静态打开时不显示,当模板执行(Thymeleaf解析)时,这些被注释内容将会显示,和上面相反:

<span>hello</span>
<!--/*/
    <div th:text="${...}">
        ...
    </div>
/*/-->
<span>goodbye</span>

7、内联

        [[...]]和[(...)]分别对应于th:text和th:utext

<p>the message is [(${msg})]</p>
===》
<p>the message is <b>great</b></p>
​
<p>the message is [[${msg}]]</p>
===》
<p>the message is &lt; b &gt; great &lt; /b &gt;</p>

8、内置对象

1.基本对象

        ctx,对象继承org.thymeleaf.context.IContext或者org.thymeleaf.context.IWebContext,取决于当前环境是不是web环境。如果程序集成了spring,那么将会是org.thymeleaf.spring[3|4].context.SpringWebContext。

/*
* ======================================================================
* See javadoc API for class org.thymeleaf.context.IContext
* ======================================================================
*/
${#ctx.locale}
${#ctx.variables}
/*
* ======================================================================
* See javadoc API for class org.thymeleaf.context.IWebContext
* ======================================================================
*/
${#ctx.applicationAttributes}
${#ctx.httpServletRequest}
${#ctx.httpServletResponse}
${#ctx.httpSession}
${#ctx.requestAttributes}
${#ctx.requestParameters}
${#ctx.servletContext}
${#ctx.sessionAttributes}

  locale,java.util.Locale对象的访问.  

  vars,org.thymeleaf.context的实例。访问VariablesMap所有上下文中的变量(包含本笃和ctx.variables中的)。

/*
* ======================================================================
* See javadoc API for class org.thymeleaf.context.VariablesMap
* ======================================================================
*/
${#vars.get('foo')}
${#vars.containsKey('foo')}
${#vars.size()}

2.web环境中访问request/session等属性

        当在web环境中使用Thymeleaf,我们可以使用一系列的快捷方式访问请求的参数,会话和应用程序的属性.

param,获取请求的参数.

/*
* ============================================================================
* See javadoc API for class org.thymeleaf.context.WebRequestParamsVariablesMap
* ============================================================================
*/
${param.foo} // Retrieves a String[] with the values of request parameter 'foo'
${param.size()}
${param.isEmpty()}
${param.containsKey('foo')}

session,访问session属性。

/*
* ======================================================================
* See javadoc API for class org.thymeleaf.context.WebSessionVariablesMap
* ======================================================================
*/
${session.foo} // Retrieves the session atttribute 'foo'
${session.size()}
${session.isEmpty()}
${session.containsKey('foo')}

application,获取应用程序/ servlet上下文属性。

/*
* =============================================================================
* See javadoc API for class org.thymeleaf.context.WebServletContextVariablesMap
* =============================================================================
*/
${application.foo} // Retrieves the ServletContext atttribute 'foo'
${application.size()}
${application.isEmpty()}
${application.containsKey('foo')}

3.web环境对象

        httpServletRequest :javax.servlet.http.HttpServletRequest对象实例。

${#httpServletRequest.getAttribute('foo')}
${#httpServletRequest.getParameter('foo')}
${#httpServletRequest.getContextPath()}
${#httpServletRequest.getRequestName()}
...

        httpSession:javax.servlet.http.HttpSession实例。

${#httpSession.getAttribute('foo')}
${#httpSession.id}
${#httpSession.lastAccessedTime}
...

4.spring环境对象

        themes : 提供和“ spring:theme JSP tag.”同样的功能。

${#themes.code('foo')}
​
<!--直接访问spring注册对象-->
<div th:text="${@authService.getUserName()}">...</div>

三、项目实战

1、基本搭建

        我们在这里创建一个包含用户增删改查功能的项目,这里采用Gradle来构建项目,其中build.gradle配置如下:

// buildscript 代码块中脚本优先执行
buildscript {
​
    // ext 用于定义动态属性
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }

    // 自定义  Thymeleaf 和 Thymeleaf Layout Dialect 的版本
    ext['thymeleaf.version'] = '3.0.3.RELEASE'
    ext['thymeleaf-layout-dialect.version'] = '2.2.0'

    // 使用了 Maven 的中央仓库(你也可以指定其他仓库)
    repositories {
        //mavenCentral()
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
    }

    // 依赖关系
    dependencies {
        // classpath 声明说明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
​
// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
​
// 打包的类型为 jar,并指定了生成的打包的文件名称和版本
jar {
    baseName = 'thymeleaf-in-action'
    version = '1.0.0'
}
​
// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8
​
// 默认使用了 Maven 的中央仓库。这里改用自定义的镜像库
repositories {
    //mavenCentral()
    maven {
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
}
​
// 依赖关系
dependencies {
    // 该依赖对于编译发行是必须的
    compile('org.springframework.boot:spring-boot-starter-web')

    // 添加 Thymeleaf 的依赖
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
​
    // 该依赖对于编译测试是必须的,默认包含编译产品依赖和编译时依
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

​然后配置application.properties内容如下:

# THYMELEAF 
spring.thymeleaf.encoding=UTF-8
# 热部署静态文件
spring.thymeleaf.cache=false
# 使用HTML5标准
spring.thymeleaf.mode=HTML5

2、后台逻辑

1.实体

public class User {
    private long id; // 用户的唯一标识
    private String name;
    private int age;
}

2.业务逻辑

/**
 * 用户仓库.
 */
public interface UserRepository {
    /**
     * 新增或者修改用户
     * @param user
     * @return
     */
    User saveOrUpateUser(User user);

    /**
     * 删除用户
     * @param id
     */
    void deleteUser(Long id);

    /**
     * 根据用户id获取用户
     * @param id
     * @return
     */
    User getUserById(Long id);

    /**
     * 获取所有用户的列表
     * @return
     */
    List<User> listUser();
}

其中实现类如下:

@Repository
public class UserRepositoryImpl implements UserRepository {

    private static AtomicLong counter = new AtomicLong();
​
    private final ConcurrentMap<Long, User> userMap = new ConcurrentHashMap<Long, User>();

    public UserRepositoryImpl(){
        User user = new User();
        user.setAge(30);
        user.setName("Way Lau");
        this.saveOrUpateUser(user);
    }

    @Override
    public User saveOrUpateUser(User user) {
        Long id = user.getId();
        if (id <= 0) {
            id = counter.incrementAndGet();
            user.setId(id);
        }
        this.userMap.put(id, user);
        return user;
    }
​
    @Override
    public void deleteUser(Long id) {
        this.userMap.remove(id);
    }
​
    @Override
    public User getUserById(Long id) {
        return this.userMap.get(id);
    }
​
    @Override
    public List<User> listUser() {
        return new ArrayList<User>(this.userMap.values());
    }
​
}

3.控制层

/**
 * 用户控制器.
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired 
    private UserRepository userRepository;
​
    /**
     * 从 用户存储库 获取用户列表
     * @return
     */
    private List<User> getUserlist() {
        return userRepository.listUser();
    }
​
    /**
     * 查询所用用户
     * @return
     */
    @GetMapping
    public ModelAndView list(Model model) {
        model.addAttribute("userList", getUserlist());
        model.addAttribute("title", "用户管理");
        return new ModelAndView("users/list", "userModel", model);
    }

    /**
     * 根据id查询用户
     * @param message
     * @return
     */
    @GetMapping("{id}")
    public ModelAndView view(@PathVariable("id") Long id, Model model) {
        User user = userRepository.getUserById(id);
        model.addAttribute("user", user);
        model.addAttribute("title", "查看用户");
        return new ModelAndView("users/view", "userModel", model);
    }
​
    /**
     * 获取 form 表单页面
     * @param user
     * @return
     */
    @GetMapping("/form")
    public ModelAndView createForm(Model model) {
        model.addAttribute("user", new User());
        model.addAttribute("title", "创建用户");
        return new ModelAndView("users/form", "userModel", model);
    }
​
    /**
     * 新建用户
     * @param user
     * @param result
     * @param redirect
     * @return
     */
    @PostMapping
    public ModelAndView create(User user) {
        user = userRepository.saveOrUpateUser(user);
        return new ModelAndView("redirect:/users");
    }
​
    /**
     * 删除用户
     * @param id
     * @return
     */
    @GetMapping(value = "delete/{id}")
    public ModelAndView delete(@PathVariable("id") Long id, Model model) {
        userRepository.deleteUser(id);

        model.addAttribute("userList", getUserlist());
        model.addAttribute("title", "删除用户");
        return new ModelAndView("users/list", "userModel", model);
    }
​
    /**
     * 修改用户
     * @param user
     * @return
     */
    @GetMapping(value = "modify/{id}")
    public ModelAndView modifyForm(@PathVariable("id") Long id, Model model) {
        User user = userRepository.getUserById(id);

        model.addAttribute("user", user);
        model.addAttribute("title", "修改用户");
        return new ModelAndView("users/form", "userModel", model);
    }
}

3、前台页面

1.公共页脚

header.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf in action</title>
</head>
<body>
<div data-th-fragment="header">
    <h1>Thymeleaf in action</h1>
    <a href="/users">首页</a>
</div>
</body>
</html>

footer.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf in action</title>
</head>
<body>
<div data-th-fragment="footer">
    <a href="https://waylau.com">Welcome to waylau.com</a>
</div>
</body>
</html>

2.功能页面

list.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <title th:text="${userModel.title}">welcome</title>
</head>
<body>
<div th:replace="~{fragments/header :: header}">...</div>
<h3 th:text="${userModel.title}">Welcome to waylau.com</h3>
<div>
    <a href="/users/form.html">创建用户</a>
</div>
<table border="1">
    <thead>
    <tr>
        <td>ID</td>
        <td>Age</td>
        <td>Name</td>
    </tr>
    </thead>
    <tbody>
    <tr th:if="${userModel.userList.size()} eq 0">
        <td colspan="3">没有用户信息!!</td>
    </tr>
    <tr th:each="user : ${userModel.userList}">
        <td th:text="${user.id}">1</td>
        <td th:text="${user.age}">11</td>
        <td><a href="view.html" th:href="@{'/users/' + ${user.id}}"
               th:text="${user.name}">waylau</a></td>
    </tr>
    </tbody>
</table>
<div th:replace="~{fragments/footer :: footer}">...</div>
</body>
</html>

form.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <title th:text="${userModel.title}">users : View</title>
</head>
<body>
<div th:replace="~{fragments/header :: header}">...</div>
<h3 th:text="${userModel.title}">Welcome to waylau.com</h3>
<div>
    <a href="/users">返回主页</a>
</div>
<form action="/users" method="POST" th:object="${userModel.user}">
    <input type="hidden" name="id" th:value="*{id}">
    名称:<br>
    <input type="text" name="name" th:value="*{name}">
    <br>
    年龄:<br>
    <input type="text" name="age" th:value="*{age}">
    <input type="submit" value="提交">
</form>
<div th:replace="~{fragments/footer :: footer}">...</div>
</body>
</html>

view.html

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <title th:text="${userModel.title}">users : View</title>
</head>
<body>
<div th:replace="~{fragments/header :: header}">...</div>
<h3 th:text="${userModel.title}">Welcome to waylau.com</h3>
<div>
    <a href="/users">返回主页</a>
</div>
<div>
    <p><strong>ID:</strong><span id="id" th:text="${userModel.user.id}">123</span></p>
    <p><strong>Name:</strong><span id="name" th:text="${userModel.user.name}">waylau</span></p>
    <p><strong>Age:</strong><span id="age" th:text="${userModel.user.age}">30</span></p>
</div>
​
<div>
    <a th:href="@{'/users/delete/' + ${userModel.user.id}}">删除 </a>
    | <a th:href="@{'/users/modify/' + ${userModel.user.id}}">修改</a>
</div>
<div th:replace="~{fragments/footer :: footer}">...</div>
</body>
</html>

下面是项目结构图:

4、扩展

1.异步加载

        在Thymeleaf中进行页面分页操作时,在翻页操作中,我们显然要通过异步方式加载数据,下面我们一分页操作来看其异步加载操作,首先其html页面如下:

我们对应的js代码为

然后后台代码如下:

最后附上分页的脚步如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
​
<nav data-th-fragment="page" data-th-if="${page.totalPages gt 0}" data-th-object="${page}">

    <!-- 处理页数小于等于7 的情况 -->
    <ul class="pagination" data-th-if="${page.totalPages le 7}" >
        <!-- 总记录数 -->
        <li class="tbpage-total-elements disabled">共[[${page.totalElements}]]条</li>

        <!-- 页面大小 -->
        <select class="custom-select tbpage-size" data-th-attr="pageIndex=${page.number}">
            <option data-th-each="i : ${#arrays.toIntegerArray({5,10,40,100})}" data-th-value="${i}" 
                 data-th-selected="${i eq page.size}" data-th-text="${i}"></option>
        </select>

        <!-- 上一页 -->
        <li class="page-item" data-th-classappend="*{first} ? 'disabled' : ''">
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=${page.number} - 1" aria-label="Previous">
                <span aria-hidden="true">«</span>
            </a>
        </li>

        <!-- 迭代生成页码 -->
        <li class="page-item" data-th-each="i : ${#numbers.sequence(1, page.totalPages)}" 
            data-th-classappend="${(page.number + 1) eq i} ? 'active' : ''" >
            <a class="page-link tbpage-item" data-th-attr="pageIndex=${i} - 1" href="javascript:void(0);">
                 <span data-th-text="${i}"></span>
            </a>
        </li>

        <!-- 下一页 -->
        <li class="page-item" data-th-classappend="*{last} ? 'disabled' : ''">
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=${page.number} + 1" aria-label="Next">
                <span aria-hidden="true">»</span>
            </a>
        </li>
    </ul>

    <!-- 处理页数大于7 的情况 -->    
    <ul class="pagination" data-th-if="${page.totalPages gt 7}" >
        <!-- 总记录数 -->
        <li class="tbpage-total-elements disabled">共[[${page.totalElements}]]条</li>

        <!-- 页面大小 -->
        <select class="custom-select tbpage-size" data-th-attr="pageIndex=${page.number}">
            <option data-th-each="i : ${#arrays.toIntegerArray({5,10,40,100})}" data-th-value="${i}" 
                 data-th-selected="${i eq page.size}" data-th-text="${i}"></option>
        </select>

        <!-- 上一页 -->
        <li class="page-item" data-th-classappend="*{first} ? 'disabled' : ''">
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=${page.number} - 1" aria-label="Previous">
                <span aria-hidden="true">«</span>
            </a>
        </li>

        <!-- 首页 -->
        <li class="page-item" data-th-classappend="${(page.number + 1) eq 1} ? 'active' : ''" >
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=0">1</a>
        </li>


        <!-- 当前页面小于等于4 -->
        <li class="page-item" data-th-if="${(page.number + 1) le 4}" data-th-each="i : ${#numbers.sequence(2,5)}" 
            data-th-classappend="${(page.number + 1) eq i} ? 'active' : ''" >
            <a class="page-link tbpage-item" href="javascript:void(0);" data-th-attr="pageIndex=${i} - 1">
                <span data-th-text="${i}"></span>
            </a>
        </li>

        <li class="page-item disabled" data-th-if="${(page.number + 1) le 4}">
            <a href="javascript:void(0);" class="page-link tbpage-item">
                <span aria-hidden="true">...</span>
            </a>
        </li>

        <!-- 最后一页与当前页面之差,小于等于3 -->
        <li class="page-item disabled" data-th-if="${(page.totalPages-(page.number + 1)) le 3}">
            <a href="javascript:void(0);" class="page-link tbpage-item">
                <span aria-hidden="true">...</span>
            </a>
        </li>  
        <li class="page-item" data-th-if="${(page.totalPages-(page.number + 1)) le 3}" data-th-each="i : ${#numbers.sequence(page.totalPages-4, page.totalPages-1)}" 
            data-th-classappend="${(page.number + 1) eq i} ? 'active' : ''" >
            <a class="page-link tbpage-item" href="javascript:void(0);" data-th-attr="pageIndex=${i} - 1">
                <span data-th-text="${i}"></span>
           </a>
        </li>

         <!-- 最后一页与当前页面之差大于3,且  当前页面大于4-->

        <li class="page-item disabled" data-th-if="${((page.number + 1) gt 4) && ((page.totalPages-(page.number + 1)) gt 3 )}">
            <a href="javascript:void(0);" class="page-link tbpage-item">
                <span aria-hidden="true">...</span>
            </a>
        </li> 
        <li class="page-item" data-th-if="${((page.number + 1) gt 4) && ((page.totalPages-(page.number + 1)) gt 3 )}" >
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=${page.number}">[[${page.number}]]</a>
        </li>
        <li class="page-item active" data-th-if="${((page.number + 1) gt 4) && ((page.totalPages-(page.number + 1)) gt 3 )}">
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=${page.number} + 1">[[${page.number + 1}]]</a>
        </li>
        <li class="page-item" data-th-if="${((page.number + 1) gt 4) && ((page.totalPages-(page.number + 1)) gt 3 )}">
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=${page.number} + 2">[[${page.number + 2}]]</a>
        </li>

        <li class="page-item disabled"  data-th-if="${((page.number + 1) gt 4) && ((page.totalPages-(page.number + 1)) gt 3 )}">
            <a href="javascript:void(0);" class="page-link tbpage-item">
                <span aria-hidden="true">...</span>
            </a>
        </li>

        <!-- 最后一页 -->
        <li class="page-item" data-th-classappend="${(page.number + 1) eq page.totalPages} ? 'active' : ''" >
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=${page.totalPages} - 1">[[${page.totalPages}]]</a>
        </li>

        <!-- 下一页 -->
        <li class="page-item" data-th-classappend="*{last} ? 'disabled' : ''">
            <a href="javascript:void(0);" class="page-link tbpage-item" data-th-attr="pageIndex=${page.number} + 1" aria-label="Next">
                <span aria-hidden="true">»</span>
            </a>
        </li>
    </ul>


</nav>
​
</body>
</html>

 

Spring Boot整合Thymeleaf实战:模板引擎选型、配置与渲染全攻略 在Java Web开发中,模板引擎是连接后端数据与前端页面的关键桥梁,用于实现服务端渲染,将动态内容高效输出为完整HTML。Thymeleaf作为现代Java生态中的主流模板引擎,凭借其“自然模板”特性,让模板本身就是合法HTML,浏览器可直接预览,同时与Spring Boot无缝集成,大幅提升前后端协作效率。其原理清晰、语法简洁,通过属性化操作(如th:text、th:each)即可完成页面渲染,避免了JSP中逻辑混入的弊病。这类技术非常适合管理后台、CMS内容页、内部系统及快速原型等场景,在不需要复杂前 阅读详情

相关推荐

spring框架漏洞整理(Spingboot Thymeleaf模板注入)

Spingboot Thymeleaf模板注入修复方案 1. 设置ResponseBody注解 如果设置ResponseBody,则不再调用模板解析 2. 设置redirect重定向 @GetMapping("/safe/redirect") public String redirect(@RequestParam String url) { return "redirect:" + url; //CWE-601, as we can control the hostname in redirect

计算机毕业论文源码,学生个人网页制作html源码。贴近用户做网络推广和互联网优化。 2136

thymeleaf 日志

thymeleaf日志

weixin_43931625的博客 1040

Spring Boot+Thymeleaf任务管理系统:从建库到答辩全流程指南

Web应用开发中,任务管理系统是典型的信息管理场景,涉及用户身份认证、权限隔离与任务状态流转等核心问题。合理设计数据库表结构、明确用户角色边界,是保障系统可用性与安全性的前提。基于Spring Boot与Thymeleaf的单体架构,通过服务端渲染简化前后端交互,配合Session拦截器实现登录校验,能够快速构建一个可演示的完整系统。这类方案非常适合毕业设计:既能覆盖用户管理、任务分配、状态更新等基础功能,又能体现分页查询、越权防护、逻辑删除等技术细节。文章从系统设计到代码落地,再到答辩演示与论文对齐,提供

weixin_34115824的博客 319

Springboot整合Thymeleaf 模板引擎(案例)- 语法详解

Thymeleaf是一个流行的模板引擎,是基于HTML的,语法应用在HTML标签中。该模板引擎采用java语言开发。

qq_60506984的博客 4406

spring boot+thymeleaf+bootstrap 简单实现后台管理系统界面(看评论酌情下载)

spring boot+thymeleaf+bootstrap 简单实现后台管理系统界面(看评论酌情下载)

从碰撞电离到雪崩击穿:Sentaurus仿真中的模型选择与验证艺术

本文深入探讨了Sentaurus TCAD中雪崩击穿模型的选取与验证方法,特别关注平行平面结的仿真实践。通过对比Chynoweth、van Overstraeten等主流模型的适用场景与精度差异,结合具体案例展示了从几何建模到边界条件设定的完整验证流程,并针对非理想因素提供了有效的补偿技术,助力工程师提升器件仿真的准确性与可靠性。

weixin_29074295的博客 602

第5章 Spring Boot模板Thymeleaf入门详解

Spring Boot模板Thymeleaf 语法详解

陈小房的自我修炼 2920

Claude skill-creator插件深度解析:从创建到优化的完整技能工程化体系

摘要:skill-creator是Anthropic推出的Claude技能开发插件,提供从创建到优化的全流程工程化支持。其核心架构采用渐进式披露机制和多Agent协作模式,包含Grader、Analyzer等专业评估组件。该插件支持自动化评测系统,通过测试用例管理、并行执行和结果聚合实现技能验证;提供迭代优化算法,基于数据驱动改进技能性能。典型应用场景包括标准化技能开发、自动化测试验证和触发优化,显著降低开发门槛并提升技能质量。

虚心若愚 1003

SpringBoot+Thymeleaf+模拟数据库+一套后台模板(练手项目总结)

说在前面: 这个呢,是我最近在自学springboot时候做的一个小项目,目前还没有整合Mybatis框架,后面会有整合。这个呢就是用来熟悉springboot的使用以及Thymeleaf的使用,html页面直接拿的bootstrap的一套后台模板,感觉还不错,这个主要用来练手,也不算是练手吧,算是学完一阶段的小demo,写这个就是为了总结,使脑子中的知识不是那么的杂乱,要清除每一个功能是如何实...

hsunnyc的博客 3197

HI3403烧录全流程:从ToolPlatform安装到eMMC配置避坑指南

本文详细介绍了海思HI3403芯片的完整烧录流程,涵盖从ToolPlatform工具安装、驱动配置到eMMC分区策略的实战指南。文章深入解析了BurnTool工具的使用方法、关键配置项,并提供了烧录过程中的常见问题排查与解决方案,旨在帮助开发者高效、稳定地完成HI3403嵌入式系统的部署。

weixin_29279101的博客 342

2024.6.15丨基于VFDB数据库的致病菌毒力因子注释实战指南

本文提供了一份基于VFDB数据库进行致病菌毒力因子本地化注释的实战指南。详细介绍了从环境准备、DIAMOND工具安装、VFDB数据库选择与构建,到核心比对操作及结果解读与过滤的全流程。重点阐述了本地化分析相较于在线工具在速度、稳定性和批量处理上的优势,旨在帮助微生物基因组研究人员高效、精准地识别致病菌的关键毒力因子。

weixin_29220405的博客 284

thymeleaf——适合后端渲染UI的前后端分离开发模式的模板引擎

thymeleaf,springboot推荐的模板引擎。thymeleaf优点:静态html嵌入标签属性,浏览器可以直接打开模板文件,便于前后端联调。springboot官方推荐方案。thymeleaf缺点:模板必须符合xml规范,比如:js脚本必须加入/*&lt;![CDATA[*/标识,否则一个&amp;符号就会导致后台模板合成抛异常,而且错误信息巨不友好。...

Wewon_real的博客 8662

C语言获取硬件信息(CPU序列号,硬盘序列号,网卡IP、MAC地址、是否插入网线)

本文主要介绍Linux和Windows下使用C语言获取各种硬件信息,包括CPU序列号、硬盘序列号、网卡信息(包括网卡名字、IP地址、MAC地址、网卡是否插入网线等)。

叶落花枯的博客 9389

Spring Boot 学习之路 -- Thymeleaf 模板引擎

Spring Boot 遵循“前后端分离”的设计理念。所谓的“前后端分离”中的前端指网页端、客户端,后端指服务器端。后端只提供服务接口,前端只有通过访问后端接口才能获取到数据。此外,前端还要完成页面的布局、渲染等工作。如果想让前端获取的数据可以根据用户的操作而发生变化,就需要使用动态网页技术。为此,Spring Boot 采用了 Web 模板引擎技术。Thymeleaf 是 Spring Boot 官方推荐使用的模板引擎。

Fucking Android & Compose & Harmony & Spring Boot ... 2154

Roboguide与TIA V16通讯

Roboguide与TIA V16通讯

haier123888的博客 2690

【K210+STM32项目】基于K210与STM32协同工作的智能垃圾分类系统设计与实现(更新3.0版本)(完整工程资料源码)

STM32F407STM32F407_2(含有app功能)

嵌入式技术开发 2503

Spring Boot 2 手动渲染 Thymeleaf:摆脱MVC,用模板引擎生成HTML

模板引擎是后端开发中将静态模板与动态数据分离的核心工具,而Thymeleaf作为Java生态中主流的模板引擎,常被绑定在Spring MVC的视图解析链路中使用。但在定时任务、消息推送、邮件生成等场景下,并不存在HTTP请求,传统自动视图解析无法生效。此时,通过手动获取SpringTemplateEngine实例并调用process方法,即可直接完成模板字符串的渲染输出。手动渲染不仅解耦了模板与Web层,还能灵活控制模板来源、字符编码和缓存策略。本文从基础API入手,剖析Context变量注入、工具对象、模

weixin_34122810的博客 410

Anomalib库安装以及使用

Anomalib是一个专注于异常检测的深度学习库。它的目标是收集最新的异常检测算法,并提供用于在公共和私有数据集上进行基准测试的工具。该库提供了一系列已实现的异常检测算法,这些算法在最近的文献中有所描述。此外,Anomalib还提供了一套工具,方便开发人员开发和实现自定义模型。该库特别注重基于图像的异常检测,旨在识别数据集中的异常图像或图像中的异常像素区域。通过使用Anomalib,用户可以快速构建和评估各种异常检测算法,以满足其特定需求。

mingo_敏 5214

Spring Boot web开发之视图解析与模板引擎(尚硅谷笔记)

现代化、服务端Java模板引擎。

2301_78850344的博客 857

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

狂神Spring Boot 员工管理系统 【源码 + 笔记 + web素材】 超详细整理

目录员工管理系统1、准备工作1.1、导入资源1.2、编写pojo层1.3、编写dao层2、首页实现2.1、引入Thymeleaf2.2、编写MyMvcConfig2.3、测试首页3、页面国际化3.1、 File Encodings设置3.2、配置文件编写4、登录+拦截器4.1、登录4.2、登录拦截器5、展示员工列表5.1、员工列表页面跳转5.2、Thymeleaf公共页面元素抽取5.3、员工信息页面展示6、添加员工实现6.1、表单及细节处理6.2、具体添加功能7、修改员工信息8、删除员工实现9、404及注销

林深不见鹿 的博客 1万+

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Spring Boot + Thymeleaf 实战:从零构建用户管理系统

服务端渲染是Web开发中一种经典且高效的方式,通过模板引擎在服务器端动态生成HTML页面,能够有效降低前后端分离带来的复杂性与部署成本。Spring Boot作为Java生态的主流框架,搭配Thymeleaf模板引擎,可以快速实现CRUD、登录认证、权限控制等后台管理核心功能。本文从分层设计、数据持久化、密码加密到拦截器配置,逐步拆解一个完整用户管理系统的实现路径,并总结了静态资源加载、重复提交、中文乱码等典型问题的排查思路。无论是准备项目经验的初学者,还是需要快速搭建内部管理平台的开发者,都能从中获得一套

weixin_34265814的博客 327

Spring Boot 2.x基础教程:使用 Thymeleaf开发Web页面

点击上方蓝色“程序猿DD”,选择“设为星标”回复“资源”获取独家整理的学习资料!作者 | 翟永超来源 |http://blog.didispace.com/spring-boot-le...

程序猿DD 1311

Thymeleaf vs FreeMarker:5个常见“找不到模板”场景与性能对比

本文深入对比了Thymeleaf和FreeMarker两大Java模板引擎的5大核心差异,包括架构设计、模板加载机制、性能优化等关键方面。针对常见的'模板找不到'问题,提供了详细的解决方案和排查指南,帮助开发者根据项目需求做出最优技术选型。

weixin_30325971的博客 407

html + thymeleaf 制作邮件模板

html + thymeleaf 制作邮件模板

weixin_45483535的博客 1777

Spring Boot 后台实战:Thymeleaf 替换 JSP 与模板复用

在 Java Web 后台开发中,服务端模板引擎负责将模型数据渲染为 HTML,是连接后端业务与页面展示的基础技术。其原理是通过模板属性在服务端解析并替换占位内容,Thymeleaf 的模板本身又是合法 HTML,浏览器可忽略 th: 属性直接预览,因而具备改样式不重启、前后端协作成本低的技术价值。对表格、表单密集的后台页面、邮件模板、导出页面和内部工具站,这类服务端渲染方式更易维护。围绕 Spring Boot 集成 Thymeleaf、JSP 迁移、fragment 复用、表单校验、国际化与静态资源缓存

weixin_33552698的博客 252
上一篇: Spring Boot整理——spring boot 与常用组件整合(六)
下一篇: Spring Boot整理——Spring Data JPA(八)
盡盡
博客等级 码龄12年 812粉丝 118原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盡盡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值