freemarker 自定義freeMarker標籤
阿新 • • 發佈:2018-12-24
Java程式碼
- import java.io.IOException;
- import java.io.Writer;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import freemarker.core.Environment;
- import freemarker.ext.beans.ArrayModel;
- import freemarker.template.ObjectWrapper;
- import freemarker.template.SimpleCollection;
- import freemarker.template.SimpleNumber;
- import freemarker.template.SimpleSequence;
- import freemarker.template.TemplateBooleanModel;
- import freemarker.template.TemplateDirectiveBody;
- import freemarker.template.TemplateDirectiveModel;
- import freemarker.template.TemplateException;
- import freemarker.template.TemplateModel;
- import freemarker.template.TemplateModelException;
- import freemarker.template.TemplateNumberModel;
- public class GetNextDirective implements TemplateDirectiveModel {
- private static final String PARAM_SOURCELIST = "sourceList";
- private static final String PARAM_INDEX = "index";
- /* (non-Javadoc)
- * @see freemarker.template.TemplateDirectiveModel#execute(freemarker.core.Environment, java.util.Map, freemarker.template.TemplateModel[], freemarker.template.TemplateDirectiveBody)
- */
- public void execute(Environment env, Map params, TemplateModel[] loopVars,
- TemplateDirectiveBody body) throws TemplateException, IOException {
- // ---------------------------------------------------------------------
- // 處理引數
- List sourceListParam = null;
- int indexParam = 0;
- Iterator paramIter = params.entrySet().iterator();
- while (paramIter.hasNext()) {
- Map.Entry ent = (Map.Entry) paramIter.next();
- String paramName = (String) ent.getKey();
- TemplateModel paramValue = (TemplateModel) ent.getValue();
- if (paramName.equals(PARAM_SOURCELIST)) {
- // if (!(paramValue instanceof TemplateNumberModel)) {
- // throw new TemplateModelException("The \"" + PARAM_NAME_HR
- // + "\" parameter " + "must be a number.");
- // }
- sourceListParam = (List) ((SimpleSequence) paramValue).toList();
- // if (countParam < 0) {
- // throw new TemplateModelException("The \"" + PARAM_NAME_HR
- // + "\" parameter " + "can't be negative.");
- // }
- } else if (paramName.equals(PARAM_INDEX)) {
- // if (!(paramValue instanceof TemplateBooleanModel)) {
- // throw new TemplateModelException("The \"" + PARAM_NAME_HR
- // + "\" parameter " + "must be a boolean.");
- // }
- indexParam = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
- } else {
- throw new TemplateModelException("Unsupported parameter: "
- + paramName);
- }
- }
- if (loopVars.length > 1) {
- throw new TemplateModelException(
- "At most one loop variable is allowed.");
- }
- // Yeah, it was long and boring...
- // ---------------------------------------------------------------------
- // 真正開始處理輸出內容
- Writer out = env.getOut();
- // if (body != null) {
- Object data = sourceListParam.get(indexParam);
- // out.write(data.toString());
- env.setVariable("obj", ObjectWrapper.DEFAULT_WRAPPER.wrap(data));
- if (body != null) {
- body.render(env.getOut());
- }
- // for (int i = 0; i < countParam; i++) {
- // // 輸出 <hr> 如果 引數hr 設定為true
- // if (hrParam && i != 0) {
- // out.write("<hr>");
- // }
- //
- // // 設定迴圈變數
- // if (loopVars.length > 0) {
- // loopVars[0] = new SimpleNumber(i + 1);
- // }
- //
- // // 執行標籤內容(same as <#nested> in FTL).
- // body.render(env.getOut());
- // }
- // }
- }
- }
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import freemarker.core.Environment;
import freemarker.ext.beans.ArrayModel;
import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleCollection;
import freemarker.template.SimpleNumber;
import freemarker.template.SimpleSequence;
import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNumberModel;
public class GetNextDirective implements TemplateDirectiveModel {
private static final String PARAM_SOURCELIST = "sourceList";
private static final String PARAM_INDEX = "index";
/* (non-Javadoc)
* @see freemarker.template.TemplateDirectiveModel#execute(freemarker.core.Environment, java.util.Map, freemarker.template.TemplateModel[], freemarker.template.TemplateDirectiveBody)
*/
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
// ---------------------------------------------------------------------
// 處理引數
List sourceListParam = null;
int indexParam = 0;
Iterator paramIter = params.entrySet().iterator();
while (paramIter.hasNext()) {
Map.Entry ent = (Map.Entry) paramIter.next();
String paramName = (String) ent.getKey();
TemplateModel paramValue = (TemplateModel) ent.getValue();
if (paramName.equals(PARAM_SOURCELIST)) {
// if (!(paramValue instanceof TemplateNumberModel)) {
// throw new TemplateModelException("The \"" + PARAM_NAME_HR
// + "\" parameter " + "must be a number.");
// }
sourceListParam = (List) ((SimpleSequence) paramValue).toList();
// if (countParam < 0) {
// throw new TemplateModelException("The \"" + PARAM_NAME_HR
// + "\" parameter " + "can't be negative.");
// }
} else if (paramName.equals(PARAM_INDEX)) {
// if (!(paramValue instanceof TemplateBooleanModel)) {
// throw new TemplateModelException("The \"" + PARAM_NAME_HR
// + "\" parameter " + "must be a boolean.");
// }
indexParam = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
} else {
throw new TemplateModelException("Unsupported parameter: "
+ paramName);
}
}
if (loopVars.length > 1) {
throw new TemplateModelException(
"At most one loop variable is allowed.");
}
// Yeah, it was long and boring...
// ---------------------------------------------------------------------
// 真正開始處理輸出內容
Writer out = env.getOut();
// if (body != null) {
Object data = sourceListParam.get(indexParam);
// out.write(data.toString());
env.setVariable("obj", ObjectWrapper.DEFAULT_WRAPPER.wrap(data));
if (body != null) {
body.render(env.getOut());
}
// for (int i = 0; i < countParam; i++) {
// // 輸出 <hr> 如果 引數hr 設定為true
// if (hrParam && i != 0) {
// out.write("<hr>");
// }
//
// // 設定迴圈變數
// if (loopVars.length > 0) {
// loopVars[0] = new SimpleNumber(i + 1);
// }
//
// // 執行標籤內容(same as <#nested> in FTL).
// body.render(env.getOut());
// }
// }
}
}
Java程式碼
- <#assign x = 1>
- 一個引數:
- <@repeat count=4>
- Test ${x}
- <#assign x = x + 1>
- </@repeat>
- 二個引數:
- <@repeat count=3 hr=true>
- Test
- </@repeat>
- 迴圈變數:
- <@repeat count=3; cnt>
- ${cnt}. Test
- </@repeat>
- <#assign tt = ['1','2','3']/>
- <#list sslist as x>
- <#if x_has_next>
- <@getNext sourceList=sslist index=x_index+1>
- id=${obj.id}
- name=${obj.name}
- </@getNext>
- </#if>
- </#list>
- <#--
- <@getNext sourceList=sslist index=1>
- id=${obj.id}
- name=${obj.name}
- </@getNext>
- -->
<#assign x = 1>
一個引數:
<@repeat count=4>
Test ${x}
<#assign x = x + 1>
</@repeat>
二個引數:
<@repeat count=3 hr=true>
Test
</@repeat>
迴圈變數:
<@repeat count=3; cnt>
${cnt}. Test
</@repeat>
<#assign tt = ['1','2','3']/>
<#list sslist as x>
<#if x_has_next>
<@getNext sourceList=sslist index=x_index+1>
id=${obj.id}
name=${obj.name}
</@getNext>
</#if>
</#list>
<#--
<@getNext sourceList=sslist index=1>
id=${obj.id}
name=${obj.name}
</@getNext>
-->