详细说明:
MVEL is an expression language – similar to OGNL – and a templating engine. I’d like to give you an example of MVEL Templates in this post so you can find out if MVEL might work for you. Templating Examples This is how templating with MVEL looks like. Basic Object Access @{name}
Simple Iteration @foreach{index : alphabetical}@{index.description}@end{}
Accessing Static Methods
@{org.apache.commons.lang. StringEscapeUtils.escapeHtml(ua.name)} Inline Ternary Operator
@{ua.hitsTotal} total @{ua.hitsTotal == 1 ? "Hit" : "Hits"}. MVEL Integration The following code integrates MVEL into your application. The first part parses a template from a String, the second part applies an object to the template and writes it to a file. public class MVELTemplateWriter { private final CompiledTemplate template; /** * Constructor for MVELTemplateWriter. * * @param template the MVEL template */ public MVELTemplateWriter(String template) { super(); this.template = TemplateCompiler.compileTemplate(template); } /** * Merge an Object with the template and write the output * tof. * * @param o the Object * @param f the output File */ public void write(Object o, File f) throws IOException { String output= (String) TemplateRuntime.execute(template, o); Writer writer= null; try { if (!f.getParentFile().exists()) { boolean created= f.getParentFile().mkdirs(); assert created; } writer= new OutputStreamWriter(new FileOutputStream(f), "UTF-8"); writer.write(output); } finally { close(writer); } }} You use this code like you would use other templating engines/expression languages: You add your objects to a Map and then merge the Map with a template. In the template, you reference the objects in the Map by their key. Note that the template is pre-compiled for performance reasons. You can use something like FileUtils.readFileToString(File) to read a template file into a String. ...展开收缩