Velocity的使用(中)

简介: Velocity的使用

二.二.二.二 If 条件语句 开发


   @Test
    public void ifFillTest()throws Exception{
        //1. 创建模板引擎
        VelocityEngine velocityEngine=new VelocityEngine();
        //2. 设置属性
        velocityEngine.setProperty(
                RuntimeConstants.RESOURCE_LOADER,"classpath"
        );
        velocityEngine.setProperty(
                "classpath.resource.loader.class",
                ClasspathResourceLoader.class.getName()
        );
        //3. 进行初始化
        velocityEngine.init();
        //4.获取对应的模板信息
        Template template=velocityEngine.getTemplate("if.vm");
        //5. 构建 VelocityContext 对象,里面放置内容
        Map<String,Object> root=new HashMap<>();
        root.put("age",130);
        root.put("sex","男");
        root.put("score",86);
        VelocityContext velocityContext=new VelocityContext(root);
        //6. 找到StringWriter对象,进行合并
        StringWriter stringWriter=new StringWriter();
        template.merge(velocityContext,stringWriter);
        System.out.println("输出信息:"+stringWriter.toString());
    }


fc1a9209892a713fc97a0f1ceef04206.png


二.二.三 foreach 循环语句 展示


二.二.三.一 foreach 循环语句 vm


创建 foreach.vm 文件


#[[ 放置foreach 循环语句,进行处理 ]]#
## 放置 list或者数组 array
#foreach ($hobby in $hobbys)
    $hobby
#end
#if(${users})
<ul>
    #foreach($user in $users)
        <li>${user.name}</li>
    #end
</ul>
#end
## 放置map语句
#foreach($key in $infoMap.keySet())
    ${key}------>${infoMap.get(${key})}
#end


二.二.三.二 foreach 循环语句 开发


User.java


@Data
public class User {
    private Integer id;
    private String name;
    private Integer sex;
    private Integer age;
    private String description;
}


   @Test
    public void foreachTest() throws Exception{
        //1.创建 VelocityEngine 引擎
        VelocityEngine velocityEngine=new VelocityEngine();
        //2. 设置属性
        velocityEngine.setProperty(
                RuntimeConstants.RESOURCE_LOADER,"classpath"
        );
        velocityEngine.setProperty(
                "classpath.resource.loader.class",
                ClasspathResourceLoader.class.getName()
        );
        //3. 进行初始化
        velocityEngine.init();
        //4.获取模板
        Template template=velocityEngine.getTemplate("foreach.vm");
        //5.进行填充数据
        Map<String,Object> root=new HashMap<>();
        String[] hobbys=new String[4];
        hobbys[0]="A";
        hobbys[1]="B";
        hobbys[2]="C";
        hobbys[3]="D";
        root.put("hobbys",hobbys);
        List<User> userList=getUserList();
        root.put("users",userList);
        Map<String,Object> infoMap=new HashMap<>();
        infoMap.put("name","YJL");
        infoMap.put("age",26);
        infoMap.put("sex","男");
        root.put("infoMap",infoMap);
        //6. 创建 VelocityContext 对象,填充数据。
        VelocityContext velocityContext=new VelocityContext(root);
        //7.
        StringWriter stringWriter=new StringWriter();
        //8. 通过 merge 方法,进行填充数据。
        template.merge(velocityContext,stringWriter);
        System.out.println("输出内容:"+stringWriter.toString());
    }
    private List<User> getUserList() {
        List<User> userList=new ArrayList<>();
        for(int i=1;i<=10;i++){
            User user=new User();
            user.setId(i);
            user.setName("蝴蝶"+i);
            user.setAge(i*3+1);
            user.setSex(i%2);
            user.setDescription("一个简单的描述");
            userList.add(user);
        }
        return userList;
    }


1c6f2806d0008fce6f89630219c45adb.png


二.二.四 macro宏语句 展示


二.二.四.一 macro宏语句 vm


创建 macro.vm 文件


## 这是宏,可以理解成一个函数。
## 声明宏   sayHello 是方法名, $name 为参数
#macro(sayHello $name)
    我们要讲述: $name
#end
## 使用宏
#sayHello("周小欢和岳泽霖的故事") 
## 定义加法
#macro(add $num1,$num2)
#set($total=$num1+$num2)
## 直接使用相加的操作,还是字符串。
   $num1+$num2=${total}
#end
#add(2,3)


二.二.四.二 macro宏语句 开发


  @Test
    public void macroTests() throws Exception{
        //1. 初始化模板引擎
        VelocityEngine ve=new VelocityEngine();
        //2. 设置相应的属性信息
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER,"classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader
                .class.getName());
        //3. 进行初始化
        ve.init();
        //4.获取模板文件
        Template template=ve.getTemplate("macro.vm","utf-8");
        //5.设置变量
        VelocityContext velocityContext=new VelocityContext();
        velocityContext.put("name","岳泽霖");
        //6.进行合并,调用模板的 merge 方法, 将其进行填充。
        StringWriter stringWriter=new StringWriter();
        template.merge(velocityContext, stringWriter);
        System.out.println("输出信息值:"
                +stringWriter.toString());
    }


d59ad14476dda0c0398334fdd6d5e074.png



相关文章
|
6月前
|
测试技术
velocity语法小结
velocity语法小结
43 0
|
Java 索引
模板引擎Velocity
Velocity是一个基于Java的模板引擎,可以通过特定的语法获取在java对象的数据 , 填充到模板中,从而实现界面和java代码的分离 !
|
Java Maven 索引
【模板引擎】velocity
【模板引擎】velocity
229 0
【模板引擎】velocity
|
Java Maven
|
JavaScript
Velocity ${} 、$!{}、!${}的区别
Velocity ${} 、$!{}、!${}的区别
|
数据安全/隐私保护 Java Maven