Springboot使用Jetbrick模板

1、引入pom

<dependency>
  <groupId>com.github.subchen</groupId>
  <artifactId>jetbrick-template-springmvc</artifactId>
  <version>2.1.6</version>
</dependency>

2、编写配置文件

配置文件有几种编写方式,可以单独写一个jetbrick的配置,然后通过主配置文件application.yml指定,也可以全部在主配置中写

  • 2.1 新建jetbrick的配置方式

主配置文件

spring:
  jetbrick:
    template:
      config-location: classpath:jetbrick.properties
      charset: UTF-8
      # 该属性必须配置为true,不然不会加载改视图处理器
      #没有特别要求其他默认即可
      enabled: true

jetbrick配置(更多配置见官网)

jetx.autoscan.packages=cn.lkangle.jet.methods

# 在springboot中加载器必须用classpath的
# spring mvc中需要用servlet的
# 默认classpath加载器,前缀后缀在这里配置无效
# 需要在主配置中配置prefix suffix
#jetx.template.loaders = $loader
#$loader = jetbrick.template.loader.ClasspathResourceLoader
  • 2.2 全部在主配置文件中配置
spring:
  jetbrick:
    template:
      # 是否启用页面缓存
      cache: false
      # 页面默认编码方式,默认utf-8
      charset: UTF-8
      # 该属性必须配置为true,不然不会加载改视图处理器
      # 没有特别要求其他默认即可
      enabled: true
      # 该试图处理器的优先级,默认最低
      order: 1
      # 视图类型,默认text/html
      content-type: text/html
      # 默认 templates/ 
      prefix: templates/
      # 默认 .html
      suffix: .html
      # 一个map配置集合
      config:
        jetx.autoscan.packages: cn.lkangle.jet.methods

没有特别要求下只需要配置启用改解析器即可

spring:
  jetbrick:
    template:
      enabled: true

3、测试

  • 3.1 配置
spring:
  jetbrick:
    template:
      enabled: true
  • 3.2 启动类
@SpringBootApplication
@Controller
public class SbootcodingApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbootcodingApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello(ModelMap map) {
        map.put("msg", "Jetbrick");
        return "hello";
    }
}

  • 3.3 自定义方法
@JetAnnotations.Methods
public class StringMethods {

    public static Integer length(String str) {
        return str.length();
    }
}
  • 3.4 html界面
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Jetbrick Test</title>
</head>
<body>
<h3>Hello Spring Boot And Jetbrick</h3>
<h4>${msg}</h4>
<h5>Hello的长度是: ${"hello".length()}</h5>
</body>
</html>
  • 3.5 效果
Hello Spring Boot And Jetbrick
Jetbrick
Hello的长度是: 5

4、结语

自打使用了Blade框架后就一直使用Jetbrick这个模板,用起来还是比较顺手的。配了好几遍一直报错,最后才发现主要的enable这块,看源码这里应该默认就是true的呀,搞不懂是怎么回事,反正现在成功了,先能用就行~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容