Gio*_*tta 6 java stringtemplate-4
我开始使用 StringTemplate 4,我正在尝试从存储在数据库中的简单字符串创建模板。我使用这样的东西:
STGroup group = new STGroupString(null, someTemplateString, '$', '$');
ST st = group.getInstanceOf(someTemplateName);
st.add(someAttribute, someValue);
Run Code Online (Sandbox Code Playgroud)
现在,如果我定义的全部或少于为模板定义的属性,则一切正常someTemplateName。现在,如果我尝试添加一个不存在的属性,则会出现以下异常:
no such attribute: fake
java.lang.IllegalArgumentException: no such attribute: fake
...
Run Code Online (Sandbox Code Playgroud)
这是有道理的。但是,我似乎无法事先知道为 template 定义了哪些属性someTemplateName。我期待找到类似的东西:
bool isDef = st.isDefined(someAttribute);
Run Code Online (Sandbox Code Playgroud)
但是没有这样的方法。我对么?有没有办法解决?
您可以使用st.impl.formalArguments来访问Map<String, FormalArgument>定义参数的位置。请注意,对于某些模板,此字段将为null。
| 归档时间: |
|
| 查看次数: |
1920 次 |
| 最近记录: |