国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
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)行這個(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) {
        /**
         * 我的一些測試輸出
         */
        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;
    }
}
以上就是兩個(gè)最簡單的velocity的例子。 一個(gè)是java application 一個(gè)是java web程序。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
CHAPTER 6 理解模板和上下文 - Velocity空間 - BlogJava
Velocity是如何工作的
Velocity之Web實(shí)踐- - - 劉文濤 - BlogJava
java 字符串模板解析
Velocity java開發(fā)指南
Velocity 的應(yīng)用示例3
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服