前端FreeMarker模板

div 属于HTML语言的,主要和css配合做样式的

freemark中设置变量

<#assige name="longteng"/>

使用变量,freemarker的取值用el表达式,请求controller路径,显示在页面的就是longteng,一般情况下变量是从后端返回至页面的

${name}

使用变量会出现空值

(后端没有name这个值,在前端直接获取就会报错),freemarker在出现空值的时候默认会抛出异常,用感叹号,防止空值时报错

${name!}

若要获取的值为空时,想给该变量设置默认值

${name! "abc"}

int类型超过3位之后自动增加逗号

(变为货币显示)
例如
后端设置money变量的值是9999

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    model.addAttribute("money",9999 );
    return "/my/FreeMarker";
}
}

前端freemarker中获取该money值,显示在页面就是9,999

${money!}

怎么让该值显示为9999
freemarker中获取值时,给变量名后面加上问号,加上string类型,加上井号格式化,例:

${money?string("#")}

时间格式化
controller中设置时间,new了一个当前时间

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    model.addAttribute("time",new Date());
    return "/my/FreeMarker";
}
}

freemarker中获取时间

,yyyy-MM-dd hh:mm:ss:sss就是获取到的时间的格式

${time?string("yyyy-MM-dd hh:mm:ss:sss")}

以上获取的都是普通参数,

freemarker中如何获取对象参数

实体类

import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
private char sex;
}

controller

import com.longteng.lesson2.my.animal.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    Animal animal=new Animal();
    animal.setName("xx");
    animal.setAge(10);
    animal.setSex('男');
    model.addAttribute("animalinfo",animal);
    return "/my/FreeMarker";
}
}

freemarker获取信息,controller中key.实体类属性名

${animalinfo.name!}
${animalinfo.age!}
${animalinfo.sex}

freemarker中的流程语句

实体类

import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
private char sex;
}

controller

import com.longteng.lesson2.my.animal.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    Animal animal=new Animal();
    animal.setName("xx");
    animal.setAge(10);
    animal.setSex('男');
    model.addAttribute("animalinfo",animal);
    return "/my/FreeMarker";
}
}

freemarker获取信息

<#if animalinfo.name="xx">
<span style="color: #0000cc">animal属性名是xx</span>
<#elseif animalinfo.name="LittleElephant">
<span style="color: green">animal属性名是LittleElephant</span>
<#else >
<span style="color: yellow">other</span>
</#if>

freemarker中判断一个对象是否存在

实体类

import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
private char sex;
}

controller

import com.longteng.lesson2.my.animal.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    Animal animal=new Animal();
    animal.setName("xx");
    animal.setAge(10);
    animal.setSex('男');
    model.addAttribute("animalinfo",animal);
    return "/my/FreeMarker";
}
}

freemarker获取信息

<#if animalinfo?exists>
animal对象存在
<#else >
animal对象不存在
</#if>

freemarker中判断一个值的大于(gt)、小于(lt)、大于等于(gte)、小于等于(lte)

实体类

import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
private char sex;
}

controller

import com.longteng.lesson2.my.animal.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    Animal animal=new Animal();
    animal.setName("xx");
    animal.setAge(10);
    animal.setSex('男');
    model.addAttribute("animalinfo",animal);
    return "/my/FreeMarker";
}
}

freemarker获取信息

<#if animalinfo.age gt 5>
animal对象的年龄大于5
<#elseif animalinfo.age lt 5>
animal对象的年龄小于5
<#else>
animalinfo.age等于5
</#if>

freemarker中的遍历,用list遍历

例:遍历的是一个对象
实体类

import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
private char sex;
}

controller

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    Animal animal=new Animal();
    animal.setName("LittleElephant");
    animal.setAge(10);
    animal.setSex('男');
    Animal animal1=new Animal();
    animal1.setName("yy");
    animal1.setAge(12);
    animal1.setSex('女');
    List<Animal> animalList=new ArrayList<>();
    animalList.add(animal);
    animalList.add(animal1);
    model.addAttribute("animalList",animalList);
    return "/my/FreeMarker";
}
}

freemarker

<#--animalList:controller中addAttribute的key-->
<#--animalinfo:别名-->
<#--用别名获取值-->
<#list animalList as animalinfo>
<div>
    ${animalinfo.name}
    ${animalinfo.age}
    ${animalinfo.sex}
</div>
</#list>

普通类型遍历
controller

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    List list=new ArrayList();
    list.add("a");
    list.add("b");
    list.add("c");
    model.addAttribute("stringList",list );
    return "/my/FreeMarker";
}
}

freemarker,直接遍历别名就好,不用取值,值就在别名里

<#list stringList as list>
<div>
    ${list}
</div>
</#list>

macros宏指令,封装方法用的

第一个参数是方法名,第一个参数只有的参数都是该方法的参数
实体类

import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
private char sex;
}

controller

import com.longteng.lesson2.my.animal.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/free")
public class FreeMarker {
@RequestMapping("/freeMarker")
public String freemarker(Model model){
    Animal animal=new Animal();
    animal.setName("LittleElephant");
    animal.setAge(10);
    animal.setSex('男');
    model.addAttribute("animalinfo",animal);
    return "/my/FreeMarker";
}
}

freemarker中
创建方法,方法名为methodName 该方法有3个参数

<#macro methodName param1 param2 param3>
<div>
    参数1:${param1}
    参数2:${param2}
    参数3:${param3}
</div>
</#macro>

freemarker中调用方法,传入3个参数,通过controller传入Animal实体类的属性参数
调用macro 在freemarker的任何地方都能调用

<@methodName "${animalinfo.name}" "${animalinfo.age}" "${animalinfo.sex}"></@methodName>

<#nested />宏指令中嵌套内容,接着上一个方法,嵌套的标签写在方法的输出div中

<#macro methodName param1 param2 param3>
<div>
    参数1:${param1}
    参数2:${param2}
    参数3:${param3}
</div>
<div>
    嵌套内容:<#nested />
</div>
</#macro>

调用方法,传参给了<#nested />标签

<@methodName
    "${animalinfo.name}"
    "${animalinfo.age}"
    "${animalinfo.sex}">
    给嵌套内容传参
</@methodName>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41767337/article/details/89476507
今日推荐