码迷,mamicode.com
首页 > 其他好文 > 详细

freemarker迭代list、map等常规操作

时间:2018-11-05 19:18:28      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:detail   mod   home   结果   png   mapping   this   字符串   RKE   

转自:https://blog.csdn.net/wickedvalley/article/details/65937189

一、controller开始准备模型、数据
1.po类

 1  package com.hbut.ssm.po;
 2      
 3     /**
 4      * pojo类
 5      *
 6      */
 7     public class Children {
 8      
 9         private String name;
10         private Integer age;
11         private String gender;
12         
13         public Children(String name, Integer age, String gender) {
14             super();
15             this.name = name;
16             this.age = age;
17             this.gender = gender;
18         }
19         public String getName() {
20             return name;
21         }
22         public void setName(String name) {
23             this.name = name;
24         }
25         public Integer getAge() {
26             return age;
27         }
28         public void setAge(Integer age) {
29             this.age = age;
30         }
31         public String getGender() {
32             return gender;
33         }
34         public void setGender(String gender) {
35             this.gender = gender;
36         }
37         
38     }

 


2.模型数据准备

 1 @RequestMapping("/freemarker")
 2         public String index(Model model,HttpServletRequest request){
 3             //1.迭代list
 4             List<Children> childrenList= new ArrayList<Children>();
 5             childrenList.add(new Children("张三", 25, "男"));
 6             childrenList.add(new Children("李四", 28, "男"));
 7             childrenList.add(new Children("小红", 22, "女"));
 8             
 9             
10             //2.迭代map
11             Map<String,Object> userMap = new HashMap<String,Object>();
12             userMap.put("address","wuhan");
13             userMap.put("birthday","11月01");
14             userMap.put("phone",null);
15             
16             //3.迭代常规对象
17             model.addAttribute("flag",1);
18             model.addAttribute("home","wuhan");
19             
20             //list、map添加进model
21             model.addAttribute("childrenList",childrenList);
22             model.addAttribute("userMap",userMap);
23             return "free";
24         }

 

 
说明:数据有常规String、list、map的数据,需要freemarker页面解析数据

二、freemarker常用解析指令

 1  <Html>
 2         <head>
 3             <title>lerning---freemarker</title>
 4         </head>
 5         <body>
 6             1.list iterator
 7             <table border="1" cellspacing="0" cellpadding="20">
 8             <!--1. list-->
 9                 <#list childrenList as chilren>
10                    <tr>
11                      <td>${chilren.name}</td><td>${chilren.age}</td><td>${chilren.gender}</td>
12                     </tr>
13                 </#list>
14             </table>     <br>
15             
16             2.if jugde   <br>
17             <!--2.测试if-->
18             <#if flag==1>
19                result:flag==1
20             </#if><br><br>
21             
22             3.converter lowercase to uppercase<br>
23             <!--3.  ?代替为.  后面接内建函数,将字符串转为大写-->
24             result:${home?upper_case}<br><br>
25             
26             4.map iterator <br>
27             <!--4.迭代map中的key/value对-->
28             <#list userMap?keys as key>
29                 ${key}--${userMap[key]!("default value")}<br>
30             </#list>
31             
32         <body>
33     </html>


三、freemarker显示结果

技术分享图片

 

freemarker迭代list、map等常规操作

标签:detail   mod   home   结果   png   mapping   this   字符串   RKE   

原文地址:https://www.cnblogs.com/sjxbg/p/9910464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!