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

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

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

    數(shù)據(jù)加載中……
    velocity 例子
    最簡(jiǎn)單的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)行這個(gè)程序,就可以輸入模板中的信息。
    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) {
            
    /**
             * 我的一些測(cè)試輸出
             
    */
            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();
                
    /**
                 * 注意相對(duì)路徑
                 
    */
                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;
        }
    }
    以上就是兩個(gè)最簡(jiǎn)單的velocity的例子。 一個(gè)是java application 一個(gè)是java web程序。
    參考資料:
    http://www.javaresearch.org/article/38895.htm

    posted on 2008-04-15 15:01 叮當(dāng)小馬 閱讀(4905) 評(píng)論(3)  編輯  收藏 所屬分類(lèi): Velocity

    評(píng)論

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

    java application 還有一種寫(xiě)法。
    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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲精品制服丝袜四区| 免费一级毛片在播放视频| 亚洲福利视频一区| a视频在线观看免费| 亚洲人成中文字幕在线观看| 一本到卡二卡三卡免费高| 337p日本欧洲亚洲大胆裸体艺术| 手机永久免费的AV在线电影网| 亚洲国产精品碰碰| 中文字字幕在线高清免费电影| 久久亚洲综合色一区二区三区| 午夜爽爽爽男女免费观看影院 | 免费国产在线观看| 免费看黄网站在线看| 久久久青草青青国产亚洲免观 | 久久精品国产99国产精品亚洲| 处破痛哭A√18成年片免费| 亚洲AV日韩AV无码污污网站| 亚洲免费视频一区二区三区| 男女作爱在线播放免费网站| 亚洲国产精品无码久久久| 永久免费看bbb| 国产激情久久久久影院老熟女免费 | 亚洲国产精品激情在线观看 | 性感美女视频免费网站午夜 | 一区二区三区无码视频免费福利| 亚洲av激情无码专区在线播放| 亚洲精品视频免费在线观看| 亚洲av无码专区在线观看亚| 亚洲中文字幕丝袜制服一区| 99久热只有精品视频免费观看17| 亚洲视频无码高清在线| 亚洲成aⅴ人片久青草影院| 国产真人无码作爱视频免费 | 亚洲乱码中文字幕综合| 国产人成免费视频网站| 免费国产在线精品一区| 亚洲系列中文字幕| 亚洲国模精品一区| 日韩不卡免费视频| sihu国产精品永久免费|