`
Java_大猫
  • 浏览: 170517 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

velocity spring 生成HTML

阅读更多
最近需要用到veloctiy 做模版 生成HTML 于是乎就简单研究了下。时间如流水。长期不用容易忘。简单记录下用法。

首先是spring 的配置方面:

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> 
        <property name="velocityProperties"> 
           <value> 
            input.encoding=UTF-8 
 	    output.encoding=UTF-8 
            resource.loader = ds 
  	    ds.resource.loader.public.name = String Template 
          ds.resource.loader.description = Velocity String Template Resource Loader 
	 ds.resource.loader.class =  org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader 
 			  ds.resource.loader.cache = false
			  ds.resource.loader.modificationCheckInterval = 60
			  velocimacro.library= 
           </value> 
        </property> 
    </bean> 

PS:ClasspathResourceLoader  加载class 下的vm 模版

接下来看 spring mvc 的controller了部分

这里我做了一个生成分页,需要先计算一共有多少页 然后 根据pageNo 去取数据
/**
		 * 获得数据总条数
		 */
		int totalCount = categoryService.getVmListCount();
		
		//获取总页数
		PageAjax<Category> page = new PageAjax<Category>(totalCount);
		
		int totalPage = page.getTotalPages();
		//生成HTML 通过总页数判断生成几个HTML
		
		Template velocity_template =velocityEngine.getTemplate("vmtem/pageList.vm","utf-8");
		
		
		for(int pageNo=1;pageNo<=totalPage;pageNo++){
			  VelocityContext context = new VelocityContext();
			  PageAjax<Category> list = categoryService.getvmForList(pageNo, totalCount);
			  context.put("list", list);
			  FileOutputStream fos = new FileOutputStream("d:\\test\\pageList"+pageNo+".html");  
			    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(  
			            fos, "UTF-8"));//设置写入的文件编码,解决中文问题  
			  velocity_template.merge(context, writer);
			  
			  writer.close();

//sevice 的部分代码

	public PageAjax<Category> getCategoryAllForList(int pageNo,int pageSize){
		
		int totalCount = categoryServiceMapper.getCategoryForListCount();
		PageAjax<Category> page = new PageAjax<Category>(totalCount, pageNo,pageSize);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("start", (pageNo-1)*pageSize);
		params.put("end", pageSize);
		page.setResult(categoryServiceMapper.getCategoryForList(params));
		return page;
	}



接下来看下VM 的模版

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

      <title>Course List</title>

    </head>

    <body>

      <h2>COURSE LIST</h2>
	<table border="1" style="margin-left: 100px" >
		<tr>
		
			<th class="jobs-time">序号</th>
			<th class="jobs-title">产品名称</th>
			<th class="jobs-title">createTime</th>
			<th class="jobs-title">createUser</th>
		
			<th class="jobs-title">操作-------${list.totalPages}</th>
		</tr>
	 #foreach($course in ${list.result})
	
		<tr>
			<td width="13%" align="center">
			  ${course.categroyId}
			</td>
		   <td>${course.name}</td>

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

          <td>${course.createUser}</td>
		  
			<td width="20%" align="center">
			<a href="#" class="btn">详细</a>|<a href="#" class="btn">删除</a>
			</td>
		</tr>
		#end 
	</table>

	<div class="page_list">
		<div class="list_info">
			#if (${list.firstPage} == false)
		  
			<a title="首页" href="pageList1.html">首页</a>
			<a title="上一页" href="pageList${list.prePage}.html">上一页</a>
			#end
		#foreach($page in ${list.slider})
				#if ($page == ${list.pageNo})		     
		        	<span class="current">
		          		<em>${page}</em>
		          </span>
		        #else 
		          <a href="pageList${page}.html">${page}</a>     
		    	#end 
			#end 
			#if (${list.lastPage} == false)
				<a title="下一页" href="pageList${list.nextPage}.html">下一页</a>
			<a title="最后一页" href="pageList${list.totalPages}.html">最后一页</a>
			#end
			<br>
		
		
		</div>
	</div>

    </body>

</html> 


以上记录完毕。希望对大家能有所帮助
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics