Velocity JAVA引擎模板 (七) null处理

x33g5p2x  于2021-12-24 转载在 其他  
字(1.0k)|赞(0)|评价(0)|浏览(327)

本文是【null处理】demo

/** * null处理 */
    @Test
    public void test7() throws IOException {

        //1 加载配置
        Properties properties = new Properties();
        properties.load(Test.class.getClassLoader().getResourceAsStream("velocity.properties"));
        Velocity.init(properties);

        //2 创建上下文对象
        VelocityContext context = new VelocityContext();

        //2 将数据对象添加到此上下文中
        context.put("title", "");
        context.put("name", null);

        //4选择模板
        Template template = Velocity.getTemplate("./src/test/resources/template/null.vm");

        //5 合并模板和数据并输出
        StringWriter sw = new StringWriter();
        template.merge(context, sw);
        sw.flush();
        log.info("-----------------------------start------------------------------------");
        log.info(sw.toString());
        log.info("-----------------------------end------------------------------------");
    }

模板 null.vm

$title
-------------------华丽的分隔线-----------------------
$name
-------------------华丽的分隔线-----------------------
$!title
-------------------华丽的分隔线-----------------------
$!name

相关文章