我想创建一个速度模板,我可以在其中获取所有上下文属性。 我只尝试了$ context.keys,但它返回了我的结果:
[text,VIEW_TOOLS_AVAILABLE,link,class,convert,esc,display,number,date,render,include,math,field,alternator,TOOLS_VERSION,GENERIC_TOOLS_AVAILABLE,sorter,context,loop,STRUTS_TOOLS_AVAILABLE,lists]
这是我用来创建引擎并在上下文中添加值的类。
package mypackage;
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.ToolManager;
public class VelocityTest {
public static void main(String[] args) throws Exception {
VelocityEngine ve = new VelocityEngine();
ve.init();
Template template = ve.getTemplate("Person.vtmpl");
ToolManager velocityToolManager = new ToolManager();
velocityToolManager.configure("tools.xml");
VelocityContext ctx = new VelocityContext(velocityToolManager.createContext());
ctx.put("FirstName", "John");
ctx.put("LastName", "Doe");
ctx.put("Age", 25);
Writer writer = new StringWriter();
template.merge(ctx, writer);
System.out.println(writer);
}
}
有人可以帮我写一个模板,它会让我得到我在上下文中添加的三个值。
tools.xml的内容
<tools>
<toolbox scope="application">
<tool class="org.apache.velocity.tools.generic.AlternatorTool"/>
<tool class="org.apache.velocity.tools.generic.DisplayTool"/>
<tool class="org.apache.velocity.tools.generic.MathTool"/>
<tool class="org.apache.velocity.tools.generic.NumberTool"/>
<tool class="org.apache.velocity.tools.generic.ComparisonDateTool"/>
<tool class="org.apache.velocity.tools.generic.ClassTool"/>
<tool class="org.apache.velocity.tools.generic.ConversionTool"/>
<tool class="org.apache.velocity.tools.generic.EscapeTool"/>
<tool class="org.apache.velocity.tools.generic.FieldTool"/>
<tool class="org.apache.velocity.tools.generic.ListTool"/>
<tool class="org.apache.velocity.tools.generic.ResourceTool"/>
<tool class="org.apache.velocity.tools.generic.SortTool"/>
</toolbox>
</tools>