美文网首页
SpringBoot集成Freemarker

SpringBoot集成Freemarker

作者: 磨陀货_ | 来源:发表于2019-10-22 16:30 被阅读0次

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

引入pom.xml依赖如下
        <!--freemarker集成依赖包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

创建Controller测试类
@Controller
public class ThymeleafController {

    @RequestMapping("/index")
    public String hello(Model model){
        model.addAttribute("username","王昭君");
        return "index";
    }
}

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

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

如果要自定义的话,可以在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/freemarker
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">${username}</h1>
</body>
</html>

启动项目,输入地址http://localhost:8080/index显示如下则成功

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

相关文章

网友评论

      本文标题:SpringBoot集成Freemarker

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