FreeMarker:
记录一点数据输出结构
(root)
|
+- string
|
+- list{string,map}
|
+- map
// Build the data-model List<Object> list = new ArrayList<Object>(); list.add("red"); list.add("green"); list.add("blue"); SimpleHash mapColor = new SimpleHash(); mapColor.put("c1", "red"); mapColor.put("c2", "green"); mapColor.put("c3", "blue"); list.add(mapColor); // 将会使用default wrapper SimpleHash root = new SimpleHash(); root.put("theList", list);
FTL 文件. 怎么遍历list中的map
***************************************************************<br>
+- theList<br>
<#list theList as item>
| |<br>
****************第4个值放的map***********************************************<br>
<#if item_index =3>
<#assign keys = item?keys>
<#list keys as key>
||<br>
+- ${key} = ${item[key]}<br>
</#list>
<#else>
+- ${item_index + 1}. ${item}<br>
</#if>
</#list>
***************************************************************<br>
output:
+- theList | | +- 1. red | | +- 2. green | | +- 3. blue | | || +- c1 = red || +- c2 = green || +- c3 = blue
参考,
http://www.iteye.com/topic/165112
附件: ―Programmer's Guide
相关推荐
在FreeMarker中,遍历`List<Map<String>>`是常见的操作,尤其在处理从后端传来的复杂数据结构时。这篇博客链接虽然无法直接访问,但从标题来看,我们可以推测其内容可能涉及如何在FreeMarker模板中遍历一个包含Map...
-- 使用Freemarker遍历JSON数组 --> <ul> <#list data.array as item> <li>${item.property}</li> </#list> </ul> <!-- 示例HTML截取 --> <div>${truncateHtml(originalHtml, 100)}</div> </body> </html> ``...
Map<String, Object> model = new HashMap<>(); model.put("title", "FreeMarker示例"); model.put("name", name); return new ModelAndView("hello", model); } } ``` 这里的`@RequestMapping("/hello")`指定...
Map<String, Object> model = new HashMap<>(); model.put("title", "用户列表"); model.put("users", getUsers()); // 假设getUsers()返回User对象的List Configuration cfg = new Configuration(Configuration....
Map<String, Object> model = new HashMap<>(); model.put("title", "报告标题"); List<List<String>> data = new ArrayList<>(); // 填充数据... model.put("data", data); model.put("imagePath", "path/to/...
Map<String, String> expected = new HashMap<>(); expected.put("CN", "China"); expected.put("US", "United States"); // 模拟服务 when(dropdownService.getDropdownList(entityType)).thenReturn...
// 假设我们有一个Map<String, String> excelData存储了从Excel读取的数据 Map<String, String> excelData = ...; for (Map.Entry<String, String> entry : excelData.entrySet()) { String key = entry.getKey(); ...
9. **条件判断和循环**:`<#if>`、`<#else>`、`<#elseif>`用于条件判断,`<#foreach>`遍历集合,`<#list>`则提供更灵活的列表处理。 10. **逻辑运算和比较**:Freemarker支持基本的逻辑运算符(`&&`、`||`、`!`)和...
Map<String, Object> model = new HashMap<>(); model.put("newsItems", newsItems); StringWriter writer = new StringWriter(); template.process(model, writer); return writer.toString(); } } ``` ...
Freemarker 提供了多种循环遍历方式,包括遍历普通 List、遍历 Map 和遍历 List 的前面几项。遍历普通 List 的语法如下: ```html <#list someList as item> ... </#list> ``` 遍历 Map 的语法如下: ```html <#...
在这个模板中,`<#list>`指令用于遍历用户列表,`${}`则用于插入变量的值。 6. **集成测试**: 为了确保功能的正确性,我们需要进行集成测试,包括对API接口的单元测试以及前后端交互的端到端测试。我们可以使用...
List<Person> persons = new ArrayList<>(); persons.add(new Person("张三", 25)); persons.add(new Person("李四", 30)); ``` 2. **Freemarker模板(Template)**:这是一个`.ftl`文件,里面定义了XML的结构和...
Map<String, Object> dataModel = new HashMap<>(); dataModel.put("user", new User("John Doe")); FreemarkerConfig freemarkerConfig = new FreemarkerConfig(); freemarkerConfig.setTemplateLoaderPath(...
2. 循环语句:`<#foreach item in list>`遍历一个集合,`item`是当前项,`list`是可迭代对象。还可用`<#break>`跳出循环,`<#continue>`跳过本次循环。 三、函数与方法调用 Freemarker提供了许多内置函数,如`...
Map<String, Object> model = new HashMap<>(); model.put("username", "John Doe"); List<Item> items = ... // 初始化物品列表 model.put("items", items); ``` 6. **渲染模板** 使用配置好的`...
例如,`<#if>`用于条件判断,`<#foreach>`用于循环遍历。 6. **内置函数和方法**:FreeMarker提供了一些内置的函数,如`?html`用于转义HTML实体,`?length`用于获取集合的长度。此外,还可以在模板中调用对象的方法...
Map<String, Object> model = new HashMap<>(); User user = new User("李三"); model.put("user", user); ``` 3. **配置Freemarker**: 初始化`Configuration`对象并指定模板目录。 ```java Configuration ...
Map<String, Object> dataModel = new HashMap<>(); dataModel.put("user", new User("John Doe")); // 输出结果 String output = template.process(dataModel, new StringWriter()); ``` 四、FreeMarker优点 1. ...
Map<String, Object> model = new HashMap<>(); model.put("user", new User("John Doe")); List<Product> products = ... // 初始化产品列表 model.put("products", products); Configuration cfg = new ...