【问题标题】:Java: Is there a way to get the custom parameters from Freemarker TemplateJava:有没有办法从 Freemarker 模板中获取自定义参数
【发布时间】:2018-03-16 17:38:31
【问题描述】:

我想在我的 java 代码中获取我的模板中定义的所有自定义参数。 示例

<pre><code><html><head><style>td {
font-family: monospace;
font-size: 12px;
}</style></head><body>
<table width="16%" border="0" align="center" cellspacing="0" cellpadding="0">
   <tr>
     <td colspan="5" align="center">${test1}</td>
   </tr>
   <tr>
    <td colspan="5" align="center">${test2}</td>
   </tr>
</table>
</body></html></code></pre>

我想要在我的 java 代码中获取 test1 和 test2 参数。所以我想要下面的东西

Template template = this.getTemplate(ftl);
// a, should have test1, test2 and in case template any other template then it should get parameters for that too
String[] a= template.getAttributes();

【问题讨论】:

    标签: java freemarker


    【解决方案1】:

    没有,一般情况下是不可能的。即使 AST API 是公开的(但它不是),也有一些复杂性使得很难判断使用了哪些数据模型变量。在更复杂的模板中,test1 是来自数据模型还是设置在模板中并不明显。还可以有#import-s 和#include-s,它们可以引用更多的数据模型变量,但它们是动态链接的。

    FreeMarker 的方法是先知道数据模型,然后为它编写模板。

    【讨论】:

      猜你喜欢
      • 2013-04-16
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2019-08-16
      • 2019-04-28
      • 2010-12-07
      • 2018-06-18
      相关资源
      最近更新 更多