📄 requestprocessor.java
字号:
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.web.config.*;
/**
*
* <p>Title:框架核心处理器</p>
* <p>Description:EasyJWeb框架的中心处理器,由ActionServlet调用,然后调用相应的Action并返回用户服务</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 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("没有找到处理模板的类:"+module.getAction()));
return;
}
if(form==null)
{
servlet.error(request,response,new Exception("表单创建错误:"+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("跳转到指定页面:"+page.getUrl());
//doForward(page.getUrl(),request,response);
response.sendRedirect(page.getUrl());
}
}
else
{
//logger.error("没有任何页面要跳转!"+module.getPath()+module.getDefaultPage());
//servlet.error(request,response,new Exception("没有设置跳转的页面!"));
}
}
}
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中的数据
//处理没有定义属性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("模块合成出错!"));
}
catch( ResourceNotFoundException rnfe )
{
logger.error("找不到模板文件!");
servlet.error(request,response,new Exception("找不到模板!"+WebConfig.getInstance().getTemplateBasePath()+uri));
}
catch( ParseErrorException pee )
{
logger.error("模板语法错误");
servlet.error(request,response,new Exception("模板文件中存在语法错误,不能正常解析!"+WebConfig.getInstance().getTemplateBasePath()+uri));
}
catch( MethodInvocationException mie )
{
logger.error("模板方法调用错误:");
}
catch( Exception e )
{
logger.error("查找模板错误!"+e);
}
}
public Template getTemplate(String name,String encoding)throws ResourceNotFoundException,ParseErrorException,MethodInvocationException,Exception
{
Template template = null;
template =RuntimeSingleton.getTemplate(name,encoding);//name
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));
//logger.debug("Context:"+name+"="+result.get(name));
}
return context;
}
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());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -