在 thymeleaf 中为变量名称设置值 [英] setting up a value for a variable name in thymeleaf

查看:118
本文介绍了在 thymeleaf 中为变量名称设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Thymeleaf 的新手,正在将我的网页从 JSP 转换为 Thymeleaf.我有一个像这样的 strut 标签:

该变量可以在 JSP 中的任何地方使用.Thymeleaf 中是否有这样的替代方案?

解决方案

您可以使用 局部变量.

使用 th:with 属性声明一个 HTML 元素.例如

文档说明

<块引用>

当处理 th:with 时,该 [someVariable] 变量被创建为局部变量并添加到来自上下文的变量映射中,以便它与任何其他变量一样可用于评估从一开始就在上下文中声明,但仅限于范围内包含标签.

I am new to Thymeleaf and converting my Web page from JSP to Thymeleaf. I have a strut tag like this:

<c:set var="someVariable" value="${someValue}"/>

That variable can be used anywhere in JSP. Is there any such alternatives for this in Thymeleaf?

解决方案

You can use local variables.

Declare an HTML element with a th:with attribute. For example

<div th:with="someVariable=${someValue}">

The documentation states

When th:with is processed, that [someVariable] variable is created as a local variable and added to the variables map coming from the context, so that it is as available for evaluation as any other variables declared in the context from the beginning, but only within the bounds of the containing tag.

这篇关于在 thymeleaf 中为变量名称设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