服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

velocity -- java web 开发新技术

 

 velocity 是一个基于 java 的通用模板工具,来自于 jakarta.apache.org 。 apache 开发的目的是替代 jsp/struts。

 jsp 是 sun 开发的 web 应用程序开发技术,与 php, asp 具有类似功能。但是 jsp 存在着很多问题,使得它难以推广, 请参照 the problems with jsp by jason hunter 。velocity 主要是用在 java servlet 中。

 template 技术最早在 php 中作为附加工具包引入。目的是为了解决 php 中 常见的 php 代码和 html 代码混在一起,难以阅读、难以编写、难以修改的问题。其实这种问题在 jsp/asp 中也存在。asp 因为语法简单,引入了 vb script (vba 最早设计的目的是达到每个 office 用户都能学会,因而超级易学,vb script 与 vba 语法类似) ,使得几乎每个一个会 java script 的人都能学会。同时会 asp 和 html 的人很多,但是同时会 php 和 html 的人很少,同时会 jsp 和 html 的人更少。php 中引入 template 将 html 和 php 代码分成不同的文件,会 php 的只改 php 文件,会 html 的只改 html 文件,分工明确,因而工作效率大大增强,程序也更容易写。使用 velocity, java 代码与 html 代码分成不同的文件,不用学习 jsp 语法。作为 mvc 的应用技术之一,velocity 远比 jsp/struts 在model 与 view 代码的分离更为成功。

 php 的 template 介绍请参考我的文章 使用php4中的 integratedtemplate类实现html和php代码分离,使用php4中的 integratedtemplate类实现block功能 。


 velocity 可以用来产生动态网页,sql,postscript 文件和其它可以从模板转换过来的文本文件。我曾经用它来产生 email 发送感谢信。发给不同用户的 email 的不同之处在于开始的用户名。用 velocity 处理这件事很容易。首先用 dreamweaver 编写一封 html 格式的 email.

 dear $username,

 ....

 然后在 java 代码中将上面的变量进行替换,发送出去。程序写好后,email 还可以用 dreamweaver 随意修改而不用改动 java 代码。

velocity 用在 servlet 中示例如下:

1) login.htm

     
  <!doctype html public "-//w3c//dtd html 4.01 transitional//en"><html><head><title>user login</title><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body><form  name="formlogin" method="post" action="/login">  <table width="950" height="81" border="0" cellpadding="0" cellspacing="0">    <tr>       <td>&nbsp;</td>      <td colspan="2">welcome!!</td>      <td>&nbsp;</td>    </tr>    <tr>       <td width="221">&nbsp;</td>      <td width="109">&nbsp;</td>      <td width="374">&nbsp;</td>      <td width="246">&nbsp;</td>    </tr>    <tr>       <td>&nbsp;</td>      <td>login account:</td>      <td><input name="textfieldaccount" type="text" id="textfieldaccount"></td>      <td>&nbsp;</td>    </tr>    <tr>       <td>&nbsp;</td>      <td>password:</td>      <td><input name="textfieldpassword" type="text" id="textfieldpassword"></td>      <td>&nbsp;</td>    </tr>    <tr>       <td>&nbsp;</td>      <td>&nbsp;</td>      <td><input type="submit" name="submit" value="login"></td>      <td>&nbsp;</td>    </tr>  </table></form></body></html>
  
     


2)自 velocityservlet 继承,写一个子类 login.java

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

public class login
  extends velocityservlet {
  protected template handlerequest(
    httpservletrequest request,
    httpservletresponse response,
    context context) throws exception {

    boolean checksuccess = false;
    //check
    //...
    string account = request.getparameter("textfieldaccount");
    string password = request.getparameter("textfieldpassword");
    checksuccess = checkloginpassword(account,password);

    template template = null;
    try {
      if (checksuccess) {
        template = gettemplate("success.htm");
        context.put("username",getloginusername());//replace $username in html file
      }
      else {
        template = gettemplate("fail.htm");
        context.put("username",getloginusername());//replace $username in html file
      }
    }
    catch (exception e) {
      e.printstacktrace();
    }

    return template;

  }

  private boolean checkloginpassword(string account, string password){
    //do something to check
    //....

    return true;
  }

  private string getloginusername(){
    return "test by jack";
  }
}

 
  
     


3) success.htm

     
  <!doctype html public "-//w3c//dtd html 4.01 transitional//en"><html><head><title>untitled document</title><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>
<body>
<p>congratulation!!</p>
<p>you are logined as $username!!</p>
</body>
</html>

  
     


3) fail.htm

     
  <!doctype html public "-//w3c//dtd html 4.01 transitional//en"><html><head><title>untitled document</title><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>
<body>
<p>error!!</p>
<p>fail to login as $username!!</p>
</body>
</html>

  
     


基本上来说, success.htm 和 fail.htm 都可以用 dreamweaver 随意排版,不用担心 java 代码的问题。比起 jsp 文件很多地方无法用 dreamweaver 排版,只能手工修改 html code,velocity 把我们带入了一个全新的世界。一个只用标准 java 写 web 程序,用标准 html 写表示层,没有 tag, 没有自定义语法的清晰代码世界。

并且现在没有用任何令人讨厌的 tag. jsp tag 让很多人感到莫名其妙, java 程序员看着觉得奇怪, 网页程序员看着也觉得奇怪。没有几个人能够精通它,并且没有几个工具能够检查 tag 语法错误,如果你那个地方写错了,没有工具查错是很令人恶心的事。

用了 velocity, 所有这些烦恼都没有了。放弃设计糟糕的 jsp/struts 吧。

扫描关注微信公众号