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

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

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

    posts - 78,  comments - 48,  trackbacks - 0
    Velocity 是一個基于 Java 的通用模板工具,來自于 jakarta.apache.org 。

    Velocity 的介紹請參考 Velocity -- Java Web 開發新技術。這里是它的一個應用示例。

    這個例子參照了 PHP-Nuke 的結構, 即所有 HTTP 請求都以 http://www.some.com/xxx/Modules?name=xxx&arg1=xxx&bbb=xxx 的形式進行處理。例子中所有文件都是 .java 和 .html , 沒有其他特殊的文件格式。除了 Modules.java 是 Java Servlet, 其余的 .java 文件都是普通的 Java Class.

    所有 HTTP 請求都通過 Modules.java 處理。Modules.java 通過 Velocity 加載 Modules.htm。 Modules.htm 有頁頭,頁腳,頁左導航鏈接,頁中內容幾個部分。其中頁頭廣告、頁中內容是變化部分。頁頭廣告由 Modules.java 處理,頁中內容部分由 Modules.java dispatch 到子頁面類處理。

    1) Modules.java

            import javax.servlet.*;
    import javax.servlet.http.*;
    import org.apache.velocity.*;
    import org.apache.velocity.context.*;
    import org.apache.velocity.exception.*;
    import org.apache.velocity.servlet.*;
    import commontools.*;

    public class Modules
      extends VelocityServlet {
      public Template handleRequest(HttpServletRequest request,
                     HttpServletResponse response,
                     Context context) {
        //init
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("utf-8");

        //prepare function page
        ProcessSubPage page = null;
        ProcessSubPage mainPage = new HomeSubPage();
        String requestFunctionName = (String) request.getParameter("name");
        boolean logined = false;

        String loginaccount = (String) request.getSession(true).getAttribute(
          "loginaccount");
        if (loginaccount != null) {
          logined = true;
        }

        //default page is mainpage
        page = mainPage;
        if (requestFunctionName == null||requestFunctionName.equalsIgnoreCase("home")) {
          page = mainPage;
        }

        //no login , can use these page
        else if (requestFunctionName.equalsIgnoreCase("login")) {
          page = new LoginProcessSubPage();
        }
        else if (requestFunctionName.equalsIgnoreCase("ChangePassword")) {
          page = new ChangePasswordSubPage();
        }
        else if (requestFunctionName.equalsIgnoreCase("ForgetPassword")) {
          page = new ForgetPassword();
        }
        else if (requestFunctionName.equalsIgnoreCase("about")) {
          page = new AboutSubPage();
        }
        else if (requestFunctionName.equalsIgnoreCase("contact")) {
          page = new ContactSubPage();
        }


        //for other page, need login first
        else if (logined == false) {
          page = new LoginProcessSubPage();
        }

        else if (requestFunctionName.equalsIgnoreCase("listProgram")) {
          page = new ListTransactionProgramSubPage();
        }
        else if (requestFunctionName.equalsIgnoreCase(
          "ViewProgramItem")) {
          page = new ViewTransactionProgramItemSubPage();
        }
        else if (requestFunctionName.equalsIgnoreCase(
          "UpdateProgramObjStatus")) {
          page = new UpdateTransactionProgramObjStatusSubPage();
        }
        else if (requestFunctionName.equalsIgnoreCase(
          "Search")) {
          page = new SearchSubPage();
        }

        //check if this is administrator
        else if (Utilities.isAdministratorLogined(request)) {
          //Utilities.debugPrintln("isAdministratorLogined : true");
          if (requestFunctionName.equalsIgnoreCase("usermanagement")) {
            page = new UserManagementSubPage();
          }
          else if (requestFunctionName.equalsIgnoreCase(
            "UploadFiles")) {
            page = new UploadFilesSubPage();
          }
          else if (requestFunctionName.equalsIgnoreCase(
            "DownloadFile")) {
            page = new DownloadFileSubPage();
          }
          else if (requestFunctionName.equalsIgnoreCase(
            "Report")) {
            page = new ReportSubPage();
          }

        }
        else {
          //no right to access.
          //Utilities.debugPrintln("isAdministratorLogined : false");
          page = null;
        }
        //Utilities.debugPrintln("page : " + page.getClass().getName());

        if(page != null){
          context.put("function_page",
                page.getHtml(this, request, response, context));
        }else{
          String msg = "Sorry, this module is for administrator only.You are not administrator.";
          context.put("function_page",msg);
        }
        
        context.put("page_header",getPageHeaderHTML());
        context.put("page_footer",getPageFooterHTML());

        Template template = null;
        try {
          template = getTemplate("/templates/Modules.htm"); //good
        }
        catch (ResourceNotFoundException rnfe) {
          Utilities.debugPrintln("ResourceNotFoundException 2");
          rnfe.printStackTrace();
        }
        catch (ParseErrorException pee) {
          Utilities.debugPrintln("ParseErrorException2 " + pee.getMessage());
        }
        catch (Exception e) {
          Utilities.debugPrintln("Exception2 " + e.getMessage());
        }

        return template;
      }

      /**
       * Loads the configuration information and returns that information as a Properties, e
       * which will be used to initializ the Velocity runtime.
       */
      protected java.util.Properties loadConfiguration(ServletConfig config) throws
        java.io.IOException, java.io.FileNotFoundException {
        return Utilities.initServletEnvironment(this);

      }

    }        
    2) ProcessSubPage.java , 比較簡單,只定義了一個函數接口 getHtml

         
      import javax.servlet.http.*;
    import org.apache.velocity.context.*;
    import org.apache.velocity.servlet.*;
    import commontools.*;

    public abstract class ProcessSubPage implements java.io.Serializable {
      public ProcessSubPage() {
      }

      public String getHtml(VelocityServlet servlet, HttpServletRequest request,
                 HttpServletResponse response,
                 Context context) {
        Utilities.debugPrintln(
          "you need to override this method in sub class of ProcessSubPage:"
          + this.getClass().getName());
        return "Sorry, this module not finish yet.";
      }

    }
      
         

    他的 .java 文件基本上是 ProcessSubPage 的子類和一些工具類。 ProcessSubPage 的子類基本上都是一樣的流程, 用類似
    context.put("page_footer",getPageFooterHTML());
    的寫法置換 .html 中的可變部分即可。如果沒有可變部分,完全是靜態網頁,比如 AboutSubPage, 就更簡單。

    3) AboutSubPage.java

         
      import org.apache.velocity.servlet.VelocityServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.velocity.context.Context;

    public class AboutSubPage extends ProcessSubPage {
      public AboutSubPage() {
      }

      public String getHtml(VelocityServlet servlet, HttpServletRequest request,
                 HttpServletResponse response, Context context) {
        //prepare data
        //context.put("xxx","xxxx");         
                 
        Template template = null;
        String fileName = "About.htm";
        try {
          template = servlet.getTemplate(fileName);
          StringWriter sw = new StringWriter();
          template.merge(context, sw);
          return sw.toString();
        }
        catch (Exception ex) {
          return "error get template " + fileName + " " + ex.getMessage();
        }
      }
    }
      
         

    其他 ProcessSubPage 的子類如上面基本類似,只不過會多了一些 context.put("xxx","xxxx") 的語句。

    通過以上的例子,我們可以看到,使用 Velocity + Servlet , 所有的代碼為: 1 個 java serverlet + m 個 java class + n 個 Html 文件。

    這里是用了集中處理,然后分發(dispatch)的機制。不用擔心用戶在沒有登陸的情況下訪問某些頁面。用戶驗證,頁眉頁腳包含都只寫一次,易于編寫、修改和維護。代碼比較簡潔,并且很容易加上自己的頁面緩沖功能??梢噪S意將某個頁面的 html 在內存中保存起來,緩存幾分鐘,實現頁面緩沖功能。成功、出錯頁面也可以用同樣的代碼封裝成函數,通過參數將 Message/Title 傳入即可。

    因為 Java 代碼與 Html 代碼完全在不同的文件中,美工與java代碼人員可以很好的分工,每個人修改自己熟悉的文件,基本上不需要花時間做協調工作。而用 JSP, 美工與java代碼人員共同修改維護 .jsp 文件,麻煩多多,噩夢多多。而且這里沒有用 xml ,說實話,懂 xml/xls 之類的人只占懂 Java 程序員中的幾分之一,人員不好找。

    因為所有 java 代碼人員寫的都是標準 Java 程序,可以用任何 Java 編輯器,調試器,因此開發速度也會大大提高。美工寫的是標準 Html 文件,沒有 xml, 對于他們也很熟悉,速度也很快。并且,當需要網站改版的時候,只要美工把 html 文件重新修飾排版即可,完全不用改動一句 java 代碼。

    爽死了!!

    4) 工具類 Utilities.java

         
      import java.io.*;
    import java.sql.*;
    import java.text.*;
    import java.util.*;
    import java.util.Date;
    import javax.naming.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import org.apache.velocity.*;
    import org.apache.velocity.app.*;
    import org.apache.velocity.context.Context;
    import org.apache.velocity.servlet.*;

    public class Utilities {
      private static Properties m_servletConfig = null;

      private Utilities() {
      }

      static {
        initJavaMail();
      }

      public static void debugPrintln(Object o) {
        String msg = "proj debug message at " + getNowTimeString() +
          " ------------- ";
        System.err.println(msg + o);
      }

      public static Properties initServletEnvironment(VelocityServlet v) {
        // init only once
        if (m_servletConfig != null) {
          return m_servletConfig;
        }

        //debugPrintln("initServletEnvironment....");

        try {
          /*
           * call the overridable method to allow the
           * derived classes a shot at altering the configuration
           * before initializing Runtime
           */
          Properties p = new Properties();

          ServletConfig config = v.getServletConfig();

          // Set the Velocity.FILE_RESOURCE_LOADED_PATH property
          // to the root directory of the context.
          String path = config.getServletContext().getRealPath("/");
          //debugPrintln("real path of / is : " + path);
          p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);

          // Set the Velocity.RUNTIME_LOG property to be the file
          // velocity.log relative to the root directory
          // of the context.
          p.setProperty(Velocity.RUNTIME_LOG, path +
                 "velocity.log");
    // Return the Properties object.
    //return p;

          Velocity.init(p);
          m_servletConfig = p;
          return p;
        }
        catch (Exception e) {
          debugPrintln(e.getMessage());
          //throw new ServletException("Error initializing Velocity: " + e);
        }
        return null;

        //this.getServletContext().getRealPath("/");
      }

      private static void initJavaMail() {
      }

      public static Connection getDatabaseConnection() {
        Connection con = null;
        try {
          InitialContext initCtx = new InitialContext();
          javax.naming.Context context = (javax.naming.Context) initCtx.
            lookup("java:comp/env");
          javax.sql.DataSource ds = (javax.sql.DataSource) context.lookup(
            "jdbc/TestDB");
          //Utilities.debugPrintln("ds = " + ds);
          con = ds.getConnection();
        }
        catch (Exception e) {
          Utilities.debugPrintln("Exception = " + e.getMessage());
          return null;
        }
        //Utilities.debugPrintln("con = " + con);
        return con;
      }

      public static java.sql.ResultSet excuteDbQuery(Connection con, String sql,
        Object[] parameters) {
        //Exception err = null;
        //Utilities.debugPrintln("excuteDbQuery" + parameters[0] + " ,sql=" + sql);

        try {
          java.sql.PreparedStatement ps = con.prepareStatement(sql);
          for (int i = 0; i < parameters.length; i++) {
            processParameter(ps, i + 1, parameters[i]);
          }
          return ps.executeQuery();
        }
        catch (Exception e) {
          //Utilities.debugPrintln(e.getMessage());
          e.printStackTrace();
        }
        return null;
      }

      public static void excuteDbUpdate(String sql, Object[] parameters) {
        Connection con = Utilities.getDatabaseConnection();
        excuteDbUpdate(con, sql, parameters);
        closeDbConnection(con);
      }

      public static void excuteDbUpdate(Connection con, String sql,
                       Object[] parameters) {
        Exception err = null;
        try {
          java.sql.PreparedStatement ps = con.prepareStatement(sql);
          for (int i = 0; i < parameters.length; i++) {
            processParameter(ps, i + 1, parameters[i]);
          }
          ps.execute();
        }
        catch (Exception e) {
          err = e;
          //Utilities.debugPrintln(err.getMessage());
          e.printStackTrace();
        }
      }

      private static void processParameter(java.sql.PreparedStatement ps,
                         int index, Object parameter) {
        try {
          if (parameter instanceof String) {
            ps.setString(index, (String) parameter);
          }
          else {
            ps.setObject(index, parameter);
          }
        }
        catch (Exception e) {
          //Utilities.debugPrintln(e.getMessage());
          e.printStackTrace();
        }
      }

      public static void closeDbConnection(java.sql.Connection con) {
        try {
          con.close();
        }
        catch (Exception e) {
          Utilities.debugPrintln(e.getMessage());
        }
      }

      public static String getResultPage(
        String title, String message, String jumpLink,
        VelocityServlet servlet, HttpServletRequest request,
        HttpServletResponse response, Context context) {

        Template template = null;

        context.put("MessageTitle", title);
        context.put("ResultMessage", message);
        context.put("JumpLink", jumpLink);

        try {
          template = servlet.getTemplate(
            "/templates/Message.htm");
          StringWriter sw = new StringWriter();
          template.merge(context, sw);
          return sw.toString();
        }
        catch (Exception ex) {
          return "error get template Message.htm " + ex.getMessage();
        }

      }

      public static String mergeTemplate(String fileName, VelocityServlet servlet,
                        Context context) {
        Template template = null;

        try {
          template = servlet.getTemplate(fileName);
          StringWriter sw = new StringWriter();
          template.merge(context, sw);
          return sw.toString();
        }
        catch (Exception ex) {
          return "error get template " + fileName + " " + ex.getMessage();
        }
      }

    }

      

    posted on 2006-03-08 17:21 黑咖啡 閱讀(470) 評論(0)  編輯  收藏 所屬分類: Velocity

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(2)

    隨筆分類(67)

    文章分類(43)

    Good Article

    Good Blogs

    Open Source

    最新隨筆

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲日本VA中文字幕久久道具| 久久久久无码精品亚洲日韩| 亚洲国产综合精品中文第一| 91精品免费国产高清在线| 亚洲网址在线观看| 在线观看av永久免费| 亚洲日韩精品无码AV海量| 国产一级做a爱免费视频| 午夜不卡AV免费| 亚洲精品成人无限看| 91青青青国产在观免费影视| 亚洲福利一区二区精品秒拍| 最近中文字幕免费mv视频7| 亚洲国产欧美一区二区三区| 亚洲精品无码久久毛片| a级日本高清免费看| 亚洲无砖砖区免费| 永久黄网站色视频免费观看 | 亚洲jjzzjjzz在线播放| 在线播放高清国语自产拍免费| 青青青亚洲精品国产| 永久亚洲成a人片777777| 久久99青青精品免费观看| 最新国产成人亚洲精品影院| 在线a亚洲v天堂网2018| a级毛片免费播放| 亚洲熟妇AV一区二区三区浪潮| 一区国严二区亚洲三区| 999久久久免费精品播放| 亚洲日韩亚洲另类激情文学| 久久久久无码专区亚洲av| 91青青国产在线观看免费| 亚洲第一街区偷拍街拍| 亚洲精品无码专区久久久| www.黄色免费网站| 香蕉免费一级视频在线观看| 亚洲国产精品线观看不卡| 亚洲精品国产福利一二区| 亚洲一级毛片免费在线观看| 特级做a爰片毛片免费看| 亚洲一区二区三区在线|