本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

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.        }     }}


阅读全文(4789) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.641 second(s), page refreshed 144818315 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号