发布时间:2025-06-24 17:26:22  作者:北方职教升学中心  阅读量:949


常用的java模板引擎还有哪些?

Jsp、"X"比较是不等的.因为FreeMarker是精确比较

  • 其它的运行符可以作用于数字和日期,但不能作用于字符串

  • 使用gt等字母运算符代替>会有更好的效果,因为 FreeMarker会把>解释成FTL标签的结束字符

  • 可以使用括号来避免这种情况,如:<#if (x>y)>

  • 逻辑运算符

    • 逻辑与:&&

    • 逻辑或:||

    • 逻辑非:!

    逻辑运算符只能作用于布尔值,否则将产生错误 。

    <#if ></if>

    4、FTL指令的内容会被freemarker忽略解析,直接输出内容。日期格式化

    显示年月日: ${today?date}显示时分秒:${today?time}显示日期+时间:${today?datetime}自定义格式化: ${today?string("yyyy年MM月")}

    3、

    01-basic.ftl

    <#--文章详情模板文件--><!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>Hello World!</title></head><body><b>普通文本 String 展示:</b><br><br>Hello ${stu.name!'-----------'} <br><hr><b>对象Student中的数据展示:</b><br/>姓名:${(stu.name)!''}<br/>年龄:${stu.age}<hr></body></html>

    02-list.ftl

    <!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>Hello World!</title></head><body><#-- list 数据的展示 --><b>展示list中的stu数据:</b><br><br><table>    <tr>        <td>序号</td>        <td>姓名</td>        <td>年龄</td>        <td>钱包</td>    </tr>    <#if stus??>        <#list stus as stu>            <#if stu.name=='小红'>                <tr style="color:#ff0000">                    <td>${stu_index + 1}</td>                    <td>${stu.name}</td>                    <td>${stu.age}</td>                    <td>${stu.money}</td>                </tr>            <#else >                <tr>                    <td>${stu_index + 1}</td>                    <td>${stu.name}</td>                    <td>${stu.age}</td>                    <td>${stu.money}</td>                </tr>            </#if>        </#list>    </#if>    stus集合的大小:${stus?size}</table><hr><#-- Map 数据的展示 --><b>map数据的展示:</b><br/><br/><a href="###">方式一:通过map['keyname'].property</a><br/>输出stu1的学生信息:<br/>姓名:${stuMap['stu1'].name}<br/>年龄:${stuMap['stu1'].age}<br/><br/><a href="###">方式二:通过map.keyname.property</a><br/>输出stu2的学生信息:<br/>姓名:${stuMap.stu2.name}<br/>年龄:${stuMap.stu2.age}<br/><br/><a href="###">遍历map中两个学生信息:</a><br/><table>    <tr>        <td>序号</td>        <td>姓名</td>        <td>年龄</td>        <td>钱包</td>    </tr>    <#list stuMap?keys as key>        <tr>            <#--索引index是从0开始的-->            <td>${key_index + 1}</td>            <td>${stuMap[key].name}</td>            <td>${stuMap[key].age}</td>            <td>${stuMap[key].money}</td>        </tr>    </#list></table><hr>当前的日期为:${today?datetime}<br>当前的日期为:${today?string("yyyy年MM月")}-----------------------------------------------------<br><#--${point?c}--></body></html>

    创建controller

    package com.example.springbootfreemarker.controller;import com.example.springbootfreemarker.entity.Student;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import java.util.*;//不能使用@RestController,因为是返回视图@Controllerpublic class HelloController {    @GetMapping("/basic")    public String hello(Model model){        //存储一个字符串//        model.addAttribute("name","freemarker");        //stu        Student student = new Student();        student.setName("小明");        student.setAge(18);        //存储一个对象        model.addAttribute("stu",student);        return "01-basic";    }    @GetMapping("/list")    public String list(Model model){        //------------------------------------        Student stu1 = new Student();        stu1.setName("小强");        stu1.setAge(18);        stu1.setMoney(1000.86f);        stu1.setBirthday(new Date());        //小红对象模型数据        Student stu2 = new Student();        stu2.setName("小红");        stu2.setMoney(200.1f);        stu2.setAge(19);        //将两个对象模型数据存放到List集合中        List<Student> stus = new ArrayList<>();        stus.add(stu1);        stus.add(stu2);        //向model中存放List集合数据        model.addAttribute("stus",stus);        //map数据        Map<String,Student> stuMap = new HashMap<>();        stuMap.put("stu1",stu1);        stuMap.put("stu2",stu2);        model.addAttribute("stuMap",stuMap);        //日期        model.addAttribute("today",new Date());        //长数值        model.addAttribute("point",38473897438743L);        return "02-list";    }}

    创建启动类

    package com.example.springbootfreemarker;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringbootFreemarkerApplication {    public static void main(String[] args) {        SpringApplication.run(SpringbootFreemarkerApplication.class, args);    }}

    测试

    启动项目:
     请求1:http://127.0.0.1:10000/list  请求2:http://127.0.0.1:10000/basic

    测试结果1: