我的开发环境
框架: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 noExistList = new ArrayList();
noExistList = null;
model.addAttribute("noExistList", noExistList);
// 示例三
List strList = new ArrayList();
strList.add("www.");
strList.add("cnblogs.");
strList.add("com/sunang");
model.addAttribute("strList", strList);
// 示例四
Map strMap = new HashMap();
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文件,代码如下:
示例一输出结果:<#-- if指令的用法--> <#-- 在指令标籤内直接使用变量名得到文本值--> <#if flag == 1> flag = 1 <#elseif flag ==2> flag = 2 <#else> <#-- 在指令标籤外使用 ${变量名} 的格式来得到文本值--> flag!=1 && flag!=2 flag的值為:${flag} #if>
----------------------------------------------------------
示例二输出结果:<#-- 判断变量是否存在--> <#if noExistList??> List存在 <#else> List不存在 #if>
----------------------------------------------------------
示例三输出结果:<#-- list指令的用法,as可设置别名--> <#list strList as sl> <#-- 在变量名后加 _index 得到变量在容器中的序号,从0开始--> <#if sl_index == 0> 我的博客地址是:${sl} <#else> ${sl} #if> #list>
直接使用下标访问List:${strList[0]}${strList[1]}${strList[2]}
免费红色响应式多语言企业通用模板1.0.0下载该模板源码有公司简介、公司新闻、产品展示、客户案例、留言等企业官网常用页面功能。模板是响应式模板,支持多语言,完善的标签调用修改起来很方便。功能特点:1. 使用的框架采用HkCms开源内容管理系统v2.2.3版本、免费可以商用。2. 所需环境Apache/Nginx,PHP7.2 及以上 + MySQL 5.6 及以上。3. 安装教程: (1) 站点运行路径填写到public目录下。 (2) 浏览
----------------------------------------------------------
示例四输出结果:<#-- 使用 ${变量名.变量名} 获取容器对象的子对象--> ${strMap.mapKey0}${strMap.mapKey1}${strMap.mapKey2}
----------------------------------------------------------
示例五输出结果:<#-- 当变量是日期对象时,可使用函数使其按格式输出--> ${nowTime?string("yyyy-MM-dd")}
step3.运行与调试
将工程部署到tomcat并运行,在瀏览器输入:http://localhost:8080/你设置的工程名/helloWorld.htm
更多Freemarker常用指令使用示例相关文章请关注PHP中文网!










