package com.xxxx.demo.xxxxxx.testvm;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xxxxx.demo.xxxx.VelocityBuilder;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class VelocityTest {
static final Logger log =LoggerFactory.getLogger(VelocityBuilder.class);
private static final VelocityEngine ve = new VelocityEngine();
private static final ObjectMapper om = new ObjectMapper();
static {
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.setProperty("classpath.resource.loader.cache", false);
ve.init();
}
public static void creatHtml(String s){
try {
OutputStream outputStream =new FileOutputStream("/xxxxxxx/xxxx/project/xxxxx/src/main/resources/static/vmtest1.html");
outputStream.write(s.getBytes());
outputStream.close();
}catch (FileNotFoundException e){
System.out.println("文件没有找到");
}catch (IOException e){
System.out.println("输出异常");
}
}
public static void main(String[] args) {
Template template = ve.getTemplate("templates/test.vm", "utf-8");
VelocityContext ctx = new VelocityContext();
//,"3","3"
List<String> ss1 = new ArrayList<>(Arrays.asList("1","1","2","2","",""));
VelocityBuilder.Rowtest rs1 = new VelocityBuilder.Rowtest(ss1);
List<String> ss2 = new ArrayList<>(Arrays.asList("4","4","5","5","6","6"));
VelocityBuilder.Rowtest rs2 = new VelocityBuilder.Rowtest(ss2);
List<String> ss3 = new ArrayList<>(Arrays.asList("7","7","8","8","9","9"));
VelocityBuilder.Rowtest rs3 = new VelocityBuilder.Rowtest(ss3);
List<String> ss4 = new ArrayList<>(Arrays.asList("10","10","11","11","12","12"));
VelocityBuilder.Rowtest rs4 = new VelocityBuilder.Rowtest(ss4);
List<VelocityBuilder.Rowtest> rowtests = new ArrayList<>();
rowtests.add(rs1); rowtests.add(rs2); //rowtests.add(rs3); rowtests.add(rs4);
ctx.put("rowtests", rowtests);
StringWriter sw = new StringWriter();
template.merge(ctx, sw);
String ss = sw.toString();
VelocityContext ctx1 = new VelocityContext();
List<String> l = new ArrayList<>(Arrays.asList("1","2","3","4","5","6","7","8","9","10","11","12"));
ctx1.put("formPrintDomain", l);
log.error("生成失败:{}",ss);
Writer html = new StringWriter();
ve.evaluate(ctx1, html, "pdf", ss);
creatHtml(html.toString());
System.out.println("打印一哈html:"+html.toString()+"\n打印结束");
}
}
<html>
<body>
<table border=1 align="center">
\#set($status=true)
\#set($eva=0)
### 模拟的demo看看效果
\#foreach($tee in $formPrintDomain)
#foreach($row_s in $rowtests) ## 1
\#if($status)
<tr>
\#end
#foreach($tr in $row_s.getCol())## 列 3
## 处理为空
#if($tr!='')
\#if($tee==$tr )
<td>当前$tr</td>
\#set($eva=1+$eva)
\#if($eva==$row_s.getCol().size())
\#set($eva=0)
</tr> ##开口
\#end
\#else
\#set($status=false)
\#end
#end
#end
#end
\#end
</table>
</body>
</html>
<html>
<body>
<table border=1 align="center">
<tr>
<td>当前1</td>
<td>当前1</td>
<td>当前2</td>
<td>当前2</td>
<td>当前4</td>
<td>当前4</td>
</tr>
<td>当前5</td>
<td>当前5</td>
<td>当前6</td>
<td>当前6</td>
</table>
</body>
</html>