Home  >  Article  >  php教程  >  Freemarker common instruction usage examples

Freemarker common instruction usage examples

高洛峰
高洛峰Original
2017-01-05 13:53:031447browse

My development environment
Framework: springmvc+freemarker
Development tool: springsource-tool-suite-2.9.0
JDK version: 1.6.0_29
tomcat version: apache-tomcat-7.0 .26

step1. Write the controller file, the code is as follows:

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. Write the ftl file, the code is as follows:

<html>
<body>
示例一输出结果:
<p>
<#-- if指令的用法-->
<#-- 在指令标籤内直接使用变量名得到文本值-->
<#if flag == 1>
    flag = 1
<#elseif flag ==2>
    flag = 2
<#else>
    <#-- 在指令标籤外使用   ${变量名}   的格式来得到文本值-->
    flag!=1 && flag!=2 flag的值為:${flag}
</#if>
</p>
<p>----------------------------------------------------------</p>
示例二输出结果:
<p>
<#-- 判断变量是否存在-->
<#if noExistList??>
    List存在
<#else>
    List不存在
</#if>
</p>
<p>----------------------------------------------------------</p>
示例三输出结果:
<p>
<#-- list指令的用法,as可设置别名-->
<#list strList as sl>
    <#-- 在变量名后加   _index   得到变量在容器中的序号,从0开始-->
    <#if sl_index == 0>
        我的博客地址是:${sl}
    <#else>
        ${sl}
    </#if>
</#list>
</p>
<p><p></p>
直接使用下标访问List:${strList[0]}${strList[1]}${strList[2]}
</p>
<p>----------------------------------------------------------</p>
示例四输出结果:
<p>
<#-- 使用    ${变量名.变量名}   获取容器对象的子对象-->
${strMap.mapKey0}${strMap.mapKey1}${strMap.mapKey2}
</p>
<p>----------------------------------------------------------</p>
示例五输出结果:
<p>
<#-- 当变量是日期对象时,可使用函数使其按格式输出-->
${nowTime?string("yyyy-MM-dd")}
</p>
</body>
</html>

step3. Run and debug

Deploy the project to tomcat and run it, enter in the browser: http://localhost:8080/the project name you set/helloWorld.htm
For more Freemarker common instruction usage examples and related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn