vax.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 的子類如上面基本類似,,只不過會(huì)多了一些 context.put("xxx","xxxx") 的語句,。
通過以上的例子,,我們可以看到,,使用 Velocity + Servlet , 所有的代碼為: 1 個(gè) java serverlet + m 個(gè) java class + n 個(gè) Html 文件,。
這里是用了集中處理,然后分發(fā)(dispatch)的機(jī)制,。不用擔(dān)心用戶在沒有登陸的情況下訪問某些頁面,。用戶驗(yàn)證,頁眉頁腳包含都只寫一次,,易于編寫,、修改和維護(hù)。代碼比較簡(jiǎn)潔,,并且很容易加上自己的頁面緩沖功能??梢噪S意將某個(gè)頁面的 html 在內(nèi)存中保存起來,,緩存幾分鐘,實(shí)現(xiàn)頁面緩沖功能,。成功,、出錯(cuò)頁面也可以用同樣的代碼封裝成函數(shù),通過參數(shù)將 Message/Title 傳入即可,。
因?yàn)?Java 代碼與 Html 代碼完全在不同的文件中,,美工與java代碼人員可以很好的分工,每個(gè)人修改自己熟悉的文件,,基本上不需要花時(shí)間做協(xié)調(diào)工作,。而用 JSP, 美工與java代碼人員共同修改維護(hù) .jsp 文件,麻煩多多,,噩夢(mèng)多多,。而且這里沒有用 xml ,說實(shí)話,,懂 xml/xls 之類的人只占懂 Java 程序員中的幾分之一,,人員不好找。
因?yàn)樗?java 代碼人員寫的都是標(biāo)準(zhǔn) Java 程序,,可以用任何 Java 編輯器,,調(diào)試器,因此開發(fā)速度也會(huì)大大提高,。美工寫的是標(biāo)準(zhǔn) Html 文件,,沒有 xml, 對(duì)于他們也很熟悉,速度也很快,。并且,,當(dāng)需要網(wǎng)站改版的時(shí)候,只要美工把 html 文件重新修飾排版即可,,完全不用改動(dòng)一句 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 onceif (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
{ |