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

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

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

    數(shù)據(jù)加載中……
    velocity 例子
    最簡單的hello world 程序。
    基礎(chǔ)搭建:從網(wǎng)上下載velocity.zip,把lib下的.jar文件導(dǎo)入到工程中。另外還要導(dǎo)入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());
        }
    }
    直接運(yùn)行這個程序,就可以輸入模板中的信息。
    2。web程序,servlet 調(diào)用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 叮當(dāng)小馬 閱讀(4906) 評論(3)  編輯  收藏 所屬分類: Velocity

    評論

    # re: velocity 例子 2008-04-15 15:27 叮當(dāng)小馬

    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();

    }
      回復(fù)  更多評論    

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

    o09;i
      回復(fù)  更多評論    

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

    ukrkrkrkr
      回復(fù)  更多評論    

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产精品无码第一区二区三区| 国产亚洲无线码一区二区| 国产成人亚洲精品| 亚洲Av永久无码精品三区在线 | 99久久99久久精品免费观看| 曰批全过程免费视频网址| 亚洲αv久久久噜噜噜噜噜| 免费观看91视频| 亚洲AV日韩AV鸥美在线观看| 亚洲乱码国产乱码精华| 四虎永久在线精品免费影视| 牛牛在线精品免费视频观看| 99爱在线观看免费完整版| 亚洲黄网在线观看| 无码国产精品久久一区免费| 亚洲国产精品无码久久九九大片| 亚洲第一福利网站在线观看| 亚洲综合国产精品| h在线看免费视频网站男男| 成熟女人特级毛片www免费| 亚洲乱码日产精品一二三| 国产成人精品日本亚洲专区| 亚洲AV无码成人网站在线观看| 国产免费久久精品久久久| 中文在线观看国语高清免费| 99久久亚洲精品无码毛片| 四虎成人免费大片在线| 亚洲视频小说图片| 夜夜爽免费888视频| 一级一级毛片免费播放| 免费a级毛片永久免费| 亚洲人成网站色7799| 亚洲国产aⅴ综合网| 久久aⅴ免费观看| 亚洲国产成人精品无码区花野真一| 亚洲国产精品成人AV无码久久综合影院| 永久免费av无码网站yy| 中文字幕亚洲综合久久男男| 一本久久免费视频| 亚洲毛片不卡av在线播放一区| 国产成人精品免费久久久久|