freemarker支持在数据模型中显示所有变量吗?

编程入门 行业动态 更新时间:2024-10-26 14:35:09
本文介绍了freemarker支持在数据模型中显示所有变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在freemarker数据模型中查看所有变量,就像struts2 debug标签显示值堆栈一样.

freemarker有办法做到这一点吗?

解决方案

没有通用的解决方案,但是您可以尝试

<#list .data_model?keys as key> ${key} </#list>

如果数据模型只是普通的Map或JavaBean,这将起作用,但对于更复杂的数据模型,如果它支持?keys且确实返回了所有内容,则取决于数据模型的实现.

您还具有在模板中设置的变量,可以像上面列出的那样,仅使用.globals,.namespace(表示当前模板名称空间)和.locals代替.data_model. /p>

您可能还具有Configuration级共享变量,并且无法单纯从FTL列出这些变量(您可以为其编写自定义的TemplateMethodModel,尽管它读取Configuration.getSharedVariableNames()并从模板中调用它)

当然,理想情况下,FreeMarker应该具有<#show_variables>指令或某种指令,可以尽最大努力显示所有这些信息……但可惜的是还没有这种东西.

I want to see all variables in freemarker data-model, just like struts2 debug tag to show value stack.

Is there a way for freemarker to do this ?

解决方案

There's no universal solution possible for that, but you can try

<#list .data_model?keys as key> ${key} </#list>

This works if the data-model is just a usual Map or JavaBean, but for more sophisticated data-models it's up to the data-model implementation if it supports ?keys and if it indeed returns everything.

You also have the variables that you set in the templates, which can be listed like above, only instead of .data_model use .globals, .namespace (which means the current template namespace) and .locals.

You may also have Configuration-level shared variables, and there's no way to list those purely from FTL (you could write a custom TemplateMethodModel for it that reads Configuration.getSharedVariableNames() though, and call it from the template).

Of course, ideally, FreeMarker should have a <#show_variables> directive or something, that does a best effort to show all this... but sadly there is no such thing yet.

更多推荐

freemarker支持在数据模型中显示所有变量吗?

本文发布于:2023-11-04 05:49:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   数据模型   freemarker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!