FreeMarker自定義TemplateDirectiveModel之重複提交&下拉列表

重複提交標籤

public class AvoidRepeatSubmitTag implements TemplateDirectiveModel {
    public AvoidRepeatSubmitTag() {
    }

    public void execute(Environment env, Map map, TemplateModel[] model, TemplateDirectiveBody body) throws TemplateException, IOException {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        String tokenName = UUID.randomUUID().toString();
        String tokenValue = tokenName + "," + UUID.randomUUID().toString();
        WebUtils.setSessionAttribute(request, tokenName, tokenValue);
        request.setAttribute("tokenName", tokenName);
        Object id = map.get("id");
        Writer out = env.getOut();
        if(id != null) {
            out.write("<input type=\'hidden\' id=\'" + id + "\' name =\'token\' value=\'" + tokenValue + "\'/>");
        } else {
            out.write("<input type=\'hidden\' name=\'token\' value=\'" + tokenValue + "\'/>");
        }

        body.render(out);
    }
}

下拉列表

public class ShowDictTemplate implements TemplateDirectiveModel {
    private Map<String, Class> mappingClazz;
    private static final Logger LOGGER = LoggerFactory.getLogger(ShowDictTemplate.class);
    private Map<String, IDictDataLoader> dataLoaderMap;

    public ShowDictTemplate() {
    }

    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        String dictType = MapUtils.getString(map, "dictType");
        String dictKey = MapUtils.getString(map, "dictKey");
        String opt1 = MapUtils.getString(map, "opt1");
        String selectValue = MapUtils.getString(map, "selectValue");
        if(LOGGER.isDebugEnabled()) {
            LOGGER.debug("showDictTemplate, dictType:{},dictKey:{}", dictType, dictKey);
        }

        if(StringUtils.equals(dictType, "EMNU")) {
            String[] list = StringUtils.split(dictKey, "\\.");
            if(list == null || list.length != 2) {
                LOGGER.error("SHOW DICT dictKey format error.key:{}", dictKey);
                return;
            }

            Class builder = (Class)this.mappingClazz.get(list[0]);
            String menuClazzName = builder.getName() + "." + list[1];

            try {
                Class e = ClassUtils.getClass(menuClazzName);
                List list1 = SelectUtils.makeList(e);
                if(StringUtils.isNotEmpty(selectValue)) {
                    list1 = this.filterShowSelectValue(list1, selectValue);
                } else {
                    list1 = this.filterNoShowList(list1);
                }

                if(StringUtils.isNotEmpty(opt1)) {
                    list1 = this.filterOptShowList(list1, opt1);
                }

                DefaultObjectWrapperBuilder builder1 = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_19);
                environment.setVariable("dictList", builder1.build().wrap(list1));
            } catch (ClassNotFoundException var15) {
                var15.printStackTrace();
            }
        } else if(this.dataLoaderMap.get(dictType) != null) {
            List list2 = ((IDictDataLoader)this.dataLoaderMap.get(dictType)).loadDataList(dictKey);
            if(StringUtils.isNotEmpty(selectValue)) {
                list2 = this.filterShowSelectValue(list2, selectValue);
            }

            DefaultObjectWrapperBuilder builder2 = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_19);
            environment.setVariable("dictList", builder2.build().wrap(list2));
        }

        if(templateDirectiveBody != null) {
            templateDirectiveBody.render(environment.getOut());
        }

    }

    private List<LabelValue> filterNoShowList(List<LabelValue> list) {
        ArrayList result = new ArrayList();
        Iterator var3 = list.iterator();

        while(var3.hasNext()) {
            LabelValue lv = (LabelValue)var3.next();
            if(lv.isListFlag()) {
                result.add(lv);
            }
        }

        return result;
    }

    private List<LabelValue> filterOptShowList(List<LabelValue> list, String opt) {
        return (List)list.stream().filter((lv) -> {
            return opt.equals(lv.getOpt1());
        }).collect(Collectors.toList());
    }

    private List<LabelValue> filterShowSelectValue(List<LabelValue> list, String value) {
        ArrayList result = new ArrayList();
        Iterator var4 = list.iterator();

        while(var4.hasNext()) {
            LabelValue lv = (LabelValue)var4.next();
            if(StringUtils.equalsIgnoreCase(value, lv.getValue())) {
                result.add(lv);
            }
        }

        return result;
    }

    public void setDataLoaderMap(Map<String, IDictDataLoader> dataLoaderMap) {
        this.dataLoaderMap = dataLoaderMap;
    }

    public void setMappingClazz(Map<String, Class> mappingClazz) {
        this.mappingClazz = mappingClazz;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章