目录
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
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";
- }
- }
- <h2>1.取值h2>
- <h3>1.1. 提供默认值h3>
- <#--报错-->
- <#--${uname} -->
- <#--不报错,但是页面无内容-->
- <#--${uname !}-->
- <#--给值设置一个默认值-->
- ${uname! '无名'}

- <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>

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

- <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>

- <h3>4 includeh3>
- <#include "/common.ftl" >
common.ftl
- <#assign ctx>
- ${springMacroRequestContext.contextPath}
- #assign>
- <#global ctx2>
- ${springMacroRequestContext.contextPath}
- #global>
- <h3>5 局部变量(assign)/全局变量(global)h3>
- ${ctx}:${ctx2}