Freemarker条件判断

判断语法

<#if target??>
    xxxx
#if>

比如
判断索引,是否为偶数

<#if student_index % 2 == 0>
    xxxx
<#else>
    xxxx
#if>

数据集

//6.创建一个数据集,可以是pojo也可以是map,推荐使用map
Map data = new HashMap<>();
data.put("hello", "hello freemarker");
Student student = new Student(1, "小米", 11, "北京昌平回龙观");
data.put("student", student);
List<Student> stuList = new ArrayList<>();
stuList.add(new Student(1, "小米", 11, "北京昌平回龙观"));
stuList.add(new Student(2, "小米2", 12, "北京昌平回龙观"));
stuList.add(new Student(3, "小米3", 13, "北京昌平回龙观"));
stuList.add(new Student(4, "小米4", 14, "北京昌平回龙观"));
stuList.add(new Student(5, "小米5", 15, "北京昌平回龙观"));
stuList.add(new Student(6, "小米6", 16, "北京昌平回龙观"));
stuList.add(new Student(7, "小米7", 17, "北京昌平回龙观"));
data.put("stuList", stuList);

模板

<html>
<head>
    <title>测试页面title>
head>
<body>
    学生列表:<br>
    <table border="1">
        <tr>
            <th>序号th>
            <th>学号th>
            <th>姓名th>
            <th>年龄th>
            <th>家庭住址th>
        tr>
        <#list stuList as stu>
        <#if stu_index%2==0>
        <tr bgcolor="red">
        <#else>
        <tr bgcolor="blue">
        #if>
            <td>${stu_index}td>
            <td>${stu.id}td>
            <td>${stu.name}td>
            <td>${stu.age}td>
            <td>${stu.address}td>
        tr>
        #list>
    table>
    <br>
    使用if判断null值:
    <#if val??>
    val是有值的
    <#else>
    val值为null
    #if>
body>
html>

你可能感兴趣的:(————Freemarker)