接受的答案似乎不适用于Thymeleaf 3;这是一个更新。请注意,我正在使用弹簧;这可能不适用于非春季应用。
<table>
<tr th:each="var : ${#vars.getVariableNames()}">
<td th:text="${var}"></td>
<td th:text="${#vars.getVariable(var)}"></td>
</tr>
<!--
Adding these manually because they are considered special.
see https://github.com/thymeleaf/thymeleaf/blob/thymeleaf-3.0.3.RELEASE/src/main/java/org/thymeleaf/context/WebEngineContext.java#L199
-->
<tr>
<td>param</td>
<td th:text="${#vars.getVariable('param')}"></td>
</tr>
<tr>
<td>session</td>
<td th:text="${#vars.getVariable('session')}"></td>
</tr>
<tr>
<td>application</td>
<td th:text="${#vars.getVariable('application')}"></td>
</tr>
</table>
也就是说,我所做的是创建了一个独立的Bean,它使事情变得更漂亮,并转储到日志而不是HTML:
@Component
public class ThymeleafDumper {
private Logger log = LoggerFactory.getLogger(ThymeleafDumper.class);
public void dumpToLog(WebEngineContext ctx) {
log.debug("Thymeleaf context: {}", formatThisUpNicely(ctx));
}
// ... etc
}
哪里可以使用,把结果放到一个,导出到,随便什么。不要忘记这三个“特殊”变量!formatThisUpNicely
ctx.getVariableNames()
SortedMap
json
然后将其的实例公开为 a 或 :@ModelAttribute
Controller
ControllerAdvice
@ControllerAdvice
public class SomeControllerAdvice {
@Autowired
private ThymeleafDumper thymeleafDumper;
@ModelAttribute("dumper")
public ThymeleafDumper dumper() {
return this.thymeleafDumper;
}
}
然后在我的模板中运行:
<div th:text="${dumper.dumpToLog(#vars)}"/>