基于velocity 生成HTML 的公用方法。

基于velocity 生成HTML 的公用方法。
很简单留着纪念。时间长了容易忘记。
/**
* 通过velocity 模板生成静态HTML 文件
* @param htmlName 文件名称
* @param toFilePath 文件位置
* @param vmTemPath velocity模板文件路径
* @param map 集合
* @param contextName 模板循环名称
*/
public void createHtmlByVelocity(String htmlName,String toFilePath,
String vmTemPath,Map<String,Object> params,String contextName){
String filePath=toFilePath+File.separator+htmlName+".html";

try {
FileOutputStream fos = new FileOutputStream(filePath);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
fos, Const.EncodingUTF8));
Template velocity_template =velocityEngine.getTemplate(vmTemPath,Const.EncodingUTF8);

VelocityContext context = new VelocityContext();
context.put(contextName, params);
velocity_template.merge(context,writer );
writer.close();


} catch (Exception e) {
log.error("文件路径失败!",e);
throw new MYException("文件路径失败!", e);
}
}




<h2>${list.title}</h2>
<table border="1" style="margin-left: 100px" >
<tr>

<th class="jobs-time">序号</th>
<th class="jobs-title">名称</th>
<th class="jobs-title">mobileNo</th>
<th class="jobs-title">email</th>

<th class="jobs-title">操作------${list.test}---asasddd</th>
</tr>

#foreach($course in ${list.list})

<tr>
<td width="13%" align="center">
${course.userId}
</td>
<td>${course.name}</td>

<td>${course.mobileNo}</td>

<td>${course.email}</td>


</tr>
#end
</table>




前台使用方式 类似于MAP 的 key 和 value 方式。这样的话 方便一些 ,后台方面就可以减少模板地方的代码书写一次性 向VM返回一个值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值