gpt4 book ai didi

spring - 无法解析为赋值表达式 : "${attrs ?: defaultAttrs}"

转载 作者:行者123 更新时间:2023-12-02 21:01:44 24 4
gpt4 key购买 nike

以下 thymeleaf 标记缺少什么:

<tr th:fragment="row" th:with="defaultAttrs='placeholder=\'' + ${placeholder} + '\''">                            
<td>
<input th:attr="${attrs ?: defaultAttrs}" />
</td>
...
</tr>

调用方

<th:block th:include="row::row(attrs='value=\'*{prodName}\', minLength=\'.{2, 16}\', required, title=\'starts with an alphabet 2 and 8\' placeholder=\'Product name\'')" />

产生此错误:

Could not parse as assignation sequence: "${attrs ?: defaultAttrs}"

在一个不相关的注释中,必须仔细查看异常消息,以了解“赋值”一词的有趣用法,而不是“赋值”

最佳答案

您正在尝试将文本字符串传递给 th:attr 。 Thymeleaf 需要表达式,而不是字符串。下一个示例将不起作用,但这就是您想要做的:

<input th:attr="${'placeholder=\'defaultPlaceholder\''}" />

我建议您采用以下方法:

<tr th:fragment="row" th:with="defaultPlaceholder='placeholder', defaultMaxlength=10">                            
<td>
<input th:attr="placeholder=${placeholder?:defaultPlaceholder},
maxlength=${maxlength?:defaultMaxlength}" />
</td>
...
</tr>

它看起来更长,但让您可以更好地控制管理属性。


更新:如果您希望在一个字符串变量中传递所有属性,则可以使用 Thymeleaf 的 preprocessing 。例如,下一个代码是您将如何在页面中使用片段:

<div th:include="fragment :: row(attrs='value=\'*{prodName}\', minLength=\'.{2, 16}\', 
required=true, title=\'starts with an alphabet 2 and 8\', placeholder=\'Product name\'')">

然后你的片段将是这样的:

<div th:fragment="row"> 
<div th:with="defaults='placeholder=\'placeholder\', maxlength=10'" th:remove="tag">
<tr>
<td>
<input th:if="${attrs!=null}" th:attr="__${attrs}__"/>
<input th:if="${attrs==null}" th:attr="__${defaults}__"/>
</td>
...
</tr>
</div>
</div>

说明:

  • 片段的主标签不会包含在结果页面中。所以,不要使用<tr>作为主标签。相反,包裹 <tr>进入<div> .
  • 传递给片段的参数将覆盖 th:with 中声明的所有变量片段的主标签。因此,如果您想将任何参数传递给片段,请不要声明 th:with就在片段的主标签中。做片段的主体。
  • 如果不需要在结果页面输出一些标签,只需使用 th:remove attribute 。此属性允许您删除片段的部分内容。在此示例中,我们使用第二个 <div>只是声明th:with我们不需要这个 <div>在结果页面中。

您在attr中有错误th:include的参数。由于属性是名称和值对,因此您不能仅指定 required 。你必须写:required=true 。另一个错误:您错过了 title 之间的逗号和placeholder 。接下来应该是正确的字符串:

<th:block th:include="row::row(attrs='value=\'*{prodName}\', minLength=\'.{2, 16}\', 
required=true, title=\'starts with an alphabet 2 and 8\', placeholder=\'Product name\'')" />

关于spring - 无法解析为赋值表达式 : "${attrs ?: defaultAttrs}",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37935062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com