spring - java freemarker没有@extends,@block这些指令?
大家讲道理
大家讲道理 2017-04-18 10:50:40
0
1
1085

文章:http://csjava.blog.163.com/bl...

那我用maven怎么下载这个依赖

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
巴扎黑

See freemarker-template-inheritance

Gradle dependencies

dependencies {
    compile 'kr.pe.kwonnam.freemarker:freemarker-template-inheritance:0.4.RELEASE'
}

Spring Configuration

@Bean
public Map<String, TemplateModel> freemarkerLayoutDirectives() {
    Map<String, TemplateModel> freemarkerLayoutDirectives = new HashMap<String, TemplateModel>();
    freemarkerLayoutDirectives.put("extends", new ExtendsDirective());
    freemarkerLayoutDirectives.put("block", new BlockDirective());
    freemarkerLayoutDirectives.put("put", new PutDirective());

    return freemarkerLayoutDirectives;
}

@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freemarkerConfig = new FreeMarkerConfigurer();
    freemarkerConfig.setTemplateLoaderPath("/WEB-INF/ftls/");
    freemarkerConfig.setDefaultEncoding("UTF-8");

    Map<String, Object> freemarkerVariables = new HashMap<String, Object>();
    freemarkerVariables.put("layout", freemarkerLayoutDirectives());

    freemarkerConfig.setFreemarkerVariables(freemarkerVariables);
    return freemarkerConfig;
}

@Bean
public ViewResolver viewResolver() {
    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
    viewResolver.setCache(false);
    viewResolver.setPrefix("");
    viewResolver.setSuffix(".ftl");
    viewResolver.setContentType("text/html; charset=utf-8");
    return viewResolver;
}

Use

base.ftl: layout

<!DOCTYPE html>
<html>
    <head>
        <title>Base Layout</title>
        <@layout.block name="head">
            <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        </@layout.block>
    </head>
    <body>
        <@layout.block name="header">
            <h1>Base Layout</h1>
        </@layout.block>
        <p class="base">
            <@layout.block name="contents">
                <h2>Contents will be here</h2>
            </@layout.block>
        </p>
        <@layout.block name="footer">
            <p>Footer base</p>
        </@layout.block>
    </body>
</html>

view.ftl : contents

<@layout.extends name="layouts/base.ftl">
    <@layout.put block="head">
        <script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script>
    </@layout.put>
    <@layout.put block="header" type="prepend">
        <h2>Index Page</h2>
    </@layout.put>
    <@layout.put block="contents">
        <p>blah.. blah..</p>
    </@layout.put>
    <@layout.put block="footer" type="replace">
        <hr/>
        <p class="footer">Footer replaced by index</p>
    </@layout.put>
</@layout.extends>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template