Freemarker常用指令使用示例_java

分享到:

我的开发环境
框架:springmvc+freemarker
开发工具:springsource-tool-suite-2.9.0
JDK版本:1.6.0_29
tomcat版本:apache-tomcat-7.0.26

step1.编写controller文件,代码如下:


package www.asuan.com.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

@Controller
public class HelloWorldController {
    @RequestMapping("/helloWorld")
    public String helloWorld(Model model) {
        // 示例一
        int flag = 0;
        model.addAttribute("flag", flag);
        // 示例二
        List<String> noExistList = new ArrayList<String>();
        noExistList = null;
        model.addAttribute("noExistList", noExistList);
        // 示例三
        List<String> strList = new ArrayList<String>();
        strList.add("www.");
        strList.add("cnblogs.");
        strList.add("com/sunang");
        model.addAttribute("strList", strList);
        // 示例四
        Map<String, String> strMap = new HashMap<String, String>();
        strMap.put("mapKey0", "www.");
        strMap.put("mapKey1", "cnblogs.");
        strMap.put("mapKey2", "com/sunang");
        model.addAttribute("strMap", strMap);
        // 示例五
        Date nowTime = new Date();
        model.addAttribute("nowTime", nowTime);//传输时间对象
        return "helloWorld.ftl";
    }
}

step2.编写ftl文件,代码如下:


<html>
<body>
示例一输出结果:
<p>
<#-- if指令的用法-->
<#-- 在指令标

昵    称:
验证码:

相关文档: