FreeMarker模板使用枚举类使if条件判断更灵活

一个简单的后台商品展示列表demo:

在使用模板引擎时,对于“类目”条件判断的字段,通常情况是写死在页面的:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>商品管理</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
<div id="wrapper" class="toggled">

    <#--主要内容    -->
    <div id="page-content-wrapper">
        <div class="container-fluid">
            <div class="row clearfix">
                <div class="col-md-12 column">
                    <table class="table table-bordered table-hover">
                        <thead>
                        <tr>
                            <th>商品id</th>
                            <th>名称</th>
                            <th>类目</th>
                            <th>价格</th>
                        </tr>
                        </thead>
                        <tbody>
                        <#list productPage.content as product>
                        <tr>
                            <td>${product.productId}</td>
                            <td>${product.productName}</td>
                            <td>
                                <#if product.productType == 0>
                                    女生最爱
                                <#elseif product.productType == 1>
                                    男生最爱
                                <#elseif product.productType == 2>
                                    小孩最爱
                                </#if>
                            </td>
                            <td>${product.productPrice}</td>

                        </tr>

                        </#list>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>
<script src="https://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>

但使用到“类目”的页面一旦多起来,这种方式修改起来太不灵活了,所以,可以写一个枚举工具类,让代码更灵活,先写一个枚举接口:

public interface CodeEnum {
    Integer getCode();
}

枚举类工具:

public class EnumUtil {
    public static <T extends CodeEnum>T getByCode(Integer code, Class<T> enumClass){
        // 遍历枚举类的值,返回与code匹配的实例
        for (T each : enumClass.getEnumConstants()) {
            if (each.getCode().equals(code)) {
                return each;
            }
        }
        return null;
    }

}

枚举类:

public enum ProductTypeEnum implements CodeEnum {
    GIRLS_LOVE(0, "女生最爱"),
    BOYS_LOVE(1, "男生最爱"),
    KIDS_LOVE(2, "小孩最爱")
    ;

    private Integer code;
    private String message;

    @Override
    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    ProductTypeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}

在实体类中,添加获得枚举实例的方法:

@Entity
@DynamicUpdate
@Data
public class Product {

    //  商品id
    @Id
    private String productId;

    /*商品名称*/
    private String productName;

    /*类目编号*/
    private Integer productType;

    /*单价*/
    private BigDecimal productPrice;

    /*获取商品类目*/
    @JsonIgnore
    public ProductTypeEnum getProductTypeEnum() {
        return EnumUtil.getByCode(productType, ProductTypeEnum.class);
    }
}

然后,将模板页面改成:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>商品管理</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
<div id="wrapper" class="toggled">

    <#--主要内容    -->
    <div id="page-content-wrapper">
        <div class="container-fluid">
            <div class="row clearfix">
                <div class="col-md-12 column">
                    <table class="table table-bordered table-hover">
                        <thead>
                        <tr>
                            <th>商品id</th>
                            <th>名称</th>
                            <th>类目</th>
                            <th>价格</th>
                        </tr>
                        </thead>
                        <tbody>
                        <#list productPage.content as product>
                        <tr>
                            <td>${product.productId}</td>
                            <td>${product.productName}</td>
                  <td>${product.getProductTypeEnum().message}</td>
<td>${product.productPrice}</td> </tr> </#list> </tbody> </table> </div> </div> </div> </div> </div> <script src="https://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> </body> </html>

这样,代码改动更加灵活了。

原文地址:https://www.cnblogs.com/libera11/p/8621077.html