Springboot 集成Beetl模板

依赖:

<!--beetl模板引擎-->
<dependency>
   <groupId>com.ibeetl</groupId>
   <artifactId>beetl</artifactId>
   <version>2.9.3</version>
</dependency>

两种方式:

方式一:

代码属性实现 

1、 配置 beetl需要的BeetlGroupUtilConfiguration和 BeetlSpringViewResolver

package com.chy.beetl.config;

import com.chy.beetl.config.properties.BeetlConfiguration;
import com.chy.beetl.config.properties.BeetlProperties;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * beetl配置类
 * */
@Configuration
public class BeetlConf {

    @Autowired
    private BeetlProperties beetlProperties;

    /**
     * 代码实现配置
     */
    @Bean(initMethod = "init",name = "beetlConfig")
    public BeetlConfiguration getBeetlConfiguration() {
        BeetlConfiguration beetlConfiguration = new BeetlConfiguration();
        //获取Spring Boot 的ClassLoader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if(loader==null){
            loader = BeetlConf.class.getClassLoader();
        }
        // 设置配置的属性(类方法中设置的属性)
        beetlConfiguration.setConfigProperties(beetlProperties.getProperties());

        ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
                beetlProperties.getPrefix());
        beetlConfiguration.setResourceLoader(cploder);

        return beetlConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        // 解析后缀为.html页面
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
}

2、BeetlConfiguration 继续BeetlGroupUtilConfiguration:

package com.chy.beetl.config.properties;

import com.chy.beetl.func.CurrentTimeUtil;
import com.chy.beetl.func.PrintTime;
import com.chy.beetl.func.SysDicSelectTag;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;

/**
 * beetl拓展配置,绑定一些工具类,方便在模板中直接调用
 *
 */
public class BeetlConfiguration extends BeetlGroupUtilConfiguration {

    public BeetlConfiguration(){}

    // beetl自定义方法注册
    @Override
    protected void initOther() {
        groupTemplate.registerFunction("printTime", new PrintTime());
        groupTemplate.registerFunctionPackage("current", new CurrentTimeUtil());
        groupTemplate.registerTag("dic_select", SysDicSelectTag.class);
    }

}

3、BeetlProperties.java(属性类) 

package com.chy.beetl.config.properties;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * beetl配置(如果需要配置别的配置可参照这个形式自己添加)
 *
 */
@Data
public class BeetlProperties {

    public static final String BEETLCONF_PREFIX = "beetl";

    /**
     *  ***.yml配置文件需要有如下配置:
     *  beetl:
     *    templatesPath: templates # 页面的根路径文件夹
     * */
    //@Value("${beetl.templatesPath}")
    //private String prefix;//模板根目录 ,比如 "templates"

    /**
     *  ***.yml配置文件需要有如下配置:
     *  spring:
     *   mvc:
     *     view:
     *       prefix: /templates # 页面的根路径文件夹
     * */
    @Value("${spring.mvc.view.prefix}")
    private String prefix;

    // 定界符开始符号
    private String delimiterStatementStart="@";
    // 定界符结束符号 - 结束符留空,或者=null 表示 是以回车作为结尾
    private String delimiterStatementEnd;
    // 文件Root目录
    private String resourceTagroot;
    // 文件后缀
    private String resourceTagsuffix;

    private String resourceAutoCheck;
    // 自定义标签定界符,默认<#></#>
    private String htmlTagFlag="tag:";

