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>


以上记录完毕。希望对大家能有所帮助
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值