SpringBoot系列十二、整合模版引擎freemarker

一、添加freemarker相关依赖
pom文件中添加以下依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

二、添加freemarker配置
在application.properties文件中加入以下配置

# 是否开启thymeleaf缓存,本地为false,生产建议为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
#类型
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
#文件后缀
spring.freemarker.suffix=.ftl
#路径
spring.freemarker.template-loader-path=classpath:/templates/

三、编写页面
在templates下创建文件加user,在user下添加页面user.ftl:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
Hello World!
<h1>hello world!</h1>

<!-- 获取controller传过来的值 -->
<h1> ${user.name} </h1>
<h1> ${user.age} </h1>

</body>
</html>

四、编写测试用的controller

@Controller
@RequestMapping("/user")
public class UserController {


    @GetMapping(value = "/index")
    public String index(ModelMap modelMap){
    	//User为一个实体类,有name和age两个属性
        User user = new User();
        user.setName("mw_monster");
        user.setAge(24);
        modelMap.addAttribute("user",user);
        return "user/user";
    }
}

五、启动项目,进行测试
启动项目,浏览器中访问http://localhost:8080/user/index,能进入到页面并且不报错,整合成功并测试成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值