java 列出 Thymeleaf 中所有可用的模型属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31387526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:31:45  来源:igfitidea点击:

List all available model attributes in Thymeleaf

javathymeleaf

提问by David Lavender

For debugging purposes I would like to list allmodel attributes available to my thymeleaf template while it is rendering.

出于调试目的,我想列出渲染时我的 thymeleaf 模板可用的所有模型属性。

Something like:

就像是:

<table>
    <tr th:each="model : ${*}">
        <td th:text="${model}"></td>
    </tr>
</table>

But obviously that's nonsense, and I get a well-deserved error. (org.springframework.expression.spel.SpelParseException: EL1070E:(pos 0): Problem parsing left operand)

但显然这是无稽之谈,我得到了一个当之无愧的错误。( org.springframework.expression.spel.SpelParseException: EL1070E:(pos 0): Problem parsing left operand)

Is there a way of outputting such debug information? I'd settle even for some logging output.

有没有办法输出这样的调试信息?我什至会满足于一些日志输出。

Or, does Thymeleaf provide something similar to Struts 2's struts.devModewhere it added a debug section at the bottom of the page listing all available properties?

或者,Thymeleaf 是否提供了类似于 Struts 2 的东西struts.devMode,它在页面底部添加了一个调试部分,列出了所有可用的属性?

回答by the4dK

Try this:

试试这个:

<table>
    <tr th:each="var : ${#vars}">
        <td th:text="${var.key}"></td>
        <td th:text="${var.value}"></td>
    </tr>
</table>

回答by Roy Truelove

The accepted answer does not seem to work for Thymeleaf 3; here's an update. Please note that I'm using Spring; this might not work for non-Spring apps.

接受的答案似乎不适用于 Thymeleaf 3;这是一个更新。请注意,我使用的是 Spring;这可能不适用于非 Spring 应用程序。

    <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're 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>

That said, what I've done is created a standalone Bean that makes things a bit prettier and dumps to logs instead of to HTML:

也就是说,我所做的是创建了一个独立的 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
}

Where formatThisUpNicelycan use ctx.getVariableNames(), put the results into a SortedMap, export to json, whatever. Don't forget those three 'special' variables!

哪里formatThisUpNicely可以用ctx.getVariableNames(),把结果放到一个SortedMap,导出到json,随便。不要忘记这三个“特殊”变量!

Then expose an instance of it as a @ModelAttributein a Controlleror a ControllerAdvice:

然后将它的一个实例公开为 a @ModelAttributein aController或 a ControllerAdvice

@ControllerAdvice
public class SomeControllerAdvice {

    @Autowired
    private ThymeleafDumper thymeleafDumper;

    @ModelAttribute("dumper")
    public ThymeleafDumper dumper() {
        return this.thymeleafDumper;
    }
}

Then in my template run:

然后在我的模板中运行:

<div th:text="${dumper.dumpToLog(#vars)}"/>

回答by Hossein

these are all the logging available configurations :

这些是所有可用的日志记录配置:

log4j.logger.org.thymeleaf=DEBUG
log4j.logger.org.thymeleaf.TemplateEngine.CONFIG=DEBUG
log4j.logger.org.thymeleaf.TemplateEngine.TIMER=DEBUG
log4j.logger.org.thymeleaf.TemplateEngine.cache.TEMPLATE_CACHE=DEBUG
log4j.logger.org.thymeleaf.TemplateEngine.cache.FRAGMENT_CACHE=DEBUG
log4j.logger.org.thymeleaf.TemplateEngine.cache.MESSAGE_CACHE=DEBUG
log4j.logger.org.thymeleaf.TemplateEngine.cache.EXPRESSION_CACHE=DEBUG

these will log all the thymeleaf actions. I hope it is helpful.

这些将记录所有百里香叶操作。我希望它有帮助。