首页 > 解决方案 > th:replace 标签中正确的 Thymeleaf 语法

问题描述

我有这个代码块 Thymeleaf 调用 Spring 上下文并拉入用户 ID。

 <li {#ctx.httpSession} >
                      <span th:object="${session.user}"> </span>
                      <span><p>Surname: <span th:text="*{user.id}">Pepper</span></p></span>

我想将其放入现有的 th:replace 列出的项目中。基本上我需要将静态 2 替换为 url 中的当前会话用户 ID,我尝试了许多选项,但似乎无法在线找到解决方案。

This is one of my attempts
<li sec:authorize="isAuthenticated()" {#ctx.httpSession}, th:object="${session.user}"  th:replace="::menuItem ( '/users/'+*{user.id}+'/coworkers/find','coworkers','find coworkers','search','Find coworkers')"><span>Find Coworkers</span>
                  </li>
This is the orginial 
<li sec:authorize="isAuthenticated()"  th:replace="::menuItem ( '/users/2/coworkers/find','coworkers','find coworkers','search','Find coworkers')">

我是 Thymeleaf 新手,非常感谢您的帮助,谢谢。

试过 { *{user.id} }

引起:org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:“user.id”(模板:“fragments/layout” - 第 65 行,第 106 列)

标签: springsyntaxthymeleaf

解决方案


这应该有效:

th:replace="::menuItem(*{'/users/' + user.id + '/coworkers/find'}, 'coworkers', 'find coworkers', 'search', 'Find coworkers')"

推荐阅读