欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot 集成 freemarker

程序员文章站 2022-04-29 17:33:49
前面我们已经实现了thymeleaf模板,其实freemarker和thymeleaf差不多,都可以取代JSP页面,实现步骤也差不多,我们来简单实现一下 引入pom.xml依赖如下 创建Controller测试类 application.properties配置文件你可以选择不配置默认,也可以进行手 ......

  前面我们已经实现了模板,其实freemarker和差不多,都可以取代jsp页面,实现步骤也差不多,我们来简单实现一下

引入pom.xml依赖如下

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-freemarker</artifactid>
</dependency>

  

创建controller测试类

/**
 * @author pillarzhang
 * @date 2019-06-03
 */
@controller
class freemarkercontroller {
    @requestmapping("/index")
    public string index(model model){
        model.addattribute("name","hello pillar");
        return "index";
    }
}

  

application.properties配置文件你可以选择不配置默认,也可以进行手动配置

选择默认时配置路径一定要写对,src/main/resources  static(js,css等静态文件),templates(页面路径)注意是ftl后缀

springboot 集成 freemarker

如果要自定义的话,可以在application.properties中设置如下等配置信息

spring.freemarker.charset=utf-8
spring.freemarker.suffix=.ftl
spring.freemarker.content-type=text/html; charset=utf-8
spring.freemarker.template-loader-path=classpath:/templates
spring.mvc.static-path-pattern=/static/**

  

index.ftl文件如下

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>freemarker</title>
</head>
<body>
<h1>hello world</h1>
<h1 style="color: red">${name}</h1>
</body>
</html>

  

启动项目,输入地址显示如下则成功

springboot 集成 freemarker

 

  如果遇到问题,可以结合集成出现的错误进行排查