springboot 之thymeleaf 与 freemarker的简单使用

本文介绍了SpringBoot不推荐使用jsp开发,企业开发主要使用Thymeleaf和Freemarker两种模板技术。Thymeleaf基于.html进行页面拿值、取值、遍历和后台传值操作;而Freemarker则在application.yml配置,支持页面拿值、取值、遍历、全局变量和页面包含。在IDEA中,可以通过设置添加Freemarker支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot不推荐使用jsp开发

那么企业的开发主要分为两种

1.thymeleaf模板(主要基于.html的开发

页面拿值

<!--页面拿值-->
<h3 th:text="${name}"></h3>

页面取值

<!--页面取值-->
<select name="hobby">
    <option th:each="user :${UserList}" th:text="${user.userName}" th:value="${user.id}"> </option>
</select>

页面遍历

<!--table表格的遍历-->
<table border="2" width="600px" th:height="20px" height="600px">
    <thead>
    <tr>
        <td>Id</td>
        <td>name</td>
        <td>distribution</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody>
    <tr th:each="user :${UserList}">
        <td  th:text="${user.id}"></td>
        <td  th:text="${user.userName}"></td>
        <td  th:text="${user.distribution}"></td>
        <td ><a  href="" onclick="">删除</a></td>
    </tr>
    </tbody>
</table>

后台传值

@Controller
public class UserController {
    @RequestMapping("/user/list")
    public ModelAndView UserList(){
        ModelAndView mv = new ModelAndView();
        List list = new ArrayList();
        list.add( new User("1","xiaowu","学生"));
        list.add( new User("2","huxiaowu","老师"));
        list.add( new User("3","dawu","经理"));
        mv.addObject("UserList",list);
        mv.addObject("name","小五");
        mv.setViewName("/list");
        return mv;
    }

2.freemarker模板(主要基于.ftl的开发

使用

1.在application.yml文件默认配置

server:
  servlet:
    context-path: /springboot
    spring:
      thymeleaf:
        cache: false
      freemarker:
        # 设置模板后缀名
        suffix: .ftl
        # 设置文档类型
        content-type: text/html
        # 设置页面编码格式
        charset: UTF-8
        # 设置页面缓存
        cache: false
        # 设置ftl文件路径,默认是/templates,为演示效果添加role
        template-loader-path: classpath:/templates

2.页面拿值 ,取值,遍历,全局变量,包含页面的使用

<h3>拿值</h3>
<#--如果前台传来null会报错  那么!表示如果为null的话替代为后面的值 也就是‘哈哈’-->
${role !'哈哈'}
<h3>if判断</h3>
<#--? exists 是否存在-->
<#if role ? exists>
    存在
</#if>

<#if sex='boy'>
    男
<#elseif sex='girl'>
    女
<#else>
保密
</#if>

<h3>foreach遍历</h3>
<table border="2" width="600px" th:height="10px" height="300px">
    <thead>
    <tr>
        <td>Id</td>
        <td>name</td>
        <td>distribution</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody>
    <#list RoleList as user>
    <tr >
        <td>${user.id}</td>
        <td>${user.userName}</td>
        <td>${user.distribution}</td>
        <td><a  href="" onclick="">删除</a></td>
    </tr>
    </#list>
    </tbody>
</table>
<h3>包含页面</h3>
<#include 'include.ftl'/>

<h3>局部变量(assign)/全局变量(global)</h3>
 <#assign ctx1>
     ${springMacroRequestContext.contextPath}
 </#assign>

<#global ctx2>
    ${springMacroRequestContext.contextPath}
</#global>

${ctx1}和${ctx2}

 

应用

 在Idea中没有创建freemarker的程序 可以自己创建与HTML一样的简单程序

1.进入IDEA的setting

 

 

2.添加

这时候就有应用了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值