`

Velocity实例

阅读更多

Velocity实例
http://www.moon-soft.com/doc/34224.htm

 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)的机制。不用担心用户在没有登陆的情况下访问某些页面。用户验证,页眉页脚包含都只写一次,易于编写、修改和维护。代码比较简洁,并且很容易加上自己的页面缓冲功能。可以随意将某个页面的 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();
    }
  }

}

  
     




分享到:
评论

相关推荐

    velocity实例

    在"velocity实例"中,我们可以看到几个特定的模板文件: 1. getter.vm:这个模板可能包含了生成getter方法的代码结构,开发者可以根据需求定制获取属性值的方法。 2. test.vm:可能用于生成单元测试代码,帮助快速...

    VELOCITY实例-餐饮新版V1.10.rar

    "VELOCITY实例-餐饮新版V1.10.rar"是一个包含有关餐饮行业的软件应用更新的压缩文件,版本为V1.10。这个文件主要关注的是Velocity模板语言在Java环境中的应用,它是一种用于生成动态网页内容的强大的模板引擎。 ...

    velocity实例及中文详细文档

    在"velocity实例"中,你可以找到实际应用Velocity模板语言的示例。这些实例通常会展示如何在模板中嵌入变量、控制结构(如条件语句和循环)以及方法调用,以动态生成HTML或其他格式的输出。例如,可能有一个例子展示...

    spring3.2+velocity 实例

    标题“spring3.2+velocity 实例”表明我们要探讨的是如何在Spring 3.2框架中集成并使用Velocity模板引擎来构建动态Web应用。Velocity是一个开源的Java模板引擎,它允许开发者将业务逻辑与表现层分离,使得网页设计...

    velocity 实例

    在上述的"velocity实例"中,提供了两个示例,一个是Java工程,另一个是Web工程,都是为了展示如何使用Velocity来处理模板并生成输出。 1. Java工程实例: 这个例子展示了如何在Java应用程序中使用Velocity。首先,...

    webwork+spring+ibatis+velocity实例

    【标题】"WebWork+Spring+Ibatis+Velocity实例"是一个综合性的开发示例,它展示了这四个技术在实际Web应用程序中的集成与应用。WebWork是早期的一个MVC(Model-View-Controller)框架,提供了丰富的动作调度和数据...

    Struts2 与 Velocity 实例

    Struts2 和 Velocity 是两种广泛应用于Java Web 开发中的开源框架。Struts2 主要负责MVC(模型-视图-控制器)架构的实现,提供了一种组织应用程序逻辑的方式,而Velocity 则是一个模板引擎,专注于视图层的渲染,...

    velocity 简单实例

    这个简单的实例将会帮助我们理解如何在实际项目中使用 Velocity。 首先,我们需要了解 Velocity 的基本概念。Velocity 模板语言(VTL)是 Velocity 的核心,它允许我们在模板中插入变量和控制结构,如条件语句和...

    velocity Java开发指南中文版

    - **单实例还是多实例(To Singleton Or Not To Singleton)**: 讨论了在不同的应用场景下,是应该使用单个 Velocity 实例还是多个实例的问题。 - **Singleton Model**: 介绍如何使用单例模式来管理 Velocity 的实例...

    velocity总结

    - **初始化 Velocity**:在 JVM 或 Web 应用中仅存在一个 Velocity 引擎实例,所有应用共享该实例。通过 `org.apache.velocity.app.Velocity` 类来获取这个单例。 - **示例代码**: ```java Velocity.set...

    Velocity模板实例

    Velocity模板实例 Velocity模板实例 Velocity模板实例 Velocity模板实例

    Java Web项目开发案例精粹--Velocity简单实例

    【Java Web项目开发案例精粹--Velocity简单实例】 在Java Web开发中,Velocity是一个非常流行的模板引擎,它允许开发者将HTML页面设计与业务逻辑相分离,使得开发者可以专注于后端逻辑,而设计师则可以自由地设计...

    Apache Velocity - Developer's Guide

    - **非单例模型**:如果需要为每个请求或用户会话单独创建 Velocity 实例,则可以使用非单例模型。 #### 四、上下文 上下文是 Velocity 中一个重要的概念,它是 Velocity 引擎用来存储数据模型中的变量的地方。...

    velocity教程

    **四、Velocity实例** 一个简单的Velocity模板可能如下所示: ```html &lt;!DOCTYPE html&gt; $title &lt;h1&gt;Welcome, $username! &lt;p&gt;You have $messageCount messages. ``` 在这个例子中,`$title`、`$username`...

    Velocity_java开发指南

    - **独立实例**:探讨在特定情况下使用多个独立Velocity实例的优点和缺点。 #### 五、The Context - **基础概念**:介绍Context类及其作用。 - **迭代支持**:展示如何在模板中使用`#foreach`指令来遍历列表或其他...

    velocity生成静态页面实例

    在“velocity生成静态页面实例”中,我们首先需要一个 Velocity模板文件(通常以`.vm`为扩展名),在这个文件中,我们可以使用Velocity语法来定义页面结构,并插入动态数据占位符。例如,我们可以写一个简单的模板:...

    Velocity入门小实例,纯java工程

    本实例旨在帮助初学者快速入门Velocity,理解其基本用法和核心概念。 1. **Velocity简介** Velocity是一个基于Java的模板引擎,它允许开发者在模板中使用特定的语法(Velocity Template Language, VTL)来插入动态...

    struts2与velocity集成 实例

    Struts2和Velocity是两种广泛应用于Java Web开发的技术。Struts2是一个强大的MVC(Model-View...通过实践这个实例,你可以深入学习到如何在Struts2中使用Velocity模板来构建动态网页,进一步提升你的Java Web开发技能。

    Maven 整合 Spring mvc + Mybatis + Velocity 的实例

    本实例将探讨如何使用Maven作为项目管理工具,结合Spring MVC作为控制层框架,Mybatis作为数据访问层框架,以及Velocity作为视图层模板引擎,来构建一个完整的Java Web应用。以下是关于这些技术的详细解释和整合步骤...

Global site tag (gtag.js) - Google Analytics