?? requestprocessor.java
字號(hào):
package com.easyjf.web;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.io.VelocityWriter;
import org.apache.velocity.runtime.RuntimeSingleton;
import com.easyjf.util.CommUtil;
import com.easyjf.util.HtmlUtil;
import com.easyjf.web.config.*;
/**
*
* <p>
* Title:框架核心處理器
* </p>
* <p>
* Description:EasyJWeb框架的中心處理器,由ActionServlet調(diào)用,然后調(diào)用相應(yīng)的Action并返回用戶服務(wù)
* </p>
* <p>
* Copyright: Copyright (c) 2006
* </p>
* <p>
* Company: www.easyjf.com
* </p>
*
* @author 蔡世友
* @version 1.0
*/
public class RequestProcessor {
private ActionServlet servlet;
private Module module;
private static final Map templateCache = new HashMap();
private static final WebConfig config = WebConfig.getInstance();
private static final Logger logger = (Logger) Logger
.getLogger(RequestProcessor.class.getName());
private RequestProcessor() {
}
public RequestProcessor(ActionServlet servlet) {
this.servlet = servlet;
}
public void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String path = request.getServletPath();
String moduleName = "";
String para = "";
if (path.lastIndexOf(".ejf") > 0) {
moduleName = path.substring(0, path.lastIndexOf(".ejf"));
} else {
path = request.getRequestURI();
if (path.indexOf("/ejf/") == 0)
path = path.substring(4);
int n = path.lastIndexOf('/');
if (n > 0) {
para = path.substring(n + 1);
moduleName = path.substring(0, n);
if (para.lastIndexOf('.') > 0)
para = para.substring(0, para.lastIndexOf('.'));
}
}
module = config.findModule(moduleName);
if (module == null)
module = FrameworkEngine.findModule(moduleName);
if (module != null) {
String formName = module.getForm();
WebForm form = FrameworkEngine.creatWebForm(request, formName);
if (para != null && (!para.equals(""))) {
String[] paras = para.split("_");
if (paras != null) {
if (paras.length > 0)
form.getTextElement().put("easyJWebCommand", paras[0]);
if (paras.length > 1)
form.getTextElement().put("easyJWebID", paras[1]);
}
}
IWebAction action = FrameworkEngine.findAction(module);
if (action == null) {
servlet.error(request, response, new Exception("沒(méi)有找到處理模板的類:"
+ module.getAction()));
return;
}
if (form == null) {
servlet.error(request, response, new Exception("表單創(chuàng)建錯(cuò)誤:"
+ formName));
return;
}
Page page = getResult(module, form, action);
if (page != null) {
if (page.getType().equals(Globals.PAGE_TEMPLATE_TYPE)) {
// logger.debug("合成模板"+page.getName()+":"+page.getUrl());
doTemplate(page.getUrl(), form, request, response);
} else {
// logger.debug("跳轉(zhuǎn)到指定頁(yè)面:"+page.getUrl());
// doForward(page.getUrl(),request,response);
response.sendRedirect(page.getUrl());
}
} else {
// logger.error("沒(méi)有任何頁(yè)面要跳轉(zhuǎn)!"+module.getPath()+module.getDefaultPage());
// servlet.error(request,response,new Exception("沒(méi)有設(shè)置跳轉(zhuǎn)的頁(yè)面!"));
}
}
}
public Page getResult(Module module, WebForm form, IWebAction action)
throws ServletException {
form.setEasyJWebResult(new HashMap());
Page page = null;
try {
page = action.execute(form, module);
} catch (Exception e) {
servlet.error(ActionContext.getContext().getRequest(),
ActionContext.getContext().getResponse(), e);
}
// 保存表單form中的數(shù)據(jù)
// 處理沒(méi)有定義屬性property的表單
if (form != null && form.getClass() == WebForm.class) {
form.setProperty(form.getTextElement());// 只保存文本屬性
form.getProperty().putAll(form.getFileElement());
}
if (form != null && (form.getProperty() != null)) {
Iterator it = form.getProperty().keySet().iterator();
while (it.hasNext()) {
String name = (String) it.next();
form.addResult(name, form.get(name));
}
}
// if(form!=null &&
// (form.getClass()==WebForm.class))form.getProperty().clear();
// request.setAttribute("easyJWebResult",easyJWebResult);
return page;
}
protected void doForward(String uri, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
rd.forward(request, response);
}
protected void doTemplate(String uri, WebForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Context context = createContext(form);
try {
Template template = getTemplate(uri, request.getCharacterEncoding());
if (context != null && (template != null)) {
mergeTemplate(template, context, response);
} else
servlet.error(request, response, new Exception("模塊合成出錯(cuò)!"));
} catch (ResourceNotFoundException rnfe) {
logger.error("找不到模板文件!");
servlet.error(request, response, new Exception("找不到模板!"
+ WebConfig.getInstance().getTemplateBasePath() + uri));
} catch (ParseErrorException pee) {
logger.error("模板語(yǔ)法錯(cuò)誤");
servlet.error(request, response, new Exception(
"模板文件中存在語(yǔ)法錯(cuò)誤,不能正常解析!"
+ WebConfig.getInstance().getTemplateBasePath()
+ uri));
} catch (MethodInvocationException mie) {
logger.error("模板方法調(diào)用錯(cuò)誤:");
} catch (Exception e) {
logger.error("查找模板錯(cuò)誤!" + e);
}
}
protected Template getTemplate(String name, String encoding)
throws ResourceNotFoundException, ParseErrorException,
MethodInvocationException, Exception {
if (WebConfig.getInstance().isDebug())
return RuntimeSingleton.getTemplate(name, encoding);// 若為Debug狀態(tài),則每次都重新載入模板
Template template = (Template) templateCache.get(name);// 先從Cache中讀取模板文件
// 這里得進(jìn)一步完善,當(dāng)用戶已經(jīng)更改模板文件后,需要能夠自動(dòng)加載,同時(shí)增加Cache數(shù)量的限制
if (template == null)
synchronized (templateCache) {
{
// System.out.println("重新加載模板文件!");
template = RuntimeSingleton.getTemplate(name, encoding);// name
templateCache.put(name, template);
}
}
return template;
}
protected Context createContext(WebForm form) {
Map result = form.getEasyJWebResult();
Context context = new VelocityContext();
Iterator it = result.keySet().iterator();
while (it.hasNext()) {
String name = (String) it.next();
context.put(name, result.get(name));
createUtilContext(context);
}
return context;
}
protected void createUtilContext(Context context) {
context.put("HtmlUtil", HtmlUtil.getInstance());
context.put("CommUtil", CommUtil.getInstance());
}
protected void mergeTemplate(Template template, Context context,
HttpServletResponse response) {
VelocityWriter vw = null;
Writer writer = null;
try {
response.setCharacterEncoding(template.getEncoding());
response.setContentType("text/html;charset=utf-8");
writer = servlet.getResponseWriter(response);
vw = (VelocityWriter) ActionServlet.getWriterPool().get();
if (vw == null) {
vw = new VelocityWriter(writer, 4 * 1024, true);
} else {
vw.recycle(writer);
}
template.merge(context, vw);
} catch (ResourceNotFoundException e) {
logger.error("ResourceNotFoundException:" + e);
} catch (ParseErrorException e) {
logger.error(e);
} catch (MethodInvocationException e) {
logger.error(e);
} catch (UnsupportedEncodingException e) {
logger.error(e);
} catch (IOException e) {
logger.error(e);
} catch (Exception e) {
logger.error(e);
}
finally {
if (vw != null) {
try {
vw.flush();
vw.recycle(null);
ActionServlet.getWriterPool().put(vw);
} catch (Exception e) {
logger.error("Trouble releasing VelocityWriter: "
+ e.getMessage());
}
}
}
}
protected ServletContext getServletContext() {
return (servlet.getServletContext());
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -