这是我自己开发的一个MVC框架
源代码在线查看: velocitylayoutcontroller.java
package dark.web.frame.velocity.controller;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dark.web.frame.velocity.command.VelocityLayoutCommand;
import dark.web.frame.velocity.form.VelocityLayoutForm;
import dark.web.frame.velocity.page.VelocityPage;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeSingleton;
/**
* Title: 支持Velocity的控制器
* Description: 根据org.apache.velocity.servlet.VelocityLayoutServlet进行修改,
* 从而将Velocity整合进dwf框架
* Copyright: Copyright (c) 2005
* Company: DIS
* Create Time: 2005-3-21 3:24:43
* @author darkhe
* @version 1.0
*/
public class VelocityLayoutController extends VelocityController
{
/**
* The velocity.properties key for specifying the
* servlet's error template.
*/
public static final String PROPERTY_ERROR_TEMPLATE =
"tools.view.servlet.error.template";
/**
* The velocity.properties key for specifying the
* relative directory holding layout templates.
*/
public static final String PROPERTY_LAYOUT_DIR =
"tools.view.servlet.layout.directory";
/**
* The velocity.properties key for specifying the
* servlet's default layout template's filename.
*/
public static final String PROPERTY_DEFAULT_LAYOUT =
"tools.view.servlet.layout.default.template";
/**
* The default error template's filename.
*/
public static String DEFAULT_ERROR_TEMPLATE = "Error.vm";
/**
* The default layout directory
*/
public static String DEFAULT_LAYOUT_DIR = "layout/";
/**
* The default filename for the servlet's default layout
*/
public static String DEFAULT_DEFAULT_LAYOUT = "Default.vm";
private String errorTemplate;
private String layoutDir;
private String defaultLayout;
/**
* Initializes Velocity, the view servlet and checks for changes to
* the initial layout configuration.
*
* @param config servlet configuration parameters
*/
public void init(ServletConfig config) throws ServletException
{
// first do VVS' init()
super.init(config);
// check for default template path overrides
errorTemplate =
RuntimeSingleton.getString(PROPERTY_ERROR_TEMPLATE, DEFAULT_ERROR_TEMPLATE);
layoutDir =
RuntimeSingleton.getString(PROPERTY_LAYOUT_DIR, DEFAULT_LAYOUT_DIR);
defaultLayout =
RuntimeSingleton.getString(PROPERTY_DEFAULT_LAYOUT, DEFAULT_DEFAULT_LAYOUT);
// preventive error checking! directory must end in /
if (!layoutDir.endsWith("/"))
{
layoutDir += '/';
}
// log the current settings
Velocity.info("VelocityLayoutController: Error screen is '"+errorTemplate+"'");
Velocity.info("VelocityLayoutController: Layout directory is '"+layoutDir+"'");
Velocity.info("VelocityLayoutController: Default layout template is '"+defaultLayout+"'");
// for efficiency's sake, make defaultLayout a full path now
defaultLayout = layoutDir + defaultLayout;
}
/**
*
* @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
// 从而根据用户的请求后缀的不同调用页面处理程序或者命令处理程序
String page_postfix = configuration.getString("page.postfix");
String command_postfix = configuration.getString("command.postfix");
if (page_postfix.equals(getRequestPostfix(request)))
// 调用页面处理程序
{
String uri = request.getRequestURI();
VelocityPage page = getVelocityPage((HttpServletRequest) request);
// 初始化页面类
page.init(
getServletContext(),
getServletConfig(),
request,
response);
// 设置velocity模版
page.setTarget(uri);
// 检证页面类的执行权限
if (page.isAuthentic())
{
// 执行真实的商业逻辑
page.process();
}
}
else if (command_postfix.equals(getRequestPostfix(request)))
// 调用命令处理程序
{
// 如果要求做表单验证,则处理之
if (isValidate(request))
{
String input = getInput(request);
// 如果用户没有指定请求转发路径,则指定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);
}
VelocityLayoutForm form = getVelocityLayoutForm(request);
// 初始化表单验证类
form.init(getServletContext(), request, response, getMessage());
// 设置default的ContentType
form.setContentType(defaultContentType);
// 设置defautl的OutputEncoding
form.setOutputEncoding(DEFAULT_OUTPUT_ENCODING);
// 传入toolboxManager
form.setToolboxManager(toolboxManager);
form.setDefaultLayout(defaultLayout);
form.setLayoutDir(layoutDir);
form.setErrorTemplate(errorTemplate);
// 设置当前表单验证的用户请求转发路径
form.setInput(input);
// 检证表单验证类的执行权限
if (form.isAuthentic())
{
// 执行实际的验证操作
form.validate(request, response);
if (!form.isPass())
{
// 如果isPass()返回false,
// 则说明验证出表单错误,
// 立即停止程序处理
//log.debug("表单验证失败");
return;
}
//log.debug("表单验证成功");
//log.debug("form.isPass()=" + form.isPass());
}
}
String target = getTarget(request);
// 如果用户没有指定请求转发路径,则指定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);
}
VelocityLayoutCommand command = getVelocityLayoutCommand(request);
// 初始化命令类
command.init(
getServletContext(),
getServletConfig(),
request,
response);
// 设置default的ContentType
command.setContentType(defaultContentType);
// 设置defautl的OutputEncoding
command.setOutputEncoding(DEFAULT_OUTPUT_ENCODING);
// 传入toolboxManager
command.setToolboxManager(toolboxManager);
command.setDefaultLayout(defaultLayout);
command.setLayoutDir(layoutDir);
command.setErrorTemplate(errorTemplate);
// 设置当前命令的用户请求转发路径
command.setTarget(target);
// 检证命令类的执行权限
if (command.isAuthentic())
{
// 执行真实的商业逻辑
command.process();
}
}
}
/**
* @param req
* @return
*/
protected VelocityLayoutCommand getVelocityLayoutCommand(HttpServletRequest request)
throws ServletException
{
try
{
return (VelocityLayoutCommand) getCommandClass(request).newInstance();
}
catch (Exception e)
{
throw new ServletException(e);
}
}
/**
* @param request
* @return
*/
protected VelocityLayoutForm getVelocityLayoutForm(HttpServletRequest request)
throws ServletException
{
try
{
return (VelocityLayoutForm) getFormClass(request).newInstance();
}
catch (Exception e)
{
throw new ServletException(e);
}
}
}