<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 叮當小馬 閱讀(4906) 評論(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
      回復  更多評論    

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


    網站導航:
     
    主站蜘蛛池模板: jizz日本免费| 国产高潮流白浆喷水免费A片 | 91精品啪在线观看国产线免费| 婷婷亚洲综合一区二区| 精品一区二区三区免费| 亚洲国产成人一区二区三区| 91视频免费观看高清观看完整| 成年私人影院免费视频网站| 国产亚洲精品资源在线26u| 老司机69精品成免费视频| 色婷婷亚洲十月十月色天| 亚洲黄色免费网址| 国产亚洲色视频在线| 中国好声音第二季免费播放| 亚洲国产精品无码久久一线 | 日本视频免费观看| 国产亚洲精品AA片在线观看不加载| 国产精品免费在线播放| 亚洲AV无码专区电影在线观看 | 人人揉揉香蕉大免费不卡| 国产精品国产午夜免费福利看| 亚洲精品无码专区久久久| 久久国产乱子免费精品| 亚洲视频无码高清在线| 免费a级毛片18以上观看精品| 国产日韩在线视频免费播放| 亚洲一区二区电影| 在线观看人成网站深夜免费| av片在线观看永久免费| 亚洲精品人成在线观看| 成人片黄网站色大片免费观看APP| 久久99亚洲网美利坚合众国| 成人黄页网站免费观看大全| 久久免费观看视频| 亚洲成人激情小说| 在线观看亚洲天天一三视| 大片免费观看92在线视频线视频| 亚洲国产精品第一区二区 | 国产亚洲日韩一区二区三区| 美女内射毛片在线看免费人动物| 国产亚洲人成在线影院|