知识点

相关文章

更多

最近更新

更多

FreeMarker集合(List、Map)

2019-03-05 21:02|来源: 领悟书生

我们上一节认识了FreeMarker基本数据类型,接口认识FreeMarker集合(List、Map)


序列(List)

定义序列

<#-- 定义序列 -->  
<#assign mynums=[11,12,13,14,15,16,17,18,19]/>  
<#list mynums as mn>  
    ${mn}  
</#list>

拆分序列  

<#-- 拆分序列 -->  
<#-- 这里是将mynums序列中下标从3到6之间的元素拆分出来,组成一个新的序列 -->  
<#assign mynum01=mynums[3..6]/>  
<#list mynum01 as mn01>  
    ${mn01}  
</#list>

字符串的拆分  

${"你好,你来了吗今天看书了吗!"[0..8]}...
你好,你来了吗今天...

连续序列

<#-- 连续序列 -->  
<#-- 从55到58:注意此时若写成[55..58]或者[66..68]则会报错 -->  
<#assign num01=55..58/>  
<#list num01 as num>  
    ${num}  
</#list>  
<#list 66..68 as num>  
    ${num}  
</#list>


哈希表(Map)

定义哈希表

<#assign maps={"1":"张三","2":"李四"}>

${maps["1"]}

张三

<#--以下代码可以将mapkey转换为相应的序列-->

<#assign keys=maps?keys>

<#list keys as key>

${key}---${maps[key]}

</#list>

1---张三

2---李四

<#assign users={"username":"张三","password":"123"}>

${users.username}---${users["password"]}

张三---123

注意:

   FreeMarker中的哈希表要求其key必须是字符串,包括数据模型中的java.util.HashMap的key也要是字符串,否则报错

   不能直接用list遍历map,需要先将map的key转换为相应的序列


本文链接:FreeMarker集合(List、Map),本文由huangyineng原创,转载请注明出处

相关问答

更多
  • <#list list as info> ${info_index+1}
  • 当Map里面的key 是Object(String,Integer,etc)等要通过以下的方式来得到 <#list testMap?keys as testKey> < option value="${testKey}" > ${testMap.get(testKey)} 或者使用: <#list testMap.keySet() as testKey> < option value="${testKey}" > ${testMap.get(testKey)} 切记在webwork自带的freemarker ...
  • 你后台设值成map了,应该传list; Expected collection or sequence. articleList evaluated instead to org.apache.struts2.views.freemarker.StrutsBeanWrapper$FriendlyMapModel 期望一个集合或列表代替map
  • Java代码: List list = new ArrayList(); Map map1 = new HashMap(); map1.put("phone", "13655555555"); map1.put("email", "admin@vip.com"); map1.put("address", "china"); list.add(map1); Map map2 = new HashMap(); map2.put("phone", "13888888888"); map2.put("email", ...
  • Java代码: List list = new ArrayList(); Map map1 = new HashMap(); map1.put("phone", "13655555555"); map1.put("email", "admin@vip.com"); map1.put("address", "china"); list.add(map1); Map map2 = new HashMap(); map2.put("phone", "13888888888"); map2.put("email", ...
  • 你后台设值成map了,应该传list; Expected collection or sequence. articleList evaluated instead to org.apache.struts2.views.freemarker.StrutsBeanWrapper$FriendlyMapModel 期望一个集合或列表代替map
  • <#if clientSourceData?exists> <#list clientSourceData?keys as key> ${key} ${clientSourceData.get(key)} 直接嵌套在循环里面即可
  • 因为当涉及到空值时,freemarker有点奇怪,有两种方法可以解决它。 1.将其作为缺失值: ${map[x + " " + y]!} do Stuff ${map[x + " " + y]!} 2.只需将支票转换为真/假支票。 这可以通过使用带有isNull(Object obj)函数的实用程序类来完成。 utilClass.isNull(map[x + " " + y])==true since freemarker is kind of odd when it comes to null ...
  • 由于Map.get(Object)签名不能帮助FreeMarker选择正确的数字类型(并且因为Java equals在不同的Number子类中是false的,即使它们的值真的相同),你必须告诉FreeMarker你是什么类型的想: map?api.get(123?long) 。 As the Map.get(Object) signature doesn't help FreeMarker to choose the right numerical type (and because Java equals ...
  • 只公开公共类的方法/属性,因此User类必须是public(或者getName()必须从公共类/接口继承)。 Only the methods/properties of public classes are exposed, thus the User class must be public (or getName() must be inherited from a public class/interface).