013_序列内建函数
1. chunk
1.1. 該內建函數將序列分隔為多個序列, 長度為第一個參數給定的值(比如: mySeq?chunk(3))。結果是包含這些序列的一個序列。最后一個序列可能比給定的長度要小, 除非第二個參數也給定了(比如: mySeq?chunk(3, '-')), 這就是用來填充最后一個序列, 以達到給定的長度。
1.2. 該內建函數通常在輸出的序列中使用表格/柱狀的格式。當被用于html表格時, 第二個參數通常是"\xA0"(也就是不換行的空格代碼, 也就是我們所知的"nbsp"), ?所以空td的邊界就不會不丟失。
1.3. 第一個參數必須是一個數字, 而且至少是1。如果這個數字不是整數, 那么它會被靜默地去掉小數部分(也就是說3.1和3.9都會被規整為3)。第二個參數可以是任意類型的值。
2. first
2.1. 序列的第一個子變量。如果序列為空, 那么模板處理將會中止。
3. join
3.1. 使用給定的分隔符來連接序列中的項為一個獨立的字符串。
3.2. 序列中不是字符串的項會被轉換為字符串, 使用${...}相同的轉換規則(當然這里不會應用自動轉義)。
3.3. join(...)最多可以有3個參數
3.3.1. 分隔符, 是必須的, 插入到每一項中間的字符串。
3.3.2. 空值, 默認是""(空字符串), 如果序列為空, 使用該值。
3.3.3. 列表結尾, 默認是"" (空字符串), 如果列表序列不為空, 該值在最后一個值后面輸出。
4. last
4.1. 序列的最后一個子變量。如果序列為空,那么模板處理將會中止。
5. reverse
5.1. 序列的反序形式。
6. seq_contains
6.1. 辨別序列中是否包含指定值。它包含一個參數, 就是來查找的值。
6.2. 為了查找值, 該內建函數使用了FreeMarker的比較規則(就像使用==操作符)。你可以使用它來查找標量值(也就是字符串, 數字, 布爾值, 或日期/時間類型)。對于其他類型結果通常都是 false。
6.3. seq_前綴在該內建函數名字中是需要的, 用來和contains內建函數區分開。contains用來在字符串中查找子串(因為變量可以同時當作字符串和序列)。
7. seq_index_of
7.1. 返回序列中第一次出現該值時的索引位置, 如果序列不包含指定的值時返回-1。要查找的值作為第一個參數。
7.2. 為了查找值, 該內建函數使用了FreeMarker的比較規則(就像使用了==操作符)。你可以使用它來查找標量值(也就是字符串, 數字, 布爾值, 或日期/時間類型)。對于其他類型結果通常是-1。
7.3. 搜索開始的索引值可以由第二個可選參數來確定。如果在同一個序列中相同的項可以多次出現時, 這是很有用的。第二個參數的數值沒有什么限制: 如果它是負數, 那么就和它是零的效果一樣, 而如果它是比序列長度還大的數, 那么就和它是序列長度值的效果一樣。小數值會被切成整數。
7.4. seq_前綴在該內建函數名字中是需要的, 用來和index_of內建函數區分開。index_of用來在字符串中查找子串(因為變量可以同時當作字符串和序列)。
8. seq_last_index_of
8.1. 返回序列中最后一次出現值的索引位置, 如果序列不包含指定的值時返回-1。也就是說, 和seq_index_of相同, 只是在序列中從最后一項開始向前搜索。
8.2. 它也支持可選的第二個參數來確定從哪里開始搜索的索引位置。
8.3. seq_前綴在該內建函數名字中是需要的, 用來和last_index_of內建函數區分開。last_index_of用來在字符串中查找子串(因為變量可以同時當作字符串和序列)。
9. size
9.1. 序列中子變量的數量(作為數字值)。假設序列中至少有一個子變量, 那么序列s中最大的索引是s?size - 1(因為第一個子變量的序列是0)。
10. sort
10.1. 以升序方式返回序列。(要使用降序排列時, 使用它之后使用reverse內建函數。)這僅在子變量都是字符串時有效, 或者子變量都是數字, 或者子變量都是日期值(日期, 時間, 或日期+時間)。如果子變量是字符串, 它使用本地化(語言)的具體單詞排序(通常是大小寫不敏感的)。
11. 例子
11.1. 新建一個名為FMBuiltInsForSequences的動態Web工程, 同時添加相關jar包。
11.2. 編寫FMFactory.java
package com.fm.util;import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import freemarker.template.Configuration; import freemarker.template.TemplateExceptionHandler;public class FMFactory {private final static FMFactory instance = new FMFactory();private FMFactory() {}public static FMFactory getInstance() {return instance;}private Map<String, Configuration> map = new ConcurrentHashMap<String, Configuration>();// 創建單個Configuration實例public synchronized Configuration getCfg(Object servletContext, String path) {if(null != map.get(path)) {return map.get(path);}Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);cfg.setServletContextForTemplateLoading(servletContext, path);cfg.setDefaultEncoding("utf-8");// 本地化設置cfg.setLocale(Locale.CHINA);cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);map.put(path, cfg);return cfg;}}11.3. 編寫BuiltInsForSequences.java
package com.fm.action;import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.fm.util.FMFactory; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;public class BuiltInsForSequences extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Configuration cfg = FMFactory.getInstance().getCfg(req.getServletContext(), "/WEB-INF/templates");Template template = cfg.getTemplate("builtinsforsequences.html");Map<String, Object> root = new HashMap<String, Object>();Writer out = new OutputStreamWriter(resp.getOutputStream());try {template.process(root, out);} catch (TemplateException e) {e.printStackTrace();}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }11.4. 修改web.xml
11.5. 在/WEB-INF/templates下編寫builtinsforsequences.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>序列內建函數</title></head><body><h2>分隔序列為多個序列</h2><#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']><#list seq?chunk(4) as row><#list row as cell>${cell} </#list><br /></#list><br /><#list seq?chunk(4, '-') as row><#list row as cell>${cell} </#list><br /></#list><h2>序列的第一個子變量</h2>${['你好', '我好', '他也好']?first}<h2>使用給定的分隔符來連接序列中的項為一個獨立的字符串</h2>[${["red", "green", "blue"]?join(", ")}]<br />[${["red", "green", "blue"]?join(", ", "-")}]<br />[${[]?join(", ", "-")}]<br />[${["red", "green", "blue"]?join(", ", "-", ".")}]<br />[${[]?join(", ", "-", ".")}]<h2>序列的最后一個子變量</h2>${['你好', '我好', '他也好']?last}<h2>序列的反序形式</h2>${['你好', '我好', '他也好']?reverse?join(", ")}<h2>辨別序列中是否包含指定值</h2>"blue": ${["red", 16, "blue", "cyan"]?seq_contains("blue")?string("yes", "no")}<br />"yellow": ${["red", 16, "blue", "cyan"]?seq_contains("yellow")?string("yes", "no")}<h2>返回序列中第一次出現該值時的索引位置</h2>"Joe"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("Joe")}<br />"lili"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("lili")}<br />從-2的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("Joe", -2)}<br />從-1的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("Joe", -1)}<br />從0的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("Joe", 0)}<br />從1的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("Joe", 1)}<br />從2的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("Joe", 2)}<br />從3的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("Joe", 3)}<br />從4的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中第一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_index_of("Joe", 4)}<h2>返回序列中最后一次出現值的索引位置</h2>"Joe"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("Joe")}<br />"lili"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("lili")}<br />從-2的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("Joe", -2)}<br />從-1的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("Joe", -1)}<br />從0的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("Joe", 0)}<br />從1的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("Joe", 1)}<br />從2的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("Joe", 2)}<br />從3的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("Joe", 3)}<br />從4的位置起, "Joe"在["Joe", "Fred", "Joe", "Susan"]中最后一次出現的位置: ${["Joe", "Fred", "Joe", "Susan"]?seq_last_index_of("Joe", 4)}<h2>序列中子變量的數量</h2>${['你好', '我好', '他也好']?size}<h2>以升序方式返回序列</h2>${['hello', 'world', 'thanks', 'word']?sort?join(", ")}<br />${[1, 22, 3, 1]?sort?join(", ")}<br />${['1900-05-12', '2021-05-12', '1999-01-01', '1900-12-21']?sort?join(", ")}<br />${['00:00:01', '12:00:01', '02:00:01', '12:59:01']?sort?join(", ")}<br />${['1900-05-12 00:00:01', '1999-05-12 12:00:01', '1900-05-12 10:00:01']?sort?join(", ")}</body> </html>11.6. 運行項目
總結
以上是生活随笔為你收集整理的013_序列内建函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 012_日期内建函数
- 下一篇: 014_哈希表内建函数