在應用中使用velocity,一般需要以下的幾個步驟:
- 初始化Velocity,可以使用單例,或者運行期實例
- 創建context對象,用于包括相應的變量
- 在context中增加相應的數據
- 選擇模板
- 合并模板,產生輸出
如下的例子:
import java.io.StringWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.MethodInvocationException;
Velocity.init(); 1
VelocityContext context = new VelocityContext(); 2
context.put( "name", new String("Velocity") ); 3
Template template = null;
try
{
template = Velocity.getTemplate("mytemplate.vm"); 4
}
catch( ResourceNotFoundException rnfe )
{
// couldn't find the template
}
catch( ParseErrorException pee )
{
// syntax error: problem parsing the template
}
catch( MethodInvocationException mie )
{
// something invoked in the template
// threw an exception
}
catch( Exception e )
{}
StringWriter sw = new StringWriter();
template.merge( context, sw ); 5
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.MethodInvocationException;
Velocity.init(); 1
VelocityContext context = new VelocityContext(); 2
context.put( "name", new String("Velocity") ); 3
Template template = null;
try
{
template = Velocity.getTemplate("mytemplate.vm"); 4
}
catch( ResourceNotFoundException rnfe )
{
// couldn't find the template
}
catch( ParseErrorException pee )
{
// syntax error: problem parsing the template
}
catch( MethodInvocationException mie )
{
// something invoked in the template
// threw an exception
}
catch( Exception e )
{}
StringWriter sw = new StringWriter();
template.merge( context, sw ); 5
上面的例子使用的是單例模式,可以使用運行期實例:
VelocityEngine ve = new VelocityEngine();
ve.setProperty(
VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
ve.init();
ve.setProperty(
VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
ve.init();
關于context
context,類似于map環境,包括兩個主要的方法:
public Object put(String key, Object value);
public Object get(String key);
public Object get(String key);
而默認的VelocityContext是使用map封裝,保存相應的變量
當然,如果想和其他環境合并,如FacesContext中的Elcontext,需要定義自己的實現類。
Context chaining,
context支持多個context串,如下:
VelocityContext context1 = new VelocityContext();
context1.put("name","Velocity");
context1.put("project", "Jakarta");
context1.put("duplicate", "I am in context1");
VelocityContext context2 = new VelocityContext( context1 );
context2.put("lang", "Java" );
context2.put("duplicate", "I am in context2");
template.merge( context2, writer );
context1.put("name","Velocity");
context1.put("project", "Jakarta");
context1.put("duplicate", "I am in context1");
VelocityContext context2 = new VelocityContext( context1 );
context2.put("lang", "Java" );
context2.put("duplicate", "I am in context2");
template.merge( context2, writer );
Velocity不僅可以用于提供模板輸出,而且可以直接對字符串進行評估:
StringWriter w = new StringWriter();
Velocity.mergeTemplate("testtemplate.vm", context, w );
String s = "We are using $project $name to render this.";
w = new StringWriter();
Velocity.evaluate( context, w, "mystring", s );
Velocity.mergeTemplate("testtemplate.vm", context, w );
String s = "We are using $project $name to render this.";
w = new StringWriter();
Velocity.evaluate( context, w, "mystring", s );