Spring Thymeleaf 自定义工具类

2017-06-23  本文已影响658人  CoderYan

定义工具类###

package com.xz.ajiaedu.appclient.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者:Yan,Email:yanlun0323@163.com
 * @version 创建时间:2017年6月23日 上午10:32:12
 */
public class TimeConsumingConverter {

    protected static class TimePhaseConverterEntity {
        private String timePhaseDisplay;
        private Integer rangeBegin;
        private Integer rangeEnd;

        public TimePhaseConverterEntity(String timePhaseDisplay) {
            this.timePhaseDisplay = timePhaseDisplay;
        }

        public TimePhaseConverterEntity(String timePhaseDisplay, Integer rangeBegin, Integer rangeEnd) {
            this.timePhaseDisplay = timePhaseDisplay;
            this.rangeBegin = rangeBegin;
            this.rangeEnd = rangeEnd;
        }

        public String getTimePhaseDisplay() {
            return timePhaseDisplay;
        }

        public void setTimePhaseDisplay(String timePhaseDisplay) {
            this.timePhaseDisplay = timePhaseDisplay;
        }

        public Integer getRangeBegin() {
            return rangeBegin;
        }

        public void setRangeBegin(Integer rangeBegin) {
            this.rangeBegin = rangeBegin;
        }

        public Integer getRangeEnd() {
            return rangeEnd;
        }

        public void setRangeEnd(Integer rangeEnd) {
            this.rangeEnd = rangeEnd;
        }
    }

    private static final List<TimePhaseConverterEntity> TIME_PHASE_CONVERTER = new ArrayList<TimePhaseConverterEntity>() {
        /**
         * 
         */
        private static final long serialVersionUID = 5383357078098529456L;

        {
            this.add(new TimePhaseConverterEntity("约5分钟内", 0, 5));
            this.add(new TimePhaseConverterEntity("约10分钟内", 5, 10));
            this.add(new TimePhaseConverterEntity("约15分钟内", 10, 15));
            this.add(new TimePhaseConverterEntity("约20分钟内", 15, 20));
            this.add(new TimePhaseConverterEntity("约25分钟内", 20, 25));
            this.add(new TimePhaseConverterEntity("约30分钟内", 25, 30));
            this.add(new TimePhaseConverterEntity("30分钟以上", 30, Integer.MAX_VALUE));
        }
    };

    /**
     * 时间阶段转换
     * 
     * @param timeConsuming
     *            单位:秒
     * @return
     * @author 作者:Yan,Email:yanlun0323@163.com
     */
    public String converter(Integer timeConsuming) {
        // 区段转换0-5,5-10,10-15,15-20,20-25,25-30,30+
        double minute = Math.ceil(timeConsuming / 60f);
        TimePhaseConverterEntity timePhase = TIME_PHASE_CONVERTER.stream().filter(converter -> {
            return minute > converter.getRangeBegin() && minute <= converter.getRangeEnd();
        }).findFirst().orElse(new TimePhaseConverterEntity("约" + minute + "分钟"));

        return timePhase.getTimePhaseDisplay();
    }
}

package com.xz.ajiaedu.appclient.utils;

import com.alibaba.fastjson.JSON;

/** 
 * @author  作者:Yan,Email:yanlun0323@163.com
 * @version 创建时间:2017年6月23日 上午10:29:37 
 */
public class JSONParser {

    public String toJSONString(Object obj){
        return JSON.toJSONString(obj);
    }
}

定义CustomDialect并实现IExpressionEnhancingDialect接口,添加解析器###

public class CustomDialect extends AbstractDialect implements IExpressionEnhancingDialect {

    public CustomDialect() {
        super();
    }

    /**
     * @see org.thymeleaf.dialect.IDialect#getPrefix
     */
    @Override
    public String getPrefix() {
        return "custom";
    }

    @Override
    public boolean isLenient() {
        return false;
    }

    @Override
    public Map<String, Object> getAdditionalExpressionObjects(IProcessingContext ctx) {
        Map<String, Object> expressions = new HashMap<>();
        expressions.put("JSON", new JSONParser());
        expressions.put("TimeConsuming", new TimeConsumingConverter());
        return expressions;
    }
}

Spring Thymeleaf模板引入方式###

<div th:text="${#JSON.toJSONString(bookshelf)}"></div>
<div th:text="${#TimeConsuming.converter(11)}"></div>

这是基于Thymeleaf+SpringBoot实现的网站http://www.hqast.com/###

上一篇下一篇

猜你喜欢

热点阅读