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

基于velocity 生成HTML 的公用方法。
很简单留着纪念。时间长了容易忘记。
Java代码 复制代码 收藏代码
  1. /**
  2. * 通过velocity 模板生成静态HTML 文件
  3. * @param htmlName 文件名称
  4. * @param toFilePath 文件位置
  5. * @param vmTemPath velocity模板文件路径
  6. * @param map 集合
  7. * @param contextName 模板循环名称
  8. */
  9. public void createHtmlByVelocity(String htmlName,String toFilePath,
  10. String vmTemPath,Map<String,Object> params,String contextName){
  11. String filePath=toFilePath+File.separator+htmlName+".html";
  12. try {
  13. FileOutputStream fos = new FileOutputStream(filePath);
  14. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
  15. fos, Const.EncodingUTF8));
  16. Template velocity_template =velocityEngine.getTemplate(vmTemPath,Const.EncodingUTF8);
  17. VelocityContext context = new VelocityContext();
  18. context.put(contextName, params);
  19. velocity_template.merge(context,writer );
  20. writer.close();
  21. } catch (Exception e) {
  22. log.error("文件路径失败!",e);
  23. throw new MYException("文件路径失败!", e);
  24. }
  25. }
  26. <h2>${list.title}</h2>
  27. <table border="1" style="margin-left: 100px" >
  28. <tr>
  29. <th class="jobs-time">序号</th>
  30. <th class="jobs-title">名称</th>
  31. <th class="jobs-title">mobileNo</th>
  32. <th class="jobs-title">email</th>
  33. <th class="jobs-title">操作------${list.test}---asasddd</th>
  34. </tr>
  35. #foreach($course in ${list.list})
  36. <tr>
  37. <td width="13%" align="center">
  38. ${course.userId}
  39. </td>
  40. <td>${course.name}</td>
  41. <td>${course.mobileNo}</td>
  42. <td>${course.email}</td>
  43. </tr>
  44. #end
  45. </table>
/**
	 * 通过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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值