@Controller
public class HelloWorldController {
@RequestMapping("/helloWorld")
public String helloWorld(Model model) {
// 1. 基本變數
model.addAttribute("flag", 0);
// 2. 空 List
List noExistList = null;
model.addAttribute("noExistList", noExistList);
// 3. 一般 List
List strList = Arrays.asList("www.", "cnblogs.", "com/sunang");
model.addAttribute("strList", strList);
// 4. Map
Map strMap = new HashMap<>();
strMap.put("mapKey0", "www.");
strMap.put("mapKey1", "cnblogs.");
strMap.put("mapKey2", "com/sunang");
model.addAttribute("strMap", strMap);
// 5. 日期物件
model.addAttribute("nowTime", new Date());
return "helloWorld.ftl";
}
}
<#if><#if flag == 1>
flag = 1
<#elseif flag == 2>
flag = 2
<#else>
flag!=1 && flag!=2 flag的值為:${flag}
</#if>
重點: 指令內直接用變數名,指令外用 ${變數} 輸出。
??<#if noExistList??>
List存在
<#else>
List不存在
</#if>
防止 NullPointerException 的必備技巧。
<#list><#list strList as sl>
<#if sl_index == 0>
我的博客地址是:${sl}
<#else>
${sl}
</#if>
</#list>
sl_index:索引(從 0 開始)sl_has_next:是否有下一項sl_is_first、sl_is_last:首尾判斷${strList[0]}${strList[1]}${strList[2]}
${strMap.mapKey0}${strMap.mapKey1}${strMap.mapKey2}
${nowTime?string("yyyy-MM-dd")}
常用格式:yyyy-MM-dd HH:mm:ss、yyyy/MM/dd 等。
部署至 Tomcat,瀏覽器訪問:https://localhost:8080/專案名/helloWorld.htm