    // 属性赋值
    public Properties getProperties() {
        Properties properties = new Properties();
        if (ToolUtil.isNotEmpty(delimiterStatementStart)) {
            if (delimiterStatementStart.startsWith("\\")) {
                delimiterStatementStart = delimiterStatementStart.substring(1);
            }
            properties.setProperty("DELIMITER_STATEMENT_START", delimiterStatementStart);
        }
        if (ToolUtil.isNotEmpty(delimiterStatementEnd)) {
            properties.setProperty("DELIMITER_STATEMENT_END", delimiterStatementEnd);
        } else {
            properties.setProperty("DELIMITER_STATEMENT_END", "null");
        }
        if (ToolUtil.isNotEmpty(resourceTagroot)) {
            properties.setProperty("RESOURCE.tagRoot", resourceTagroot);
        }
        if (ToolUtil.isNotEmpty(resourceTagsuffix)) {
            properties.setProperty("RESOURCE.tagSuffix", resourceTagsuffix);
        }
        if (ToolUtil.isNotEmpty(resourceAutoCheck)) {
            properties.setProperty("RESOURCE.autoCheck", resourceAutoCheck);
        }
        if (ToolUtil.isNotEmpty(htmlTagFlag)) {
            properties.setProperty("HTML_TAG_FLAG", htmlTagFlag);
        }
        return properties;
    }

}
/**
 * 判断对象为空工具
 * */
class ToolUtil{

