发表于: 2017-10-13 23:22:11
1 643
【今日完成】
今天PM还在看项目,等待PM搞定。
今天思考了一些实现的具体细节。比如在列表页List传回表中所有的信息,又去复习了一下List集合。
然后因为代码生成用到的是基于velocity的,所以学习了一下velocity:
下面是一个简单的Hello程序:
注意是new了一个VelocityEngine,然后把一个VM文件传入。
Velocity大致就是把固定的代码写死,然后把VM中写好变量。
public class HelloVilocity {
public static void main(String[] args) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
Template t = ve.getTemplate("HelloVilocity.vm");
VelocityContext ctx = new VelocityContext();
ctx.put("name", "velocity");
ctx.put("date", (new Date()).toString());
List temp = new ArrayList();
temp.add("1");
temp.add("2");
ctx.put("list", temp);
StringWriter sw = new StringWriter();
t.merge(ctx, sw);
System.out.println(sw.toString());
}
}
VM文件,注意这里的#并不是注释,而是一个语法的前缀
像#set和#foreach都是语法
在VM文件中##才表示注释,多行注释为#* 注释内容 *#
#set( $iAmVariable = "good!" )
Welcome $name to velocity.com
today is $date.
#foreach ($i in $list)
$i
#end
$iAmVariable
最后打印出来的是VM文件的格式,其中$符号的变量会自动匹配名字相同的那个变量
System.out.println(sw.toString());
打印的格式与VM中的格式一模一样
其实VM就是一个模板,模板中的变量会从class中自动匹配。
【今日收获】
学习了Velocity
评论