【Java工具】模板引擎freemarker,基本使用

官方文档

http://freemarker.foofun.cn/toc.html

为什么要使用模板引擎,我认为有这一个好处:减少网络资源争夺程度。
假如你的tomcat只能支持500个并发,你一个页面里面需要调用5次ajax请求,去获取数据,那么这个页面会有一定程度的卡顿,对服务器的压力也比较大。所以使用模板引擎,直接将数据放入模板引擎中返回即可,只需要调用一次接口,也只需要接收一次数据。

一、普通数据操作

后端:

@Controller
public class Test1Controller {
    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response){
        List<User> userList = new ArrayList<User>();
        userList.add(new User(1,"老王","123456",1));
        userList.add(new User(2,"老李","123456",1));
        userList.add(new User(3,"小红","123456",1));
        userList.add(new User(4,"小白","123456",0));
        userList.add(new User(5,"小黑","123456",1));
        model.addAttribute("userList",userList);
        return "index";
    }
}

public class User {
    private int id;
    private String name;
    private String password;
    private int state;

    public User(int id, String name, String password, int state) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.state = state;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}

配置项:

server:
  port: 8888

spring:
  freemarker:
    template-loader-path: classpath:/templates
    cache: false
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request
    suffix: .ftl

前端:

1、得到对象:
${u}
${u.name} ${u["name"]} <!--这两个等价-->
${u.name[0]}  <!--获取name中第一个字符-->
${userList[0].name}
<!--字符串切分: 包含结尾:-->
${name[0..4]}
<!--不包含结尾:-->
${name[0..<5]}
<!--基于长度(宽容处理):-->
${name[0..*5]}
<!--去除开头:-->
${name[5..]}
2、流程控制:
直接if
<#if condition1>
    ...
</#if>
if 和 elseif
<#if condition1>
    condition1条件下执行的内容
    <#elseif condition2>
        condition2条件下执行的内容
</#if>
if和elseif和else
<#if condition1>
    condition1条件下执行的内容
    <#elseif condition2>
        condition2条件下执行的内容
    <#else>
        else的内容(非condition1&&非condition2的内容)
</#if>
3、集合遍历
普通遍历
<#list userList as user>
    ${user.name}
</#list>
遍历时加上特殊符号

以逗号隔开。最后一位的后面没有逗号

<#list userList as user>${user.name}<#sep>, </#list>
<!--输出很多用户名,以逗号隔开,最后一个后面没有括号--> 
list+else

集合为空时,显示else中的内容

<p>Users: <#list userList as user>${user.name}<#sep>, <#else>None</#list>
list+if
<#list userList as user>
      <div<#if user.state> style="color='red'"</#if>>
        ${user.name} : ${user.address}
      </div>
</#list>

二、内建函数

1、处理不存在的变量
在变量引用后面加入一个!
<h1>Welcome ${user!"visitor"}!</h1>
在变量后面加入??
<#if user??><h1>Welcome ${user}!</h1></#if>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值