FreeMarker取对象的值

阅读量: 499 编辑

FreeMarker取对象的值

一、语法

  • 取值语法 ${}

  • 基本类型 ${var}

  • 对象类型 ${obj.var}

  • 如何避免 null 值:${var!} ${(obj.var)!}

  • 如何输出默认值 ${var!'default'}

二、代码实战

代码的详细解读,可以参考视频教程

S10Controller.java

package com.qicong.sboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * User: 祁大聪
 */
@Controller
public class S10Controller {

    @GetMapping("/s10")
    public ModelAndView s10(){

        ModelAndView mv = new ModelAndView("s10");
        mv.addObject("age", 22);
        mv.addObject("name", "qiDaCong");

        Person p = new Person();
        p.setName("sanNi");
        p.setAge(18);
        p.setHeight(180);
        mv.addObject("person",p);//key:value

        return mv;
    }

}



s10.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>s10</title>
    </head>
    <body>
        hello ${name!}, 年龄=${age!}
        <br/>
        ${height!'170'}
        <br/>
        ${(person.name)!},${(person.age)!},${(person.height)!}
    </body>
</html>