?? velocitycontroller2.java
字號:
package dark.web.frame.velocity.controller.bak;
/**
* <p>Title: 支持Velocity的控制器</p>
* <p>Description: 根據(jù)org.apache.velocity.servlet.VelocityServlet進(jìn)行修改,
* 從而將Velocity整合進(jìn)dwf框架</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: DIS</p>
* <p>Create Time: 2005-2-28 16:24:43</p>
* @author <a href="mailto:dark_he@hotmail.com">darkhe</a>
* @version 1.0
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dark.web.frame.Controller;
import dark.web.frame.controller.FrontController;
import dark.web.frame.velocity.command.VelocityCommand;
import dark.web.frame.velocity.form.VelocityForm;
import dark.web.frame.velocity.page.UnknownVelocityPage;
import dark.web.frame.velocity.page.VelocityPage;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeSingleton;
import org.apache.velocity.util.SimplePool;
public abstract class VelocityController2
extends FrontController
implements Controller
{
/**
* The context key for the HTTP request object.
*/
public static final String REQUEST = "req";
/**
* The context key for the HTTP response object.
*/
public static final String RESPONSE = "res";
/**
* The HTTP content type context key.
*/
public static final String CONTENT_TYPE = "default.contentType";
/**
* The default content type for the response
*/
public static final String DEFAULT_CONTENT_TYPE = "text/html";
/**
* Encoding for the output stream
*/
public static final String DEFAULT_OUTPUT_ENCODING = "ISO-8859-1";
/**
* The default content type, itself defaulting to {@link
* #DEFAULT_CONTENT_TYPE} if not configured.
*/
public static String defaultContentType;
/**
* This is the string that is looked for when getInitParameter is
* called (<code>org.apache.velocity.properties</code>).
*/
protected static final String INIT_PROPS_KEY =
"org.apache.velocity.properties";
/**
* Cache of writers
*/
public static SimplePool writerPool = new SimplePool(40);
/**
* Velocity配置文件路徑,default設(shè)定為CLASSPATH根目錄下的velocity.properties
*/
public static final String PROPS_FILE = "/velocity.properties";
/**
* 初始化Velocity引擎
*/
private void initVelocity()
{
try
{
InputStream is =
VelocityController2.class.getResourceAsStream(PROPS_FILE);
Properties p = new Properties();
p.load(is);
/*
* now, lets get the two elements we care about, the
* template path and the log, and fix those from relative
* to the webapp root, to absolute on the filesystem, which is
* what velocity needs
*/
String path = p.getProperty("file.resource.loader.path");
if (path != null)
{
path = getServletContext().getRealPath(path);
log.info("file.resource.loader.path=" + path);
p.setProperty("file.resource.loader.path", path);
}
path = p.getProperty("runtime.log");
if (path != null)
{
path = getServletContext().getRealPath(path);
log.info("runtime.log=" + path);
p.setProperty("runtime.log", path);
}
Velocity.init(p);
log.info("初始化Velocity引擎成功......");
}
catch (IOException e)
{
log.error("初始化Velocity引擎出錯", e);
}
catch (Exception e)
{
log.error("初始化Velocity引擎出錯", e);
}
/*
* Now that Velocity is initialized, cache some config.
*/
defaultContentType =
RuntimeSingleton.getString(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
}
/**
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @see dark.web.frame.Controller#doRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doRequest(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// 獲取page.postfix和command.postfix
// 從而根據(jù)用戶的請求后綴的不同調(diào)用頁面處理程序或者命令處理程序
String page_postfix = configuration.getString("page.postfix");
String command_postfix = configuration.getString("command.postfix");
if (page_postfix.equals(getRequestPostfix(request)))
// 調(diào)用頁面處理程序
{
String uri = request.getRequestURI();
VelocityPage page = getVelocityPage((HttpServletRequest) request);
// 初始化頁面類
page.init(
getServletContext(),
getServletConfig(),
request,
response);
// 設(shè)置velocity模版
page.setTarget(uri);
// 檢證頁面類的執(zhí)行權(quán)限
if (page.isAuthentic())
{
// 執(zhí)行真實(shí)的商業(yè)邏輯
page.process();
}
}
else if (command_postfix.equals(getRequestPostfix(request)))
// 調(diào)用命令處理程序
{
// 如果要求做表單驗(yàn)證,則處理之
if (isValidate(request))
{
String input = getInput(request);
// 如果用戶沒有指定請求轉(zhuǎn)發(fā)路徑,則指定default value
if (input.equals(""))
{
input = configuration.getString("default.input");
log.warn(
"dont't specify parameter $INPUT, use default.input:"
+ input);
request.setAttribute(
"$WARM",
"dont't specify parameter $INPUT, use default.input"
+ input);
}
VelocityForm form = getVelocityForm(request);
// 初始化表單驗(yàn)證類
form.init(getServletContext(), request, response, getMessage());
// 設(shè)置當(dāng)前表單驗(yàn)證的用戶請求轉(zhuǎn)發(fā)路徑
form.setInput(input);
// 檢證表單驗(yàn)證類的執(zhí)行權(quán)限
if (form.isAuthentic())
{
// 執(zhí)行實(shí)際的驗(yàn)證操作
form.validate(request, response);
if (!form.isPass())
{
// 如果isPass()返回false,
// 則說明驗(yàn)證出表單錯誤,
// 立即停止程序處理
//log.debug("表單驗(yàn)證失敗");
return;
}
//log.debug("表單驗(yàn)證成功");
//log.debug("form.isPass()=" + form.isPass());
}
}
String target = getTarget(request);
// 如果用戶沒有指定請求轉(zhuǎn)發(fā)路徑,則指定default value
if (target.equals(""))
{
target = configuration.getString("default.target");
log.warn(
"don't specify paramter $TARGER, use default.target:"
+ target);
request.setAttribute(
"$WARM",
"don't specify paramter $TARGER, use default.target:"
+ target);
}
VelocityCommand command = getVelocityCommand(request);
// 初始化命令類
command.init(
getServletContext(),
getServletConfig(),
request,
response);
// 設(shè)置當(dāng)前命令的用戶請求轉(zhuǎn)發(fā)路徑
command.setTarget(target);
// 檢證命令類的執(zhí)行權(quán)限
if (command.isAuthentic())
{
// 執(zhí)行真實(shí)的商業(yè)邏輯
command.process();
}
}
}
/**
* @throws javax.servlet.ServletException
* @see javax.servlet.GenericServlet#init()
*/
public void init() throws ServletException
{
super.init();
initVelocity();
}
/**
* @param request
* @return
*/
protected VelocityForm getVelocityForm(HttpServletRequest request)
throws ServletException
{
try
{
return (VelocityForm) getFormClass(request).newInstance();
}
catch (Exception e)
{
throw new ServletException(e);
}
}
/**
* @param req
* @return
*/
protected VelocityCommand getVelocityCommand(HttpServletRequest request)
throws ServletException
{
try
{
return (VelocityCommand) getCommandClass(request).newInstance();
}
catch (Exception e)
{
throw new ServletException(e);
}
}
/**
* @param request
* @return
*/
protected VelocityPage getVelocityPage(HttpServletRequest request)
throws ServletException
{
try
{
return (VelocityPage) getVelocityPageClass(request).newInstance();
}
catch (Exception e)
{
throw new ServletException(e);
}
}
/**
* 通過類名串值到得類對象
* @param req
*/
protected Class getVelocityPageClass(HttpServletRequest request)
{
Class result = null;
String cp = configuration.getString("page.package");
String pageName = getPageName(request);
String pageClassName = cp + "." + pageName;
try
{
result = Class.forName(pageClassName);
}
catch (ClassNotFoundException e)
{
log.info(
"class:"
+ pageClassName
+ " not found, return UnknownVelocityPage.class");
result = UnknownVelocityPage.class;
}
return result;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -