List loop using freemarker tutorial in Java

Reprinted:  https://blog.csdn.net/Cheung1021/article/details/6146239

Use reference: web chapter of spring boot entry

In Freemarker applications, the List is often traversed to obtain the required data, and the data is sorted and processed and presented to the user. So how to traverse the List in Freemarker and sort the data in the List appropriately?

Through the following introduction, I believe you will find the answer.
1. A 
 
brief introduction
to the list command in Freemarker To traverse the list in Freemarker, you must use the list command, that is, <#list sequence as item>…</#list>
sequence is a collection expression, and item is a loop variable name, cannot be an expression.
When traversing the sequence, the value of the traversal variable is saved to the item.
For example:
<#list userList as user>
 
 

</#list>
 
 
The userList encapsulates many User objects. When we traverse the userList, we will save the value of the traversed User object to the above user variable. . Then when getting the value, we can get the userName property value of the User object through ${user.userName }.
 
 
The List directive also implies two loop variables:
         item_index: The position of the current iteration item among all iteration items, which is a numeric value.
         item_has_next: Used to determine whether the current iteration item is the last item in all iteration items.
Note: When using the above two loop variables, be sure to replace item with the loop variable name you define yourself, item is actually the prefix.
For example, if you define with <# list list as l>..</#list> then use l_index, l_has_next.
 
 
During the loop, if you want to
 
 
Breaking out of a loop is accomplished using a combination of the break instruction , ie <#break>.
2. 
 
Sorting List in Freemarker
Usually our sorting operations are implemented through the DAO layer. If we want to change our sorting at any time, we must modify our DAO layer code, which is really inconvenient. But Freemarker provides us with such a sorting method, which solves this problem.
1. 
 
sort The ascending sorting function
sort sorts the sequence, and requires that the variables in the sequence must be: strings (sorted by the first letter), numbers, and date values.
<#list list?sort as l>…</#list>
2. The 
 
sort_by function
sort_by has a parameter, which is used to specify the sub-variables to be sorted. The sorting is based on the corresponding values ​​of the variables, such as:
<# list userList?sort_by(“age”) as user>…</#list>
age is an attribute of the User object, and the sorting is performed according to the value of age.
3.  The
 
reverse descending sorting function
 
 
<#list list? reverse as l>…</#list>. reverse uses the same as sort. reverse can also be used with sort_by  

For example, if you want users to be sorted by age in descending order, you can write like this <#list userList?sort_by(“age”)?reverse as user>…</#list>

3.   How Freemarker traverses List instance application
creation Through the above introduction, I believe you already have an understanding of how Freemarker traverses List, so let's stop talking nonsense and start making an application.
User class
public class User{
 
 
private String username; private (omit set and get methods)  
 
 
  
 
 

}

user.html template file

<#--Freemarker traverse list-->
Simple traverse list:
<#list userList as user>
 
 
Username: ${user.userName}
 
 
Password   : ${user.userPassword}
 
 
Age   : ${user.age}
</ #list>


<#--Freemarker traverses the list and applies the list implicit variable item_index-->
item_index uses:
<#list userList as user>
${user_index+1}th user
 
 
username: ${user.userName}
 
 
password   : ${ user.userPassword}
 
 
age   : ${user.age}

</#list>

<#--Freemarker遍历list并应用list隐含变量item_has_next-->
item_has_next,size使用:
<#list userList as user>

  用户名:${user.userName}
 
 
密  码:${user.userPassword}
 
 
年  龄: ${user.age}
 
 
<#if !user_has_next>
 
 
共有${userList?size}最后一个用户是:${user.userName}
</#if>

</#list>


<#--Freemarker遍历list并按用户年龄升序排序-->

按用户年龄升序排序:
<#list userList?sort_by("age") as user>

  用户名:${user.userName}
 
 
密  码:${user.userPassword}
 
 
年  龄: ${user.age}
 
 

</#list>
<#--Freemarker遍历list并按用户年龄降序排序-->

按用户年龄降序排序:
<#list userList?sort_by("age")?reverse as user>

  用户名:${user.userName}
 
 
密  码:${user.userPassword}
 
 
年  龄: ${user.age}
</#list>
<#--Freemarker遍历list当用户年龄大于21岁时,停止输出-->
list中应用break:
<#list userList?sort_by("age")?reverse as user>

  用户名:${user.userName}
 
 
密  码:${user.userPassword}
 
 
年  龄: ${user.age}
 
 
<#if (user.age>21) >
 
 
  <#break>
 
 
</#if>
</#list>

ClientTest类
public class ClientTest{
 
 
public static List<User> initUserList(){
 
 
  
User user1=new User();
 
 
  user1.setUserName("张三");
 
 
  user1.setUserPassword("123");
 
 
  user1.setAge(20);
 
 
  
 
 
  User user2=new User();
 
 
  user2.setUserName("李四");
 
 
  user2.setUserPassword("123");
 
 
  user2.setAge(22);
 
 
  
 
 
  User user3=new User();
 
 
  user3.setUserName("王五");
 
 
  user3.setUserPassword("123");
 
 
  user3.setAge(21);

List<User> list=new ArrayList<User>();
 
 
  list.add(user1);
 
 
  list.add(user2);
list.add(user3);
return list;
}
public static void main(String[] args){
 
 
List<User> list=ClientTest.initUserList();  
Map<String,Object> root=new HashMap<String,Object>();
root.put(“userList”,list);
 
 
FreeMarkertUtil.analysisTemplate(“user.ftl”,”UTF-8”,root);
//FreeMarkUtil类可以参考我上传得源码。
}
}
好了,到这里程序就结束了,您可以运行ClientTest类来查看输出结果了。

希望通过本文的介绍,可以让您对FreeMarker如何遍历List有一个清楚的了解。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325506355&siteId=291194637