« | October 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9722581 建立时间:2004年12月20日 |

| |
[template engine]使用jelly和jexl 是痛苦的事情 原创空间, 软件技术, 电脑与网络
邢红瑞 发表于 2006/7/31 15:10:15 |
最初这个tatan框架生成sql的部分,后来准备用于utm的时候,突然没用上,贡献出来。jelly是个基于xml的脚本语言,jexl是一种很好的表达式语言,但是文档写的不行,以后使用velocity和freemaker算了。先下载commons-jelly-1.0.jarcommons-logging-1.0.4.jardom4j-1.6.1.jarcommons-beanutils-1.7.0.jarcommons-collections-3.1.jarcommons-jexl-1.1.jarJellyScript的实现import java.io.*;import java.util.*;import java.util.logging.*;import org.apache.commons.jelly.JellyContext;import org.apache.commons.jelly.Script;import org.apache.commons.jelly.XMLOutput;
import org.xml.sax.*;
public class JellyScript {
protected final static Logger logger = Logger.getLogger(JellyScript.class.getName());
protected final static JellyContext context = new JellyContext();
protected final Script script;
public JellyScript(String text) throws Exception { if (logger.isLoggable(Level.FINE)) logger.fine("setText():text=" + text);
final StringBuffer sb = new StringBuffer(); sb.append("<jelly:jelly trim=\"false\" xmlns:jelly=\"jelly:core\">"); sb.append(text); sb.append("</jelly:jelly>");
final StringReader stringReader = new StringReader(sb.toString()); final InputSource inputSource = new InputSource(stringReader); inputSource.setPublicId("http://localhost/"); inputSource.setSystemId("http://localhost/");
this.script = context.compileScript(inputSource); }
public String execute(Map<String, Object> variables) throws Exception { final StringWriter stringWriter = new StringWriter(); execute(variables, stringWriter, false); return stringWriter.toString(); }
public synchronized void execute(Map<String, Object> variables, Writer writer, boolean escapeText) throws Exception { if (logger.isLoggable(Level.FINE)) logger.fine("execute():variables=" + variables + ",writer=" + writer + ",escapeText=" + escapeText); final XMLOutput output = XMLOutput.createXMLOutput(writer, escapeText); try { if (this.script!=null) { final JellyContext jellyContext = context.newJellyContext(); if (variables!=null) jellyContext.setVariables(variables); this.script.run(jellyContext, output); } output.flush(); } finally { output.close(); } }
}
程序运行例子import java.util.HashMap;import java.util.Map;
/** * Created by IntelliJ IDEA. * User: hongruixing * Date: 2006-7-15 * Time: 16:12:45 * To change this template use File | Settings | File Templates. */public class SQlTest { public static void main(String[] args) { String sql = "SELECT * \n" + "FROM Employee \n" + "WHERE State='${State}' \n" + " <jelly:if test='${!empty City}'> \n" + " AND City='${City}' \n" + " </jelly:if> ";
try { final JellyScript jellyScript = new JellyScript(sql); // Create the variables needed by the template final Map<String, Object> variables = new HashMap<String, Object>(); variables.put("State", "Kent"); // variables.put("City", "kent13600"); String result = jellyScript.execute(variables); System.out.println(result); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. }
}} |
|
|