读写文件 mvel 渲染


Hello, my name is @{name.toUpperCase()}


print(java.lang.System.getProperty("user.name"));
var FileUtils = Java.type("org.apache.commons.io.FileUtils");
var File = Java.type("java.io.File");
var TemplateRuntime = Java.type("org.mvel2.templates.TemplateRuntime");

function readFileToString(fileName, encoding) {
var file = new File(fileName);
return FileUtils.readFileToString(file, encoding);

}
function writeStringToFile(fileName, content, encoding) {
var file = new File(fileName);
return FileUtils.writeStringToFile(file, content, encoding);
}
function rendertmpl() {
var template = readFileToString("a.txt","utf-8");
var vars = {
"name" : "Michael",
"bb" : "Michael"
};
var out = TemplateRuntime.eval(template, vars);
//print(out);
writeStringToFile("b.txt",out,"utf-8");
}
rendertmpl();



@foreach{tm : tms}
- @{tm["tableName"]}
- @{tm["modelName"]}
@foreach{cm : tm["cms"]}
- @{cm["columnName"]}
- @{cm["columnClass"]}
- @{cm["columnComment"]}
@end{}
@end{}




import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

public class App {
public static void main(String[] args) {
String fileName = "a.txt";
File file = new File(fileName);
String template = "";
try {
template = org.apache.commons.io.FileUtils.readFileToString(file, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
Map<String,Object> cm= new HashMap<String, Object>();
cm.put("columnName", "columnNamev");
cm.put("columnClass", "columnClassV");
cm.put("columnComment", "columnCommentV");
List<Map<String,Object>> cms = new ArrayList<Map<String,Object>>();
cms.add(cm);

Map<String, Object> tm = new HashMap<String, Object>();
tm.put("tableName", "tableNameV");
tm.put("modelName", "modelNameV");
tm.put("cms", cms);

Map<String, Object> vars = new HashMap<String, Object>();
List<Map<String,Object>> tms = new ArrayList<Map<String,Object>>();
tms.add(tm);

vars.put("tms", tms);

String output = (String) TemplateRuntime.eval(template, vars);
System.out.println(output);
// compile the template
CompiledTemplate compiled = TemplateCompiler.compileTemplate(template);

// execute the template
//String output = (String) TemplateRuntime.execute(compiled, vars);

System.out.println(java.lang.System.getProperty("java.class.path"));

/*String fileName = "a.txt";
File file = new File(fileName);
String fileContent = "";
try {
fileContent = org.apache.commons.io.FileUtils.readFileToString(
file, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("");
fileContent += "Helloworld";
try {
org.apache.commons.io.FileUtils.writeStringToFile(file,
fileContent, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
*/
}
}



jjs -cp lib\commons-io-2.4.jar;lib\mvel2-2.2.7.Final.jar mvel.js
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 <h1>@{name}</h1> Simple Iteration <p>@foreach{index : alphabetical}<a href="@{index.uri}">@{index.description}</a>@end{}</p> Accessing Static Methods <a href="@{ua.pageURI}">@{org.apache.commons.lang.StringEscapeUtils.escapeHtml(ua.name)}</a> Inline Ternary Operator <li>@{ua.hitsTotal} total @{ua.hitsTotal == 1 ? "Hit" : "Hits"}.</li> 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值