velocity spring 生成HTML
来源:互联网 发布:c语言布尔类型有什么用 编辑:程序博客网 时间:2024/05/21 10:51
最近需要用到veloctiy 做模版 生成HTML 于是乎就简单研究了下。时间如流水。长期不用容易忘。简单记录下用法。
首先是spring 的配置方面:
PS:ClasspathResourceLoader 加载class 下的vm 模版
接下来看 spring mvc 的controller了部分
这里我做了一个生成分页,需要先计算一共有多少页 然后 根据pageNo 去取数据
接下来看下VM 的模版
以上记录完毕。希望对大家能有所帮助
首先是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 0
- velocity spring 生成HTML
- velocity 生成html
- spring velocity生成静态文件
- 使用Spring计时器和velocity模板定时生成静态html/jsp文件
- 基于velocity 生成HTML 的公用方法。
- 使用Apache Velocity生成HTML报表
- Velocity + Spring定时器 生成静态页面
- Spring velocity
- Spring Velocity
- VELOCITY HTML 模板
- jsp与html+velocity
- 代码生成框架Velocity
- 使用VELOCITY生成文件
- velocity 代码模板生成
- Velocity生成静态页面
- velocity 用模版生成HTML,PDF文件(解决了中文问题)
- JFinal+Velocity环境下封装根据.vm文件生成.html文件的工具案例
- velocity+struts+spring+ibatis
- 创建并管理图像动画,滑块和步进控件
- Android EditText样式自定义
- 美图秀秀各插件适用场景
- sqlserver 以年月日为条件查询记录
- OpenSSL原理
- velocity spring 生成HTML
- Linux内核无法启动问题
- Oracle Apps DBA 常用命令
- hdu 5074 相邻数和最大dp
- Linux内核API
- 嵌入式 hi3518x平台h264+g711a封装mp4代码demo
- 利用UIImageView实现动画特效
- tinyos中节点-pc串口通信实验中net.tinyos.message包出错问题的解决方案
- 有关C#中的数学函数