基于velocity 生成HTML 的公用方法。
很简单留着纪念。时间长了容易忘记。
前台使用方式 类似于MAP 的 key 和 value 方式。这样的话 方便一些 ,后台方面就可以减少模板地方的代码书写一次性 向VM返回一个值
很简单留着纪念。时间长了容易忘记。
- /**
- * 通过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>
/**
* 通过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返回一个值