【问题标题】:Proper Thymeleaf syntex within a th:replace tagth:replace 标签中正确的 Thymeleaf 语法
【发布时间】:2019-10-31 03:35:37
【问题描述】:

我有这个代码块 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 列)

【问题讨论】:

    标签: spring syntax thymeleaf


    【解决方案1】:

    这应该可行:

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

    【讨论】:

    • 感谢您的回复。我试过code
    • code 正如你所说,但我收到了同样的错误。我 90% 确定我没有正确输入 thymeleaf 标签,即使在文档中也很难找到一些信息。
  • {#ctx.httpSession}, 是什么,为什么要在这样的标签中使用它?它看起来不像是有效的 html。
  • 这是我不需要的东西,完全没用。来自我对模板代码如何工作的误解。此代码

    姓:id

    没有它也可以工作。
  • 这有点用。但输出是“/users/null/coworkers/find”我认为 th:replace 导致 id 出现为空。 code
  • code
  • 你定义了th:object吗?这就是*{...}(星号而不是美元符号)表达式的用途。你的模型对象叫什么名字?
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签