spring-boot与模板引擎:修改模板路径和后缀名

系列传送门

spring-boot与模板引擎:静态资源文件路径
spring-boot与模板引擎:修改模板路径和后缀名
spring-boot与模板引擎:使用metisMenu实现动态多级菜单

spring-boot与模板引擎:使用metisMenu实现动态多级菜单一文中,我们使用了spring-boot和beetl模板引擎。
在使用时,产生了下面2个疑问:
1、创建模板文件夹为什么一定要用“templates”,可以换成其他的文件名吗?
2、创建的模板,一定要用“btl”做后缀名吗?能不能使用“html”或者其他的后缀名?
下面让我们一起探讨下以上的问题。

先看看FreeMarker,Groovy,Thymeleaf,Mustache这四种模板引擎

spring-boot docs 中有下面句话:

Spring Boot includes auto-configuration support for the following templating engines:

When you’re using one of these templating engines with the default configuration, your templates will be picked up automatically from:

src/main/resources/templates.

查看包名org.springframework.boot.autoconfigure.thymeleaf下的ThymeleafProperties源码:

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
    private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML5";
    ......
    ......
}

查看包名org.springframework.boot.autoconfigure.groovy.template下的GroovyTemplateProperties源码:

@ConfigurationProperties(
    prefix = "spring.groovy.template",
    ignoreUnknownFields = true
)
public class GroovyTemplateProperties extends AbstractTemplateViewResolverProperties {
    public static final String DEFAULT_RESOURCE_LOADER_PATH = "classpath:/templates/";
    public static final String DEFAULT_PREFIX = "";
    public static final String DEFAULT_SUFFIX = ".tpl";
    public static final String DEFAULT_REQUEST_CONTEXT_ATTRIBUTE = "spring";
    private String resourceLoaderPath = "classpath:/templates/";
    ......
    ......
}

查看包名org.springframework.boot.autoconfigure.freemarker下的FreeMarkerProperties源码:

@ConfigurationProperties(
    prefix = "spring.freemarker"
)
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
    public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
    public static final String DEFAULT_PREFIX = "";
    public static final String DEFAULT_SUFFIX = ".ftl";
    private Map<String, String> settings = new HashMap();
    private String[] templateLoaderPath = new String[]{"classpath:/templates/"};
    private boolean preferFileSystemAccess = true;
    ......
    ......
}

查看包名org.springframework.boot.autoconfigure.mustache下的MustacheProperties源码:

@ConfigurationProperties(
    prefix = "spring.mustache"
)
public class MustacheProperties extends AbstractTemplateViewResolverProperties {
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";

    public MustacheProperties() {
        super("classpath:/templates/", ".html");
    }
    ......
    ......
}

从上面可以看出,对于spring-boot项目,如果使用以上任一模板,而且不做任何设置,spring-boot是默认把classpath路径下的templates作为模板文件的根目录,模板的后缀名也默认为对应模板的默认后缀名。

如果想要更改模板文件的根目录和模板后缀名,就需要新建一个Application.properties文件,在该文件里进行配置。

配置其实很简单。继续翻看上面4个模板的配置源码,我们注意到,在class定义之前,有一个@ConfigurationProperties注解:

@ConfigurationProperties(
    prefix = "spring.XXXXXX"
)

从该注解可以看出,不同的模板引擎,spring-boo从Application.properties中读取的配置的名字的前缀是不同的。
而且,在spring-boot启动时,会通过该注解,自动把Application.properties中配置的spring.XXXXXX.abc赋值给对应模板Properties的abc属性。
所以,在Application.properties中只需要添加spring.XXXXXX.abc对应的配置即可。
下面是配置示例:

......
......
#THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache=true # set to false for hot refresh

#FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allowRequestOverride=false
spring.freemarker.allowSessionOverride=false
spring.freemarker.cache=true
spring.freemarker.checkTemplateLocation=true
spring.freemarker.contentType=text/html
spring.freemarker.exposeRequestAttributes=false
spring.freemarker.exposeSessionAttributes=false
spring.freemarker.exposeSpringMacroHelpers=false
spring.freemarker.prefix=
spring.freemarker.requestContextAttribute=
spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
spring.freemarker.templateEncoding=UTF-8
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.viewNames= # whitelist of view names that can be resolved


#GROOVY TEMPLATES (GroovyTemplateAutoConfiguration)
spring.groovy.template.allowRequestOverride=false
spring.groovy.template.allowSessionOverride=false
spring.groovy.template.cache=true
spring.groovy.template.configuration.*= # See Groovy's TemplateConfiguration
spring.groovy.template.contentType=text/html
spring.groovy.template.prefix=classpath:/templates/
spring.groovy.template.suffix=.tpl
spring.groovy.template.templateEncoding=UTF-8
spring.groovy.template.viewNames= # whitelist of view names that can be resolved
......
......

下面让我们来看看beetl模板引擎

对于spring-boot,如果使用beetl模板引擎,不能添加beetl的lib包,需要添加下面的lib包:

<!-- beetl -->
<dependency>
    <groupId>com.ibeetl</groupId>
    <artifactId>beetl-framework-starter</artifactId>
    <version>1.1.22.RELEASE</version>
</dependency>

添加好之后,让我们像上面一样,分析下beetl模板引擎的默认配置,和怎么修改默认配置。

查看包名com.ibeetl.starter下的BeetlTemplateConfig源码:

......
......
public class BeetlTemplateConfig {
    @Value("${beetl.templatesPath:templates}")
    String templatesPath;
    @Value("${beetl.suffix:btl}")
    String suffix;
    @Value("${beetl-beetlsq.dev:true}")
    boolean dev;
    ......
    ......
}

一目了然,修改Application.properties如下:

beetl.templatesPath=templates/other/
beetl.suffix=html

OR

beetl.templatesPath=other/
beetl.suffix=html

此时,你可以自由的在templates目录下新建其他文件夹或者直接在resource下新建其他文件夹,并把以html为后缀的模板文件放进去了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容