freemarker整合springboot(一):简单页面渲染

简单页面渲染

freemarker做静态化有很多细节,有些是和业务相关的,有些是与技术的使用相关,比如分页的实现、国家化、前后端分离、自定义标签等,这里先介绍简单的页面渲染。

本地渲染(前后端在一个容器中)

maven依赖

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

application.yml配置

spring:
  freemarker:
    template-loader-path:
    - classpath:/templates
    - file:./templates
    cache: true
    checkTemplateLocation: true
    settings:
      classic_compatible: true #处理空值
      template_exception_handler: rethrow
      template_update_delay: 0
      datetime_format: yyyy-MM-dd HH:mm
      number_format: 0.##
    suffix: .ftl

页面模板

页面模板路径
模板页面

渲染

服务端
@Controller
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private Configuration configuration;

    @RequestMapping("show.html")
    public String user(ModelMap modelMap) {
        PageVO page = new PageVO();
        // 读取json数据
           page.setContent(JsonUtil.readJsonFile("static/config/userList.json"));
        // 将要渲染的数据放到modelMap       
        modelMap.addAttribute("page", page);
        // 返回模板页面
        return "/user/show";
    }
}
页面
<tbody>
    <#list page.content as row>
    <tr>
        <!-- 页面通过${}取数据,通过list,if等标签做逻辑判断等数据处理 -->
        <td class="text-center">${row.id}</td>
        <td>${row.username}</td>
        <td>${row.name}</td>
        <td>${row.email}</td>
        <td>
            <#list row.roles as role>
                ${role.name}
            </#list>
        </td>
        <td>
            <#if (row.status == 0)>
                <span class="label label-success">启用</span>
            <#else>
                <span class="label label-default">禁用</span>
            </#if>
        </td>

效果

渲染结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

future_1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值