016_循环变量内建函数
1. 循環變量內建函數counter、has_next、index、is_even_item、is_first、is_last、is_odd_item、item_cycle、item_parity和item_parity_cap只能用于list和items指令的循環變量。
2. index
2.1. 返回當前迭代(由循環變量名稱識別)從0開始的索引。
<#list ['a', 'b', 'c', 'd'] as word>${word?index}: ${word}<br /> </#list>3. counter
3.1. 返回當前迭代(由循環變量名稱識別)從1開始的索引。
3.2. 當list指令不指定循環變量時, 這些內建函數就作用于items指令的循環變量。
<#list ['a', 'b', 'c', 'd']><#items as word>${word?counter}: ${word}<br /></#items> </#list>4. has_next
4.1. 辨別循環項是否是當前迭代(由循環變量名稱識別)的最后一項。
5. is_even_item
5.1. 辨別循環項是否是當前迭代(由循環變量名稱識別)間隔1的奇數項。
6. is_first
6.1. 辨別循環項是否是當前迭代(由循環變量名稱識別)的第一項。
7. is_last
7.1. 辨別循環項是否是當前迭代(由循環變量名稱識別)的最后一項。
8. is_odd_item
8.1. 辨別循環項是否是當前迭代(由循環變量名稱識別)間隔1的偶數項。
9. item_parity
9.1. 基于當前迭代(由循環變量名稱識別)間隔為1的索引的奇偶性, 返回字符串值"odd"或"even"。這通常用于表格中行間的顏色變換。
10. item_parity_cap
10.1. 基于當前迭代(由循環變量名稱識別)間隔為1的索引的奇偶性, 返回字符串值"Odd"或"Even"(請注意大寫)。
11. item_cycle
11.1. 這是item_parity內建函數更為通用的版本, 這里可以指定何值來代替"odd"和"even"。它也允許多余兩個值來循環。
12. switch獨立類型內建函數
12.1. 它的通用版本就像matchedValue?switch(case1, result1, case2, result2, ... caseN, resultN, defaultResult), 這里的defaultResult可以被忽略。
12.2. switch會找到第一個case和參數(從左到右)值matchedValue相等, 之后返回直接在case參數后的result參數的值, 如果它沒有找到一個相等的case, 那么就返回defaultResult的值, 如果沒有defaultResult參數(換言之, 參數的個數是基數), 那么就發生錯誤中止模板處理。
12.3. matchedValue和case參數值的比較, 就像==操作符。那就只比較標量并且是相同類型的值。
12.4. 對case參數值的類型沒有任何限制, 它們可以是字符串, 或數字, 布爾, 或日期等。但因為==操作符的特性, 那么在相同的switch中使用不同類型的case參數是沒有意義的。
12.5. case參數表達式不需要是常量值, 它們可以是任意復雜的表達式。當然result, defaultResult和matchedValue也是相同的。
12.6. 不像普通的方法調用, switch(...)的那些參數被評估為確實需要的。比如: 在two()?switch(c1(), r1(), c2(), r2(), c3(), r3())中, 如果two()返回2, c1()返回1, 且c2()返回2, 那么只有下面的函數會被調用, 而且順序是這樣c1(), c2(), r2()。它保證了case參數表達式被從左到右進行評估, 直到第一個匹配項被找到。它也保證了只有屬于第一個匹配case的result表達式會被評估。它還保證了如果沒有匹配的case參數, 那么defaultResult表達式會被評估。
13. 例子
13.1. 新建一個名為FMBuiltInsForLoop的動態Web工程, 同時添加相關jar包。
13.2. 編寫FMFactory.java
package com.fm.util;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.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);map.put(path, cfg);return cfg;}}13.3. 編寫BuiltInsForLoop.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 BuiltInsForLoop 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("builtinsforloop.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);} }13.4. 修改web.xml
13.5. 在/WEB-INF/templates下編寫builtinsforloop.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>循環變量內建函數</title><style type="text/css">b {font-size: 32px;}.odd {color: #009688;}.even {color: #FFB800;}.Odd {color: #5FB878;}.Even {color: #FF5722;}.first {color: #393D49;}.second {color: #01AAED;}.third {color: #FFB800;}.fourth {color: #FF5722;}</style></head><body><h2>index</h2><#list ['a', 'b', 'c', 'd'] as word>${word?index}: ${word}<br /></#list><h2>counter</h2><#list ['a', 'b', 'c', 'd']><#items as word>${word?counter}: ${word}<br /></#items></#list><h2>has_next</h2><#list ['a', 'b', 'c', 'd'] as word>${word}<#if word?has_next>,<#else>.</#if></#list><h2>is_even_item</h2><#list ['a', 'b', 'c', 'd'] as word><#if word?is_even_item>${word?index}: ${word}<br /></#if></#list><h2>is_first</h2><#list ['a', 'b', 'c', 'd'] as word><#if word?is_first>${word}</#if></#list><h2>is_last</h2><#list ['a', 'b', 'c', 'd'] as word><#if word?is_last>${word}</#if></#list><h2>is_odd_item</h2><#list ['a', 'b', 'c', 'd'] as word><#if word?is_odd_item>${word?index}: ${word}<br /></#if></#list><h2>item_parity</h2><#list ['a', 'b', 'c', 'd'] as word><b class="${word?item_parity}">${word}</b></#list><h2>item_parity_cap</h2><#list ['a', 'b', 'c', 'd'] as word><b class="${word?item_parity_cap}">${word}</b></#list><h2>item_cycle</h2><#list ['a', 'b', 'c', 'd'] as word><b class="${word?item_cycle('first', 'second', 'third', 'fourth')}">${word}</b></#list><h2>switch</h2><#list ['r', 'w', 'x', 's'] as flag>${flag?switch('r', 'readable', 'w' 'writable', 'x', 'executable', 'unknown flag: ' + flag)}<br /></#list></body> </html>13.6. 運行項目
總結
以上是生活随笔為你收集整理的016_循环变量内建函数的全部內容,希望文章能夠幫你解決所遇到的問題。