velocity 生成表格动态排列,可以拷贝直接使用,内容分别为:java ,vm ,和生成的html

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>










 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值