    /**
     * 对象是否不为空(新增)
     *
     */
    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    /**
     * 对象是否为空
     *
     * @author fengshuonan
     * @Date 2018/3/18 21:57
     */
    private static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            if (o.toString().trim().equals("")) {
                return true;
            }
        } else if (o instanceof List) {
            if (((List) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Map) {
            if (((Map) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Set) {
            if (((Set) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Object[]) {
            if (((Object[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof int[]) {
            if (((int[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof long[]) {
            if (((long[]) o).length == 0) {
                return true;
            }
        }
        return false;
    }

}

4、实现BeetlProperties 

package com.chy.beetl.config;

import com.chy.beetl.config.properties.BeetlProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 项目中的配置
 */
@Configuration
public class WebBeanConfig {

    /**
     * beetl模板的配置
     *
     */
    @Bean
    @ConfigurationProperties(prefix = BeetlProperties.BEETLCONF_PREFIX)
    public BeetlProperties beetlProperties() {
        return new BeetlProperties();
    }

}

方式二:

静态资源属性实现

1、配置 beetl需要的BeetlGroupUtilConfiguration和 BeetlSpringViewResolver

package com.chy.beetl.config;

import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;


/**
 * beetl配置类(***.properties)
 * */
//@Configuration
public class BeetlStaticConf {

    /**
     *  ***.yml配置文件需要有如下配置:
     *  beetl:
     *    templatesPath: templates # 页面的根路径文件夹
     * */
    //@Value("${beetl.templatesPath}")
    //private String prefix;//模板根目录 ,比如 "templates"

    /**
     *  ***.yml配置文件需要有如下配置:
     *  spring:
     *   mvc:
     *     view:
     *       prefix: /templates # 页面的根路径文件夹
     * */
    @Value("${spring.mvc.view.prefix}")
    private String prefix;

    @Bean(initMethod = "init",name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        //获取Spring Boot 的ClassLoader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if(loader==null){
            loader = BeetlConf.class.getClassLoader();
        }

        // 读取配置文件信息
        ResourcePatternResolver patternResolver = ResourcePatternUtils
                .getResourcePatternResolver(new DefaultResourceLoader());

        beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl/beetl.properties"));

        ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
                prefix);
        beetlGroupUtilConfiguration.setResourceLoader(cploder);
        beetlGroupUtilConfiguration.init();
        //如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
        beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);

        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        // 解析后缀为.html页面
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
}

2、 beetl.properties配置(resources文件夹下的beetl文件夹)

package com.chy.beetl.config;

import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;


/**
 * beetl配置类(***.properties)
 * */
//@Configuration
public class BeetlStaticConf {

    /**
     *  ***.yml配置文件需要有如下配置:
     *  beetl:
     *    templatesPath: templates # 页面的根路径文件夹
     * */
    //@Value("${beetl.templatesPath}")
    //private String prefix;//模板根目录 ,比如 "templates"

    /**
     *  ***.yml配置文件需要有如下配置:
     *  spring:
     *   mvc:
     *     view:
     *       prefix: /templates # 页面的根路径文件夹
     * */
    @Value("${spring.mvc.view.prefix}")
    private String prefix;

    @Bean(initMethod = "init",name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        //获取Spring Boot 的ClassLoader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if(loader==null){
            loader = BeetlConf.class.getClassLoader();
        }

        // 读取配置文件信息
        ResourcePatternResolver patternResolver = ResourcePatternUtils
                .getResourcePatternResolver(new DefaultResourceLoader());

        beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl/beetl.properties"));

        ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
                prefix);
        beetlGroupUtilConfiguration.setResourceLoader(cploder);
        beetlGroupUtilConfiguration.init();
        //如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
        beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);

        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        // 解析后缀为.html页面
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
}

后台代码:

package com.chy.beetl.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class IndexController {
    /**
     * 方式一
     * */
    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        // 日期
        try {
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = format.parse("2023-10-10 10:01:01");
            request.setAttribute("createTime",date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        //标题
        request.setAttribute("title","标题");
        // 内容
        request.setAttribute("content","内容");
        // 输出安全值
        request.setAttribute("secure",null);
        // 数字
        request.setAttribute("num",3);
        // 普通数组
        request.setAttribute("datas",getList());
        // map数组
        request.setAttribute("maps",getMapList());

        return "index";
    }

    /**
     * 方式二
     * */
    @RequestMapping("/model")
    public String index(Model model){
        model.addAttribute("title","model");
        model.addAttribute("content","model内容");
        model.addAttribute("datas",getList());
        model.addAttribute("maps",getMapList());

        return "model";
    }

    /**
     * 方式三
     * */
    @RequestMapping("/modelView")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.addObject("title","modelViewTitle");
        modelAndView.addObject("content","modelView内容");
        modelAndView.addObject("datas",getList());
        modelAndView.addObject("maps",getMapList());
        // 指定访问的html页面名称
        modelAndView.setViewName("modelView");

        return modelAndView;
    }


    /**
     * 简单数组
     * */
    private List<String> getList(){
        List<String> list = new ArrayList<>();
        list.add("绿园区");
        list.add("朝阳区");
        list.add("南关区");
        list.add("宽城区");
        list.add("二道区");
        list.add("经开区");

        return list;
    }

    /**
     * 简单数组
     * */
    private List<HashMap<String,Object>> getMapList(){
        List<HashMap<String,Object>> list = new ArrayList<>();

        HashMap<String,Object> map = new HashMap<>();
        map.put("username","张三");
        map.put("age","23");
        map.put("like","足球,篮球,乒乓球");

        list.add(map);

        return list;

    }

}

自定义类、方法和标签 

CurrentTimeUtil.java

package com.chy.beetl.func;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 需要在静态资源或者类中注册
 * 定义beetl使用的标签
 * */
public class CurrentTimeUtil {

    public CurrentTimeUtil(){}
    /**
     * @return yyyy-MM-dd HH:mm:ss
     * */
    public static String CurrentTime(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date date = new Date();
        String fmt = format.format(date);

        return fmt;
    }

}

NowTimeUtil.java

package com.chy.beetl.func;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 不需要注册
 * 定义beetl可以使用的类和方法
 * */
public class NowTimeUtil {

    public static String NowTime(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String fmt = format.format(date);

        return fmt;
    }

}

PrintTime.java 

package com.chy.beetl.func;

import org.beetl.core.Context;
import org.beetl.core.Function;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 需要在静态资源或者类中注册
 * 定义beetl使用的类和方法
 * */
public class PrintTime implements Function {

    /**
     * @param  paras 模板传入的参数
     * @param  ctx 模板上下文
     * */
    @Override
    public Object call(Object[] paras, Context ctx) {
        String result = null;
        // paras[0] 模板传递的日期
        Date date = (Date) paras[0];
        Long fiveM = date.getTime() + (5*60*1000);// 过了五分钟
        Long thrityM = date.getTime() + (30*60*1000);// 过了三十分钟
        Long oneH = date.getTime() + (60*60*1000);// 过了一个小时

        // 获取当前时间
        Date currentTime = new Date();

        if (currentTime.getTime() <= fiveM){
            result = "刚刚";
        }else if (currentTime.getTime() <= thrityM){
            result = "半小时前";
        }else if (currentTime.getTime() <= oneH){
            result  = "一个小时前";
        }else {
            // paras[1] 模板传递的日期格式
            SimpleDateFormat sdf = new SimpleDateFormat(paras[1].toString());
            result = sdf.format(date);
        }

        return result;
    }
}

标签SysDicSelectTag.java

package com.chy.beetl.func;

import lombok.Data;
import org.beetl.core.GeneralVarTagBinding;

import java.io.IOException;
import java.util.*;

/**
 * 需要在静态资源或者类中注册
 * 定义beetl使用的标签
 * */
@Data
public class SysDicSelectTag extends GeneralVarTagBinding {

    public String id;
    public String name;
    public String dicCode;
    public String type;
    public String skin;
    public String onClick;
    public String onChange;
    public String headName;
    public String headValue;
    public String headType;
    public String defaultValue;
    public String disabled;
    public String layVerify;
    public String workFlowForm;
    public String itemName;
    public String exclude;

    public void initAttr(){
        Map<String, Object> attrs = this.getAttributes();
        if(attrs.size()>0){
            if(StringUtils.checkValNotNull(attrs.get("id"))){
                this.setId(attrs.get("id").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("name"))){
                this.setName(attrs.get("name").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("dicCode"))){
                this.setDicCode(attrs.get("dicCode").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("type"))){
                this.setType(attrs.get("type").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("skin"))){
                this.setSkin(attrs.get("skin").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("onClick"))){
                this.setOnClick(attrs.get("onClick").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("onChange"))){
                this.setOnChange(attrs.get("onChange").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("headName"))){
                this.setHeadName(attrs.get("headName").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("headValue"))){
                this.setHeadValue(attrs.get("headValue").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("headType"))){
                this.setHeadType(attrs.get("headType").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("defaultValue"))){
                this.setDefaultValue(attrs.get("defaultValue").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("disabled"))){
                this.setDisabled(attrs.get("disabled").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("layVerify"))){
                this.setLayVerify(attrs.get("layVerify").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("workFlowForm"))){
                this.setWorkFlowForm(attrs.get("workFlowForm").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("itemName"))){
                this.setItemName(attrs.get("itemName").toString());
            }
            if(StringUtils.checkValNotNull(attrs.get("exclude"))){
                this.setExclude(attrs.get("exclude").toString());
            }
        }
    }


    @Override
    public void render() {
        initAttr();

        StringBuilder sb = new StringBuilder();
        if (StringUtils.checkValNotNull(dicCode)) {
            sb.append("<select name='" + this.getName() + "' id='" + this.getId() + "' ");

            if (this.getOnChange() != null) {
                sb.append(" lay-filter='").append(this.getOnChange()).append("' ");
            }else {
                sb.append(" lay-filter='").append(this.getName()).append("' ");
            }


            if (layVerify != null) {
                sb.append(" lay-verify='").append(layVerify == null ? "" : layVerify).append("' ");
            }
            if (workFlowForm != null) {
                sb.append(" workFlowForm='").append(workFlowForm == null ? "" : workFlowForm).append("' ");
            }
            if (itemName != null) {
                sb.append(" itemName='").append(itemName == null ? "" : itemName).append("' ");
            }

            sb.append(" >");

            if (headName != null) {
                sb.append("<option value='").append(headValue == null ? "" : headValue).append("' selected>")
                        .append(headName).append("</option>");
            }

            if (headType != null) {
                if ("1".equals(headType)) {
                    sb.append("<option value='").append(headValue == null ? "" : headValue).append("' selected>")
                            .append(" - 全部 - ").append("</option>");
                }
                if ("2".equals(headType)) {
                    sb.append("<option value='").append(headValue == null ? "" : headValue).append("' selected>")
                            .append(" - 请选择 - ").append("</option>");
                }
                if ("3".equals(headType)) {
                    sb.append("<option value='").append(headValue == null ? "" : headValue).append("' selected>")
                            .append(" - 无 - ").append("</option>");
                }
            }

            List<Map<String,Object>> dictList = ArrayUtils.getSelList();

            if (dictList != null && dictList.size()>0) {
                Collections.sort(dictList, new Comparator<Map<String, Object>>() {
                    @Override
                    public int compare(Map<String, Object> t0, Map<String, Object> t1) {
                        Object t00 = t0.get("sort");
                        int it0=0;
                        if(t00!=null){
                            if(t00 instanceof Integer){
                                it0 = (Integer)t00;
                            } else if(t00 instanceof String){
                                it0 = Integer.valueOf(t00.toString().trim());
                            }
                        }
                        Object t01 = t1.get("sort");
                        int it1=0;
                        if(t01!=null){
                            if(t01 instanceof Integer){
                                it1 = (Integer)t01;
                            } else if(t01 instanceof String){
                                it1 = Integer.valueOf(t01.toString().trim());
                            }
                        }
                        return it0 - it1;
                    }
                });
                String paramValue = this.getDefaultValue();
                int index = 0;
                String exclude = this.getExclude();
                if (exclude == null) {
                    exclude = "";
                }
                String code;
                String name;
                for (Map<String,Object> option : dictList) {
                    code = option.get("code").toString();
                    name = option.get("name").toString();
                    if (exclude.indexOf(code) >= 0) {
                        continue;
                    }
                    sb.append("<option value='").append(code).append("'");
                    if (paramValue != null) {
                        if (paramValue.equals(code)) {
                            sb.append(" selected ");
                        }
                    } else {
                        if (index == 0 && headName == null && headType == null) {
                            sb.append(" selected ");
                        }
                    }
                    sb.append(">").append(name).append("</option>");
                    index++;
                }


            }
        }
        sb.append("</select>");
        try {
            this.ctx.byteWriter.writeString(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

/**
 * 集合数组
 * */
class ArrayUtils {
    public static List<Map<String,Object>> getSelList(){
        List<Map<String,Object>> mapList = new ArrayList<>();

        HashMap<String,Object> map1 = new HashMap<>();
        map1.put("dictId","1");
        map1.put("dictTypeId","1");
        map1.put("sort","1");
        map1.put("code","vue");
        map1.put("name","VUE");
        mapList.add(map1);

        HashMap<String,Object> map2 = new HashMap<>();
        map2.put("dictId","2");
        map2.put("dictTypeId","2");
        map2.put("sort","2");
        map2.put("code","java");
        map2.put("name","JAVA");
        mapList.add(map2);

        HashMap<String,Object> map3 = new HashMap<>();
        map3.put("dictId","3");
        map3.put("dictTypeId","3");
        map3.put("sort","3");
        map3.put("code","php");
        map3.put("name","PHP");
        mapList.add(map3);

        return mapList;
    }
}

/**
 * 判断字符串是否为空
 * */
class StringUtils{
    /**
     * 判断对象是否为空
     *
     * @param object ignore
     * @return ignore
     */
    public static boolean checkValNotNull(Object object) {
        if (object instanceof CharSequence) {
            return isNotBlank((CharSequence) object);
        }
        return object != null;
    }

    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    public static boolean isBlank(final CharSequence cs) {
        if (cs == null) {
            return true;
        }
        int l = cs.length();
        if (l > 0) {
            for (int i = 0; i < l; i++) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }
        }
        return true;
    }
}

前端代码:

  index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>${title}</title>
</head>
<body>
  <!--beetl自定义类和方法-->
  <div>
    <span>(自定义)发布时间:${printTime(createTime!,"yyyy-MM-dd HH:mm:ss")}</span><br/>
    <span>(自定义)当前时间:${@com.chy.beetl.func.NowTimeUtil.NowTime()}</span><br/>
    <span>(自定义)当前时间:${current.CurrentTime()}</span><br/>
    <div>
      <tag:dic_select id="postType" name="postType" type="select" dicCode="postType" headName="-请选择岗位性质-" ></tag:dic_select>
    </div>
  </div>


  <!--beetl模板layout布局-->
    @layout("/header.html",{content:"页面头"}){
      <div>页面布局111</div>
    @}

  <!-- beetl模板引入其他页面作为一部分 -->
  @include("/part.html",{part_content:"include引入的页面","part_one":"N","part_two":"Y"}){}

  <!--beetl模板赋值-->
  <h2>${content}</h2>

  <!--
    beetl模板安全输出
    变量后面加一个感叹号标识安全输出,占位符,定界符都可以使用,感叹号后面可以写默认值
    -->
  <h3>安全输出</h3>
  <div>${secure!"测试"}</div>

  <!--
    #beetl模板循环(简单数组)
    #因为重新设置了定位符
    # 默认定位符:<% * %>,现在定位符:@
    -->
  <h3>简单循环</h3>
  @for(item in datas){
  <span>${item}&nbsp;</span>
  @}

  <!--beetl模板循环(HashMap数组)-->
  <h3>HashMap数组循环</h3>
  @for(idx in maps){
  <div>姓名:${idx.username}</div>
  <div>年龄:${idx.age}</div>
  <div>
    喜好:${idx.like}
  </div>
  @}


  <!--beetl模板内置函数与if else-->
  <!--<h3>内置函数与if else</h3>
  <%
    if(isNotEmpty(num) && num >= 5){
  %>
  <div>num:${num}</div>
  <% } %>
  <%else{%>
  <div>num:为空或者小于5</div>
  <% } %>-->
  <h3>内置函数与if else</h3>
    @if(isNotEmpty(num) && num >= 5){
    <div>num:${num}</div>
    @}
    @else{
    <div>num:为空或者小于5</div>
    @}




  </body>
  </html>

header.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Header</title>
</head>
<body>

    <div style="height: 200px;width: 100%;background: aqua">
        <h1>${content!}</h1>
    </div>

</body>
</html>

 part.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Header</title>
</head>
<body>

    <table style="height: 200px;width: 100%;background: brown">
        <tr>
            <h3>${part_content}</h3>
        </tr>
        <!--默认显示该行,不需要显示时,需要引入时设置part_one为"N" - 例如:index.html-->
        @if(part_one! != "N"){
        <tr>
            <td></td>
            <td></td>
            <td>
                <div>第一个part</div>
            </td>
        </tr>
        @}
        <!--默认不显示该行,需要引入的时设置part_two为"Y" - 例如:index.html-->
        @if(part_two! == "Y"){
        <tr>
            <td></td>
            <td></td>
            <td>
                <div>第二个part</div>
            </td>
        </tr>
        @}
    </table>

</body>
</html>

 model.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>${title}</title>
</head>
<body>

    <h1>hello,world!</h1> <br/>
    <!--beetl模板取值-->
    <h2>${content}</h2>
    <!--beetl模板循环(简单数组)-->
    <%
    for(item in datas){
    %>
    <div>${item}</div><br/>
    <% } %>

    <!--beetl模板循环(HashMap数组)-->
    <%
    for(idx in maps){
    %>
    <div>姓名:${idx.username}</div>
    <div>年龄:${idx.age}</div>
    <div>
        喜好:${idx.like}
    </div>
    <% } %>

</body>
</html>

modelView.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>${title}</title>
</head>
<body>

    <h1>hello,world!</h1> <br/>
    <!--beetl模板取值-->
    <h2>${content}</h2>
    <!--beetl模板循环(简单数组)-->
    <%
    for(item in datas){
    %>
    <div>${item}</div><br/>
    <% } %>

    <!--beetl模板循环(HashMap数组)-->
    <%
    for(idx in maps){
    %>
    <div>姓名:${idx.username}</div>
    <div>年龄:${idx.age}</div>
    <div>
        喜好:${idx.like}
    </div>
    <% } %>


</body>
</html>

网页效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以使用以下步骤来生成SpringBoot+Beetl的代码模板: 1. 首先,在你的SpringBoot项目中添加Beetl的依赖,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.beetl</groupId> <artifactId>beetl-framework-starter</artifactId> <version>2.8.1.RELEASE</version> </dependency> ``` 2. 创建一个Beetl模板文件,命名为entity.btl,内容如下: ```java package ${packageName}.entity; import lombok.Data; import javax.persistence.*; import java.util.Date; @Data @Entity @Table(name = "${tableName}") public class ${className} { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; <#list columns as column> /** * ${column.columnComment} */ private ${column.columnType} ${column.columnName}; </#list> private Date createTime; private Date updateTime; } ``` 3. 在同级目录下创建generator.btl,用于生成Controller、Service和Mapper的代码,内容如下: ```java package ${packageName}; import org.beetl.core.Template; import org.beetl.sql.core.NameConversion; import org.beetl.sql.core.db.TableDesc; import org.beetl.sql.core.kit.StringUtils; import org.beetl.sql.core.mapper.BaseMapper; import org.beetl.sql.core.mapper.MapperInvoke; import org.beetl.sql.core.mapper.builder.MapperBuilder; import org.beetl.sql.core.mapper.builder.MapperConfig; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; public class ${className}Generator { private static final String CONTROLLER_TEMPLATE = "package ${packageName}.controller;\n" + "\n" + "import ${packageName}.entity.${className};\n" + "import ${packageName}.service.${className}Service;\n" + "import lombok.RequiredArgsConstructor;\n" + "import org.springframework.http.HttpStatus;\n" + "import org.springframework.http.ResponseEntity;\n" + "import org.springframework.web.bind.annotation.*;\n" + "\n" + "import java.util.List;\n" + "\n" + "@RestController\n" + "@RequestMapping(\"/${tableName}\")\n" + "@RequiredArgsConstructor\n" + "public class ${className}Controller {\n" + "\n" + " private final ${className}Service ${classNameVar}Service;\n" + "\n" + " @GetMapping\n" + " public ResponseEntity<List<${className}>> getAll${className}() {\n" + " return ResponseEntity.ok(${classNameVar}Service.getAll());\n" + " }\n" + "\n" + " @GetMapping(\"/{id}\")\n" + " public ResponseEntity<${className}> get${className}ById(@PathVariable Long id) {\n" + " return ResponseEntity.ok(${classNameVar}Service.getById(id));\n" + " }\n" + "\n" + " @PostMapping\n" + " public ResponseEntity<${className}> create${className}(@RequestBody ${className} ${classNameVar}) {\n" + " return ResponseEntity.status(HttpStatus.CREATED).body(${classNameVar}Service.create(${classNameVar}));\n" + " }\n" + "\n" + " @PutMapping(\"/{id}\")\n" + " public ResponseEntity<${className}> update${className}(@PathVariable Long id, @RequestBody ${className} ${classNameVar}) {\n" + " return ResponseEntity.ok(${classNameVar}Service.update(id, ${classNameVar}));\n" + " }\n" + "\n" + " @DeleteMapping(\"/{id}\")\n" + " public ResponseEntity<Void> delete${className}(@PathVariable Long id) {\n" + " ${classNameVar}Service.deleteById(id);\n" + " return ResponseEntity.noContent().build();\n" + " }\n" + "}"; private static final String SERVICE_TEMPLATE = "package ${packageName}.service;\n" + "\n" + "import ${packageName}.entity.${className};\n" + "import ${packageName}.mapper.${className}Mapper;\n" + "import lombok.RequiredArgsConstructor;\n" + "import org.springframework.stereotype.Service;\n" + "\n" + "import java.util.List;\n" + "\n" + "@Service\n" + "@RequiredArgsConstructor\n" + "public class ${className}Service {\n" + "\n" + " private final ${className}Mapper ${classNameVar}Mapper;\n" + "\n" + " public List<${className}> getAll() {\n" + " return ${classNameVar}Mapper.all();\n" + " }\n" + "\n" + " public ${className} getById(Long id) {\n" + " return ${classNameVar}Mapper.single(id);\n" + " }\n" + "\n" + " public ${className} create(${className} ${classNameVar}) {\n" + " ${classNameVar}.setId(null);\n" + " ${classNameVar}Mapper.insert(${classNameVar});\n" + " return ${classNameVar};\n" + " }\n" + "\n" + " public ${className} update(Long id, ${className} ${classNameVar}) {\n" + " ${classNameVar}.setId(id);\n" + " ${classNameVar}Mapper.updateTemplateById(${classNameVar});\n" + " return ${classNameVar};\n" + " }\n" + "\n" + " public void deleteById(Long id) {\n" + " ${classNameVar}Mapper.deleteById(id);\n" + " }\n" + "}"; private static final String MAPPER_TEMPLATE = "package ${packageName}.mapper;\n" + "\n" + "import ${packageName}.entity.${className};\n" + "import org.beetl.sql.core.mapper.BaseMapper;\n" + "import org.beetl.sql.core.mapper.MapperTemplate;\n" + "import org.springframework.stereotype.Repository;\n" + "\n" + "@Repository\n" + "public interface ${className}Mapper extends BaseMapper<${className}> {\n" + "\n" + "}"; public static void main(String[] args) throws Exception { String packageName = "com.example.demo"; String tablePrefix = "t_"; String entityName = "${className}"; String classNameVar = StringUtils.toLowerCaseFirstOne(entityName); String entityPackage = packageName + ".entity"; String servicePackage = packageName + ".service"; String controllerPackage = packageName + ".controller"; String mapperPackage = packageName + ".mapper"; // 获取所有的表名 List<String> tableNames = new ArrayList<>(); tableNames.add("user"); tableNames.add("role"); for (String tableName : tableNames) { // 获取表的元数据信息 TableDesc tableDesc = MapperBuilder.INSTANCE.getMapperConfig().getDbConfig().getTableDesc(tableName); // 将下划线转换为驼峰命名 String className = NameConversion.underlineToCamel(tableDesc.getName(), true); String fileName = className + ".java"; // 获取模板 Template controllerTemplate = MapperBuilder.INSTANCE.getEngine().getTemplate(CONTROLLER_TEMPLATE); Template serviceTemplate = MapperBuilder.INSTANCE.getEngine().getTemplate(SERVICE_TEMPLATE); Template mapperTemplate = MapperBuilder.INSTANCE.getEngine().getTemplate(MAPPER_TEMPLATE); // 设置模板参数 controllerTemplate.binding("packageName", controllerPackage); controllerTemplate.binding("tableName", tableName); controllerTemplate.binding("className", className); controllerTemplate.binding("classNameVar", classNameVar); serviceTemplate.binding("packageName", servicePackage); serviceTemplate.binding("tableName", tableName); serviceTemplate.binding("className", className); serviceTemplate.binding("classNameVar", classNameVar); mapperTemplate.binding("packageName", mapperPackage); mapperTemplate.binding("tableName", tableName); mapperTemplate.binding("className", className); mapperTemplate.binding("classNameVar", classNameVar); // 创建文件夹 Files.createDirectories(Paths.get("src/main/java/" + controllerPackage.replace(".", "/"))); Files.createDirectories(Paths.get("src/main/java/" + servicePackage.replace(".", "/"))); Files.createDirectories(Paths.get("src/main/java/" + mapperPackage.replace(".", "/"))); // 生成文件 MapperInvoke invoke = new MapperInvoke(); invoke.setEntityPackage(entityPackage); invoke.setMapperClass(BaseMapper.class); invoke.setMapperTemplate(MapperTemplate.class); invoke.setSqlManager(MapperBuilder.INSTANCE.getSqlManager()); invoke.addOutput(controllerPackage, fileName, controllerTemplate.render()); invoke.addOutput(servicePackage, fileName, serviceTemplate.render()); invoke.addOutput(mapperPackage, fileName, mapperTemplate.render()); MapperConfig config = new MapperConfig(); config.setMapperInvoke(invoke); config.setEntityPackage(entityPackage); config.setMapperPackage(mapperPackage); config.setDaoSuffix("Mapper"); config.setSqlManager(MapperBuilder.INSTANCE.getSqlManager()); config.setTablePrefix(tablePrefix); config.init(); MapperBuilder.INSTANCE.build(config); } } } ``` 4. 运行${className}Generator类的main方法,即可自动生成Controller、Service和Mapper的代码。 需要注意的是,这里的代码模板只是一个简单的示例,如果需要根据实际需求进行修改。同时,生成的代码中可能需要根据实际情况进行一些调整和优化,比如添加日志记录、异常处理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值