springboot视图渲染技术

目录

一、Freemarker简介及入门

二、Freemarker语法

 1、取值

1.1默认值

1.2 对null值进行判断

2、条件 

3、循环

4、include

5、 局部变量(assign)/全局变量(global)


一、Freemarker简介及入门

学习网址:什么是 FreeMarker? - FreeMarker 中文官方参考手册

二、Freemarker语法

indexController.java

package com.cxy.testspboot04.web;

import com.cxy.testspboot04.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;


@Controller
public class indexController {
    @GetMapping("/")
    public String index(Model model) {
        System.out.println("come in............");
        model.addAttribute("uname", "小刘");
        model.addAttribute("sex", "girl");
        List<User> lst = new ArrayList<>();
        lst.add(new User(1, "zs"));
        lst.add(new User(2, "ls"));
        lst.add(new User(3, "ww"));

        model.addAttribute("lst",lst);
        model.addAttribute("arr", new Integer[]{5, 6, 7, 8, 9});
        return "index";
    }
}

 1、取值

1.1默认值

<h2>1.取值</h2>
<h3>1.1. 提供默认值</h3>
<#--报错-->
<#--${uname}    -->
<#--不报错,但是页面无内容-->
<#--${uname !}-->
<#--给值设置一个默认值-->
${uname! '无名'}

1.2 对null值进行判断

<h3>1.2. 对null值先进行判断</h3>
<p>1)exists用在逻辑判断;</p>
<#if uname?exists>
    ${uname}
</#if>
<br>
<p>2)??是判断对象是否为空</p>
<#if uname??>
    ${uname}
</#if>
<br>
<p>3)if_exists用来打印东西</p>
${uname?if_exists}
<br>

2、条件 

<h3>2.条件</h3>
<#if sex=='nv'>
    女
<#elseif sex='nan'>
    男
<#else>
    未知
</#if>
<br>

3、循环

<h3>3.循环</h3>
<p>1)取出数组中的元素</p>
<#list arr as a>
    ${a}==
</#list>
<br>
<p>2)取出集合中的对象(注:访问的类要被public所修饰)</p>
<#list lst as item>
    ${item.id} : ${item.name}    <br>
</#list>

4、include

<h3>4 include</h3>
<#include "/common.ftl" >

common.ftl  

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

5、 局部变量(assign)/全局变量(global)

<h3>5 局部变量(assign)/全局变量(global)</h3>
${ctx}:${ctx2}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值