English 中文(简体)
Java代码动态参考价值修改
原标题:Velocity template dynamic reference value modification from Java code

I need to replace references (properties and methods) in the Velocity templates to some other values during template rendering. For example, from the template below:

___
I have $some text $daisy
#foreach ($someElement in $someCollection)
  #$someElement.doSomething()
#end
___

我需要获得这样的案文:

___
I have lalala1 text lalala1
lalala2
lalala2
lalala2
___

Besides, the reason is I don t know the names of references and the amount of them beforehand.

在技术上,我确实可以开放模板,进行人工研究,但我希望逃脱。

First of all, I tried to get references names from the template, working with implementation of ReferenceInsertionEventHandler, but the problem was in directives.

For example, with foreach I had exception "Could not determine type of iterator in #foreach loop" I looked through the velocity source code, I can change it as I need, but it will kill future project maintainability.

So the question is: is there any way to get all references names (properties and methods) from template and to change the values of them during rendering?

最佳回答

我不敢肯定,如果我完全理解你的问题,但如果你想要把一个变数与其他变量(实际上,要作一个前后一致的变数重新命名)相替代,那么你仍然需要知道什么变数将用其他变数取代。

你可以像你一样经常操作速度模板。 例如,在第一张通行证中,你可以用其他美元取代某些美元,在第二张通行证中,用你真正想要的最终价值替代其他美元。

然而,为了始终如一地把所有变量(例如宏观变量)纳入模板,我认为,依靠大鼠的工具通常会使用经过修改的版本。 例如,在这种情况下,你需要从模板中挑选出一名来访者,并收集ASTVariable ,但我认为这不是一件容易的任务。

例如,outdated example

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签