eclipse開發velocity執行個體(初學)

來源:互聯網
上載者:User

標籤:

開發環境

        Eclipse Java EE IDE for Web Developers.(Version: Helios Service Release 1)

        jdk1.6.0_07

        apache-tomcat-6.0.10

        首先需要為eclipse安裝velocity編輯外掛程式:

                在Eclipse的Help->Install New Software...

                點擊“Add加 Veloeclipse,值:http://veloeclipse.googlecode.com/svn/trunk/update/

                備忘:如果無法安裝,在安裝外掛程式的介面中把 Group Items by Catagory 前打鉤去掉

建立項目

        開啟eclipse,File -> New -> Other... -> Web -> Dynamic Web Projec  之後按照提示一路next;

        注意1:修改代碼檔案輸出路徑,預設是build\classes;這個不符合tomcat的要求,需要改成WebContent\WEB-INF\classes 如下:

        

        注意2:勾選 Generate web.xml deployment descriptor

        

 

        添加velocity依賴包

                velocity相關的包有十多個,下載解壓後複製全部,粘貼到項目WebContent -> WEB-INF ->lib檔案夾中

                項目名右鍵菜單選擇build path -> configure build path ->java build path ->libraries -> add jars 選擇項目lib檔案夾,選擇全部jar最後點確認按鈕

                備忘:velocity所需的jar包 http://download.csdn.net/detail/smilevt/4802244

        添加velocity模板檔案

                在WebContent下添加一個檔案夾,我起名叫templates。在該檔案下添加一個hello.vm檔案,裡面內容如下:
                <html>
                        <head><title>Sample velocity page</title></head>
                        <body bgcolor="#ffffff">
                                <center>
                                        <h2>Hello My First Velocity</h2>
                                        <table width="100" cellpadding="5" cellspacing="1" bordercolor="#333333">
                                                <tr><td bgcolor="#eeeeee" align="center">name list</td></tr>
                                                #foreach ($name in $theList)
                                                <tr><td bgcolor="#6666FF" align="center">$name</td></tr>
                                                #end
                                        </table>
                                </center>
                        </body>
                </html>

 

        添加java類檔案

                在Java Resource -> src下建立一個包,包名com.velocitydemo.velocityhandler,名稱隨意,後面設定檔裡要用到,前後需要一致

                在該包下添加一個類,類名HelloHandler,名稱隨意,道理同上

                類的代碼如下:

                package com.velocitydemo.velocityhandler;
                import java.util.Properties;
                import java.util.Vector;
                import javax.servlet.ServletException;
                import javax.servlet.http.HttpServletRequest;
                import javax.servlet.http.HttpServletResponse;
                import org.apache.velocity.Template;
                import org.apache.velocity.app.Velocity;
                import org.apache.velocity.app.VelocityEngine;
                import org.apache.velocity.context.Context;
                import org.apache.velocity.tools.view.VelocityViewServlet;
                public class HelloHandler extends VelocityViewServlet {
                        private static final long serialVersionUID = 1L;
                        private VelocityEngine velo;
                        public void init() throws ServletException {
                                velo = new VelocityEngine();// velocity引擎對象
                                Properties prop = new Properties();// 設定vm模板的裝載路徑
                                String path = this.getServletContext().getRealPath("/");
                                prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path + "templates/");
                                try {
                                        velo.init(prop);// 初始化設定,下面用到getTemplate("*.vm")輸出時;一定要調用velo對象去做,即velo.getTemplate("*.vm")
                                } catch (Exception e1) {
                                        e1.printStackTrace();
                                }
                        }
                        protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) {
                                String p1 = "Charles";
                                String p2 = "Michael";
                                Vector personList = new Vector();
                                personList.addElement(p1);
                                personList.addElement(p2);
                                ctx.put("theList", personList); // 將模板資料 list放置到上下文環境context中
                                Template template = velo.getTemplate("hello.vm");
                                return template;
                        }
                }               


        配置web.xml

                開啟WEB-INF下的web.xml檔案,內容如下:

                <?xml version="1.0" encoding="UTF-8"?>

                <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                   xmlns="http://java.sun.com/xml/ns/javaee"
                                   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                                   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
                                   id="WebApp_ID" version="3.0">

                        <display-name>velocity-demo</display-name>

                        <servlet>
                                <servlet-name>velocity</servlet-name>
                                <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
                        </servlet>
                        <servlet-mapping>
                                <servlet-name>velocity</servlet-name>
                                <url-pattern>*.vm</url-pattern>
                        </servlet-mapping>

                        <servlet>
                                <servlet-name>hello</servlet-name>
                                <servlet-class>com.velocitydemo.velocityhandler.HelloHandler</servlet-class>
                        </servlet>
                        <servlet-mapping>
                                <servlet-name>hello</servlet-name>
                                <url-pattern>/hello</url-pattern>
                        </servlet-mapping>

                        <welcome-file-list>
                                <welcome-file>/hello</welcome-file>
                        </welcome-file-list>

                </web-app>

 

        至此一個最簡單的velocity項目建立完畢

                注意:WEB-INF下的classes檔案夾並不顯示在項目結構圖中

                

        發布項目

                到這裡可以把專案檔夾中的WebContent發布到Tomcat中進行測試了

                發布的方式參考地址(http://blog.csdn.net/smilevt/article/details/8212075)

 

        啟動tomcat訪問一個最簡單的velocity項目

                

eclipse開發velocity執行個體(初學)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.