SpringBoot整合Beetl模板

1.引入依赖

      
        
            com.ibeetl
            beetl
            2.9.3
        

2.创建BeetlConfig配置类

package com.etone.etonenetcard.config;

import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * web 配置类
 *
 * @author fengshuonan
 * @date 2016年11月12日 下午5:03:32
 */
@Configuration
public class BeetlConfig {

    @Autowired
    BeetlProperties beetlProperties;

    /**
     * beetl的配置
     */
    @Bean(initMethod = "init")
    public BeetlConfiguration beetlConfiguration() {
        BeetlConfiguration beetlConfiguration = new BeetlConfiguration();
        beetlConfiguration.setResourceLoader(new ClasspathResourceLoader(BeetlConfig.class.getClassLoader(), beetlProperties.getPrefix()));
        beetlConfiguration.setConfigProperties(beetlProperties.getProperties());
        return beetlConfiguration;
    }

    /**
     * beetl的视图解析器
     */
    @Bean
    public BeetlSpringViewResolver beetlViewResolver() {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setConfig(beetlConfiguration());
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        return beetlSpringViewResolver;
    }
}

2.BeetlProperties属性映射类

package com.etone.etonenetcard.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.Properties;

/**
 * beetl配置(如果需要配置别的配置可参照这个形式自己添加)
 *
 * @author fengshuonan
 * @date 2017-05-24 20:37
 */
@Configuration
@ConfigurationProperties(prefix = BeetlProperties.BEETLCONF_PREFIX)
public class BeetlProperties {

    public static final String BEETLCONF_PREFIX = "beetl";

    private String delimiterStatementStart;

    private String delimiterStatementEnd;

    private String resourceTagroot;

    private String resourceTagsuffix;

    private String resourceAutoCheck;

    @Value("${spring.mvc.view.prefix}")
    private String prefix;

    public Properties getProperties() {
        Properties properties = new Properties();
        if (!StringUtils.isEmpty(delimiterStatementStart)) {
            if (delimiterStatementStart.startsWith("\\")) {
                delimiterStatementStart = delimiterStatementStart.substring(1);
            }
            properties.setProperty("DELIMITER_STATEMENT_START", delimiterStatementStart);
        }
        if (!StringUtils.isEmpty(delimiterStatementEnd)) {
            properties.setProperty("DELIMITER_STATEMENT_END", delimiterStatementEnd);
        } else {
            properties.setProperty("DELIMITER_STATEMENT_END", "null");
        }
        if (!StringUtils.isEmpty(resourceTagroot)) {
            properties.setProperty("RESOURCE.tagRoot", resourceTagroot);
        }
        if (!StringUtils.isEmpty(resourceTagsuffix)) {
            properties.setProperty("RESOURCE.tagSuffix", resourceTagsuffix);
        }
        if (!StringUtils.isEmpty(resourceAutoCheck)) {
            properties.setProperty("RESOURCE.autoCheck", resourceAutoCheck);
        }
        return properties;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getDelimiterStatementStart() {
        return delimiterStatementStart;
    }

    public void setDelimiterStatementStart(String delimiterStatementStart) {
        this.delimiterStatementStart = delimiterStatementStart;
    }

    public String getDelimiterStatementEnd() {
        return delimiterStatementEnd;
    }

    public void setDelimiterStatementEnd(String delimiterStatementEnd) {
        this.delimiterStatementEnd = delimiterStatementEnd;
    }

    public String getResourceTagroot() {
        return resourceTagroot;
    }

    public void setResourceTagroot(String resourceTagroot) {
        this.resourceTagroot = resourceTagroot;
    }

    public String getResourceTagsuffix() {
        return resourceTagsuffix;
    }

    public void setResourceTagsuffix(String resourceTagsuffix) {
        this.resourceTagsuffix = resourceTagsuffix;
    }

    public String getResourceAutoCheck() {
        return resourceAutoCheck;
    }

    public void setResourceAutoCheck(String resourceAutoCheck) {
        this.resourceAutoCheck = resourceAutoCheck;
    }
}

BeetlConfiguration 

package com.stylefeng.guns.core.beetl;

import com.stylefeng.guns.core.util.KaptchaUtil;
import com.stylefeng.guns.core.util.ToolUtil;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;

/**
 * beetl拓展配置,绑定一些工具类,方便在模板中直接调用
 *
 * @author stylefeng
 * @Date 2018/2/22 21:03
 */
public class BeetlConfiguration extends BeetlGroupUtilConfiguration {

    @Override
    public void initOther() {
        groupTemplate.registerFunctionPackage("shiro", new ShiroExt());
        groupTemplate.registerFunctionPackage("tool", new ToolUtil());
        groupTemplate.registerFunctionPackage("kaptcha", new KaptchaUtil());
    }
}

3.yml

server:
  servlet:
    context-path: /netcard
##########################################################
##################  所有profile共有的配置  #################
##########################################################


###################  beetl配置  ###################
beetl:
  delimiter-statement-start: \@   #开始结束标签(yaml不允许@开头)
  delimiter-statement-end: null
  resource-tagroot: common/tags   #自定义标签文件Root目录和后缀
  resource-tagsuffix: tag
  resource-auto-check: true #是否检测文件变化,开发用true合适,但线上要改为false


###################  spring配置  ###################
spring:
  profiles:
    active: @profileActive@
  mvc:
    static-path-pattern: /static/**
    view:
      prefix: /WEB-INF/view
  http:
    converters:
      preferred-json-mapper: fastjson
  devtools:
    restart:
      enabled: false #是否开启开发者工具(true/false)
      additional-paths: src/main/java
      exclude: static/**,WEB-INF/view/**
  aop:
    proxy-target-class: true #false为启用jdk默认动态代理,true为cglib动态代理

##################  mybatis配置  ###################
mybatis:
  mapper-locations: classpath:mapping/**/*.xml
  type-aliases-package: com.etone.etonenetcard.controller


4.创建webapp文件夹,并设置为根目录

SpringBoot整合Beetl模板_第1张图片

SpringBoot整合Beetl模板_第2张图片

5.controller

package com.etone.etonenetcard.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class CollectionInformation {
    /**
     * 跳转到主页
     */
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(){
        return "/informationCollection.html";
    }

    /**
     * 跳转到信息收集页面
     */
    @RequestMapping(value="/information",method = RequestMethod.GET)
    public String collectionInformation(){
        return "/informationCollection.html";
    }
}

7.HTML




    
    
    员工信息收集
    
    
    
    
    



SpringBoot整合Beetl模板_第3张图片

8.引入静态资源的方式

SpringBoot整合Beetl模板_第4张图片

SpringBoot整合Beetl模板_第5张图片

SpringBoot整合Beetl模板_第6张图片

SpringBoot整合Beetl模板_第7张图片

2.搭建之后就可以用model向html页面传值,然后用${item}取值了

//用户是唯一的,所以根据openid可以区分出不同的用户,和推广员绑定
model.addAttribute("openid",openid);
return "/handleOnlineCard.html";

SpringBoot整合Beetl模板_第8张图片

 

 

你可能感兴趣的:(Beetl模板引擎)