<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    數據加載中……
    velocity 例子
    最簡單的hello world 程序。
    基礎搭建:從網上下載velocity.zip,把lib下的.jar文件導入到工程中。另外還要導入velocity.jar 和velocity-dep.jar.
    hellosite.vm  放到工程的目錄底下。
    Hello $name! Welcome to $site world!
    HelloWorld.java
    package com;
    import java.io.StringWriter;
    import org.apache.velocity.Template;
    import org.apache.velocity.VelocityContext;
    import org.apache.velocity.app.VelocityEngine;
    public class HelloWorld {
        
    public static void main(String[] args) throws Exception {
            
    /* first, get and initialize an engine */
            VelocityEngine ve 
    = new VelocityEngine();
            ve.init();
            
    /* next, get the Template */
            Template t 
    = ve.getTemplate("hellosite.vm");
            
    /* create a context and add data */
            VelocityContext context 
    = new VelocityContext();
            context.put(
    "name""DingDangXiaoMa");
            context.put(
    "site""http://www.sun.com");
            
    /* now render the template into a StringWriter */
            StringWriter writer 
    = new StringWriter();
            t.merge(context, writer);
            
    /* show the World */
            System.out.println(writer.toString());
        }
    }
    直接運行這個程序,就可以輸入模板中的信息。
    2。web程序,servlet 調用vm顯示信息。
    hello.vm 放在webRoot/web/下。
    <html>
        
    <body >        
         #set($name
    ="Hello ! DingDang")
            Hello, $name 
            woowoo
        
    </body>
    </html>
    servlet 文件:
    public class SampleServlet extends VelocityServlet {
        
    protected Properties loadConfiguration(ServletConfig config)
                
    throws IOException, FileNotFoundException {
            Properties p 
    = new Properties();
            String path 
    = config.getServletContext().getRealPath("/");
            
    if (path == null) {
                System.out
                        .println(
    " SampleServlet.loadConfiguration() : unable to "
                                
    + "get the current webapp root.  Using '/'. Please fix.");
                path 
    = "/";
            }
            p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
            p.setProperty(
    "runtime.log", path + "velocity.log");
            
    return p;
        }
        
    public Template handleRequest(HttpServletRequest request,
                HttpServletResponse response, Context ctx) {
            
    /**
             * 我的一些測試輸出
             
    */
            System.out.println(
    "Hello  everyone");
            System.out.println(
    "sfdsfsd");
            System.out.println(ctx);
            System.out.println(ctx.toString());
            System.out.println(request.getContextPath());
            Template template 
    = null;

            
    try {
                
    /**
                 * 主要代碼
                 
    */
                Velocity.init();
                
    /**
                 * 注意相對路徑
                 
    */
                template 
    = Velocity.getTemplate("/web/hello.vm");
                VelocityContext context 
    = new VelocityContext();
                context.put(
    "name""wowowowowowo  hello   hello  hello");
                
    // template = Velocity.getTemplate("/web/sample.html");
            } catch (Exception e) {
                e.printStackTrace();
            }
            
    return template;
        }
    }
    以上就是兩個最簡單的velocity的例子。 一個是java application 一個是java web程序。
    參考資料:
    http://www.javaresearch.org/article/38895.htm

    posted on 2008-04-15 15:01 叮當小馬 閱讀(4904) 評論(3)  編輯  收藏 所屬分類: Velocity

    評論

    # re: velocity 例子 2008-04-15 15:27 叮當小馬

    java application 還有一種寫法。
    public static void main(String[] args) throws Exception {

    Velocity.init();

    VelocityContext context = new VelocityContext();

    context.put("name", "yy");

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    Template template = Velocity.getTemplate("hello.vm");

    template.merge(context, writer);

    writer.flush();

    writer.close();

    }
      回復  更多評論    

    # re: velocity 例子 2011-07-08 11:17 uo;

    o09;i
      回復  更多評論    

    # re: velocity 例子 2011-07-08 11:17 uo;

    ukrkrkrkr
      回復  更多評論    

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 少妇亚洲免费精品| 亚洲日韩精品无码专区| 一级毛片免费视频网站| 成年女人永久免费观看片| 亚洲色大成WWW亚洲女子| 成人免费无码大片A毛片抽搐色欲 成人免费无码大片a毛片 | 国产情侣激情在线视频免费看| 亚洲av无码专区在线播放| 日本在线看片免费| 日韩中文字幕在线免费观看| 久久精品国产亚洲av瑜伽| 国产日产成人免费视频在线观看| 国产精品亚洲va在线观看| 亚洲人成无码网站久久99热国产| 国产免费牲交视频免费播放 | 久九九精品免费视频| 亚洲AV无码一区二区三区在线| 波多野结衣在线免费观看| 最新亚洲卡一卡二卡三新区| 免费一级毛片在级播放| 皇色在线免费视频| 亚洲黄色三级网站| 国产成人免费a在线资源| 国产特黄一级一片免费 | 鲁死你资源站亚洲av| 亚洲乱码国产一区网址| 亚洲欧美日韩中文高清www777| 国产a级特黄的片子视频免费| 亚欧乱色国产精品免费视频| 91大神亚洲影视在线| 国产精品无码一二区免费| 成人妇女免费播放久久久| 亚洲电影免费观看| 免费很黄很色裸乳在线观看| AAA日本高清在线播放免费观看| 亚洲一级黄色大片| 国产成人毛片亚洲精品| 91麻豆最新在线人成免费观看| 美女扒开尿口给男人爽免费视频 | 久久精品毛片免费观看| 亚洲av日韩综合一区久热|