spring - 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 列)
解决方案
这应该有效:
th:replace="::menuItem(*{'/users/' + user.id + '/coworkers/find'}, 'coworkers', 'find coworkers', 'search', 'Find coworkers')"
推荐阅读
- cmake - 使用 cmake 构建 Fortran 共享库的奇怪问题
- java - 从服务器上部署的 Spring Boot 应用程序中读取 API 元数据
- laravel - Laravel 急切加载 - 一对多关系
- python - 读取字符串,计算出现次数而不使用 pYthon 中的循环
- three.js - 使用单个透明平面的透明 z-fighting
- qt - 如何为多个按钮设置样式表但不是全部
- javascript - 在 react js 中创建一个自定义范围
- python-3.x - Python中跨多个程序的只读共享内存
- swiftui - SwiftUI 的 tabViewStyle PageView 不适用于纵向到横向的旋转
- oracle - 克隆自治数据库时计费何时开始,终止时计费何时结束?