Spring 如何将变量插入';然后';表达
我正在尝试创建一个thymeleaf片段,其行为如下所示:Spring 如何将变量插入';然后';表达,spring,thymeleaf,spring-el,Spring,Thymeleaf,Spring El,我正在尝试创建一个thymeleaf片段,其行为如下所示: 我尝试过插值| alert-${type}|和串联'alert-'+type,但无法使其工作。有什么建议吗?在ThymileAF片段中,添加一个空检查: <section th:fragment="alert(type)" th:class="${type != null ? 'alert-' + type : ''}"> </section>
我尝试过插值| alert-${type}|和串联'alert-'+type,但无法使其工作。有什么建议吗?在ThymileAF片段中,添加一个空检查:
<section th:fragment="alert(type)"
th:class="${type != null ? 'alert-' + type : ''}">
</section>
对于空的情况:
<div th:replace="alert_fragment.html :: alert(type=null)"></div>
如果您正在从服务器传递类型值,那么它当然是这样的:
<div th:replace="alert_fragment.html :: alert(type=${info})"></div>
生成的HTML:
<body>
<section class="alert-info"></section>
<section></section>
</body>
更新
如果警报类型永远不会为null,则可以将片段简化为:
<section th:fragment="alert(type)" th:class="'alert-' + ${type}"></section>
在ThymileAF片段中,添加空检查:
<section th:fragment="alert(type)"
th:class="${type != null ? 'alert-' + type : ''}">
</section>
对于空的情况:
<div th:replace="alert_fragment.html :: alert(type=null)"></div>
如果您正在从服务器传递类型值,那么它当然是这样的:
<div th:replace="alert_fragment.html :: alert(type=${info})"></div>
生成的HTML:
<body>
<section class="alert-info"></section>
<section></section>
</body>
更新
如果警报类型永远不会为null,则可以将片段简化为:
<section th:fragment="alert(type)" th:class="'alert-' + ${type}"></section>
fantastic:)。谢谢顺便说一句,在我的测试中,我总是提供一个值,为什么即使有一个值,也需要空检查?我将更新答案并添加一个注释-您可以简化代码。太棒了:)。谢谢顺便说一句,在我的测试中,我总是提供一个值,为什么即使有一个值,空检查也是必要的?我将更新答案并添加一个注释-您可以简化代码。