列出Thymeleaf中所有可用的模型属性
出于调试目的,我想列出我的百万美元模板在渲染时可用的所有模型属性。
就像是:
但显然这是胡说八道,我得到了一个当之无愧的错误。 ( org.springframework.expression.spel.SpelParseException: EL1070E:(pos 0): Problem parsing left operand
)
有没有办法输出这样的调试信息? 即使是一些日志输出,我也会满意。
或者,Thymeleaf是否提供类似于Struts 2的struts.devMode
,它在页面底部添加了一个列出所有可用属性的调试部分?
尝试这个:
接受的答案似乎不适用于Thymeleaf 3; 这是一个更新。 请注意我正在使用Spring; 这可能不适用于非Spring应用程序。
param session application
也就是说,我所做的是创建一个独立的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
,无论如何。 不要忘记那三个’特殊’变量!
然后在Controller
或ControllerAdvice
中将其实例公开为@ModelAttribute
:
@ControllerAdvice public class SomeControllerAdvice { @Autowired private ThymeleafDumper thymeleafDumper; @ModelAttribute("dumper") public ThymeleafDumper dumper() { return this.thymeleafDumper; } }
然后在我的模板中运行:
这些都是可用的日志记录配置:
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
这些将记录所有百里香的行动。 我希望它有所帮助。