velocity spring 生成HTML

来源:互联网 发布:c语言布尔类型有什么用 编辑:程序博客网 时间:2024/05/21 10:51
最近需要用到veloctiy 做模版 生成HTML 于是乎就简单研究了下。时间如流水。长期不用容易忘。简单记录下用法。

首先是spring 的配置方面:

Java代码  收藏代码
  1. <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">   
  2.         <property name="velocityProperties">   
  3.            <value>   
  4.             input.encoding=UTF-8   
  5.         output.encoding=UTF-8   
  6.             resource.loader = ds   
  7.         ds.resource.loader.public.name = String Template   
  8.           ds.resource.loader.description = Velocity String Template Resource Loader   
  9.      ds.resource.loader.class =  org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader   
  10.               ds.resource.loader.cache = false  
  11.               ds.resource.loader.modificationCheckInterval = 60  
  12.               velocimacro.library=   
  13.            </value>   
  14.         </property>   
  15.     </bean>   

PS:ClasspathResourceLoader  加载class 下的vm 模版

接下来看 spring mvc 的controller了部分

这里我做了一个生成分页,需要先计算一共有多少页 然后 根据pageNo 去取数据
Java代码  收藏代码
  1. /** 
  2.          * 获得数据总条数 
  3.          */  
  4.         int totalCount = categoryService.getVmListCount();  
  5.           
  6.         //获取总页数  
  7.         PageAjax<Category> page = new PageAjax<Category>(totalCount);  
  8.           
  9.         int totalPage = page.getTotalPages();  
  10.         //生成HTML 通过总页数判断生成几个HTML  
  11.           
  12.         Template velocity_template =velocityEngine.getTemplate("vmtem/pageList.vm","utf-8");  
  13.           
  14.           
  15.         for(int pageNo=1;pageNo<=totalPage;pageNo++){  
  16.               VelocityContext context = new VelocityContext();  
  17.               PageAjax<Category> list = categoryService.getvmForList(pageNo, totalCount);  
  18.               context.put("list", list);  
  19.               FileOutputStream fos = new FileOutputStream("d:\\test\\pageList"+pageNo+".html");    
  20.                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(    
  21.                         fos, "UTF-8"));//设置写入的文件编码,解决中文问题    
  22.               velocity_template.merge(context, writer);  
  23.                 
  24.               writer.close();  
  25.   
  26. //sevice 的部分代码  
  27.   
  28.     public PageAjax<Category> getCategoryAllForList(int pageNo,int pageSize){  
  29.           
  30.         int totalCount = categoryServiceMapper.getCategoryForListCount();  
  31.         PageAjax<Category> page = new PageAjax<Category>(totalCount, pageNo,pageSize);  
  32.         Map<String,Object> params = new HashMap<String,Object>();  
  33.         params.put("start", (pageNo-1)*pageSize);  
  34.         params.put("end", pageSize);  
  35.         page.setResult(categoryServiceMapper.getCategoryForList(params));  
  36.         return page;  
  37.     }  


接下来看下VM 的模版

Java代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
  5.   
  6.       <title>Course List</title>  
  7.   
  8.     </head>  
  9.   
  10.     <body>  
  11.   
  12.       <h2>COURSE LIST</h2>  
  13.     <table border="1" style="margin-left: 100px" >  
  14.         <tr>  
  15.           
  16.             <th class="jobs-time">序号</th>  
  17.             <th class="jobs-title">产品名称</th>  
  18.             <th class="jobs-title">createTime</th>  
  19.             <th class="jobs-title">createUser</th>  
  20.           
  21.             <th class="jobs-title">操作-------${list.totalPages}</th>  
  22.         </tr>  
  23.      #foreach($course in ${list.result})  
  24.       
  25.         <tr>  
  26.             <td width="13%" align="center">  
  27.               ${course.categroyId}  
  28.             </td>  
  29.            <td>${course.name}</td>  
  30.   
  31.           <td>${course.createTime}</td>  
  32.   
  33.           <td>${course.createUser}</td>  
  34.             
  35.             <td width="20%" align="center">  
  36.             <a href="#" class="btn">详细</a>|<a href="#" class="btn">删除</a>  
  37.             </td>  
  38.         </tr>  
  39.         #end   
  40.     </table>  
  41.   
  42.     <div class="page_list">  
  43.         <div class="list_info">  
  44.             #if (${list.firstPage} == false)  
  45.             
  46.             <a title="首页" href="pageList1.html">首页</a>  
  47.             <a title="上一页" href="pageList${list.prePage}.html">上一页</a>  
  48.             #end  
  49.         #foreach($page in ${list.slider})  
  50.                 #if ($page == ${list.pageNo})              
  51.                     <span class="current">  
  52.                         <em>${page}</em>  
  53.                   </span>  
  54.                 #else   
  55.                   <a href="pageList${page}.html">${page}</a>       
  56.                 #end   
  57.             #end   
  58.             #if (${list.lastPage} == false)  
  59.                 <a title="下一页" href="pageList${list.nextPage}.html">下一页</a>  
  60.             <a title="最后一页" href="pageList${list.totalPages}.html">最后一页</a>  
  61.             #end  
  62.             <br>  
  63.           
  64.           
  65.         </div>  
  66.     </div>  
  67.   
  68.     </body>  
  69.   
  70. </html>   

以上记录完毕。希望对大家能有所帮助
0 0