赞
踩
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.Map;
-
- import freemarker.template.Configuration;
- import freemarker.template.DefaultObjectWrapper;
- import freemarker.template.Template;
- import freemarker.template.TemplateException;
- import freemarker.template.TemplateExceptionHandler;
- import junit.framework.TestCase;
-
- public class FreemarkerTest extends TestCase {
-
- private String dir = "E:/.../OA/TestTotal/src/com/bjsxt/oa/freemarker";
-
- public void testFreemarker() {
- Configuration cfg = new Configuration();
-
- try {
- // 从哪里加载模板文件
- cfg.setDirectoryForTemplateLoading(new File(dir));
-
- // 定义模版的位置,从类路径中,相对于FreemarkerManager所在的路径加载模版
- // cfg.setTemplateLoader(new ClassTemplateLoader(FreemarkerManager.class, "templates"))
-
- // 设置对象包装器
- cfg.setObjectWrapper(new DefaultObjectWrapper());
-
- // 设置异常处理器
- cfg
- .setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
-
- // 定义数据模型
- Map root = new HashMap();
- root.put("abc", "世界,你好");
-
- // 通过freemarker解释模板,首先需要获得Template对象
- Template template = cfg.getTemplate("test.ftl");
-
- // 定义模板解释完成之后的输出
- PrintWriter out = new PrintWriter(new BufferedWriter(
- new FileWriter(dir+"/out.txt")));
-
- try {
- // 解释模板
- template.process(root, out);
- } catch (TemplateException e) {
- e.printStackTrace();
- }
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- <form action="./submit">
- <div>
- <label for="title">标题:</label>
- <input type="text" id="title" name="title"/>
- </div>
- <div>
- <label for="brief">摘要:</label>
- <input type="text" id="brief" name="brief"/>
- </div>
- <div>
- <label for="sex">性别:</label>
- <select id="sex" name="sex">
- <option value="0">男</option>
- <option value="1">女</option>
- </select>
- </div>
- <div>
- <label for="job">职业:</label>
- <select id="job" name="job">
- <option value="0">Java工程师</option>
- <option value="1">Net工程师</option>
- </select>
- </div>
- </form>
- ##表单模板
- ##@author fsjohnhuang
- ##@version 1.0
- ## 引入外部模板文件
- #parse('macro.vm')
- ## 主逻辑
- <form action="$action">
- #foreach($input in $inputs)
- #input($input.title $input.id)
- #end
- #foreach($select in $selects)
- #select($select.title $select.id $select.items)
- #end
- </form>
- ## 生成input表单元素区域的宏
- #macro(input $title $id)
- <div>
- <label for="$id">$title</label>
- <input type="text" id="$id" name="$id"/>
- </div>
- #end
- ## 生成select表单元素区域的宏
- #macro(select $title $id $items)
- <div>
- <label for="$id">$title</label>
- <select id="$id" name="$id">
- ## VTL指令紧贴左侧才能确保结果的排版正常(不会有多余空格)
- #foreach($key in $items.keySet())
- <option value="$key">$items.get($key)</option>
- #end
- </select>
- </div>
- #end
- public static void main(String[] args) {
- // 初始化模板引擎
- Properties props = new Properties();
- props.put("file.resource.loader.path", ".\\vm");
- VelocityEngine ve = new VelocityEngine(props);
- // 配置引擎上下文对象
- VelocityContext ctx = new VelocityContext();
- ctx.put("action", "./submit");
- ArrayList<HashMap<String, String>> inputs = new ArrayList<HashMap<String,String>>();
- HashMap<String, String> input1 = new HashMap<String, String>();
- input1.put("id", "title");
- input1.put("title", "标题:");
- inputs.add(input1);
- HashMap<String, String> input2 = new HashMap<String, String>();
- input2.put("id", "brief");
- input2.put("title", "摘要:");
- inputs.add(input2);
- ctx.put("inputs", inputs);
- ArrayList<HashMap<String, Object>> selects = new ArrayList<HashMap<String,Object>>();
- HashMap<String, Object> select1 = new HashMap<String, Object>();
- selects.add(select1);
- select1.put("id", "sex");
- select1.put("title", "性别:");
- HashMap<Integer, String> kv1 = new HashMap<Integer, String>();
- kv1.put(0, "男");
- kv1.put(1, "女");
- select1.put("items", kv1);
- HashMap<String, Object> select2 = new HashMap<String, Object>();
- selects.add(select2);
- select2.put("id", "job");
- select2.put("title", "职业:");
- HashMap<Integer, String> kv2 = new HashMap<Integer, String>();
- kv2.put(0, "Java工程师");
- kv2.put(1, "Net工程师");
- select2.put("items", kv2);
- ctx.put("selects", selects);
- // 加载模板文件
- Template t = ve.getTemplate("test.vm");
- StringWriter sw = new StringWriter();
- // 渲染模板
- t.merge(ctx, sw);
- System.out.print(sw.toString());
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。