美文网首页
springboot集成freemarker模板步骤

springboot集成freemarker模板步骤

作者: JFang | 来源:发表于2018-05-07 11:28 被阅读0次

1.再pom.xml引入包依赖

<!-- 引入freemarker包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

2.加入配置文件(可不加,系统有默认配置)

########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
#comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

3.再resource文件夹中创建templates文件夹,然后创建index.ftl文件。freemarker中一般使用的${}进行标识

<html xmlns="http://www.w3.org/1999/html">
    <h1>${name}</h1>

    <#list names as name>
    <p>${name}</p></<br>
    </#list>

</html>

4.再Controller中进行调用,返回值是对应templates文件夹中的名称

 @RequestMapping("/home1")
    String home2(HttpServletRequest request) {
        List<String> mList = new ArrayList<String>();
        mList.add("zhangsan");
        mList.add("lisi");
        mList.add("方鼎捷");
        request.setAttribute("name","fangdingjie");
        request.setAttribute("names",mList);
        return "index";
    }

运行效果如下:


image.png

相关文章

网友评论

      本文标题:springboot集成freemarker模板步骤

      本文链接:https://www.haomeiwen.com/subject/msymrftx.html