📄 frameworkengine.java
字号:
package com.easyjf.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import com.easyjf.web.config.WebConfig;
/**
*
* <p>Title:框架处理引擎</p>
* <p>Description: 负责执WebFrom的创建,Action类的创建等 </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: www.easyjf.com</p>
* @author 蔡世友
* @version 1.0
*/
public abstract class FrameworkEngine {
private static final Logger logger = (Logger) Logger.getLogger(FrameworkEngine.class.getName());
/**
* 根据request创建一个form对象
* @param request
* @param formName
* @return 封装了用户数据的Form
*/
public static WebForm creatWebForm(HttpServletRequest request,String formName)
{
Map textElement=new HashMap();
Map fileElement=new HashMap();
String contentType = request.getContentType();
String reMethod = request.getMethod();
if ((contentType != null) && (contentType.startsWith("multipart/form-data"))&& (reMethod.equalsIgnoreCase("POST")))
{
//二进制 multipart/form-data
DiskFileUpload df=new DiskFileUpload();
df.setHeaderEncoding(request.getCharacterEncoding());
df.setSizeMax(1024*1024);
df.setSizeThreshold(1024*10);
List reqPars=null;
try{
reqPars=df.parseRequest(request);
for(int i=0;i<reqPars.size();i++)
{
FileItem it=(FileItem)reqPars.get(i);
if(it.isFormField())
{
textElement.put(it.getFieldName(),it.getString(request.getCharacterEncoding()));//文本字段需要转码
}
else
{
fileElement.put(it.getFieldName(),it);//文件不需要转码
}
}
}
catch(Exception e)
{
logger.error(e);
}
}
else if((contentType != null) && contentType.equals("text/xml"))
{
StringBuffer buffer=new StringBuffer();
try{
String s=request.getReader().readLine();
while(s!=null)
{
buffer.append(s+"\n");
s=request.getReader().readLine();
}
}
catch(Exception e)
{
logger.error(e);
}
textElement.put("xml",buffer.toString());
}
else
{
textElement=request2map(request);
}
// logger.debug("表单数据处理完毕!");
WebForm wf=findForm(formName);
if(wf!=null){
wf.setFileElement(fileElement);
wf.setTextElement(textElement);
}
return wf;
}
/**
* 根据formName查找(创建)一个Form对象
* @param formName
* @return 查找Form
*/
public static WebForm findForm(String formName)
{
WebForm wf=null;
String formClass=Globals.DEFAULT_FORM_CLASS;
if(formName!=null &&(! formName.equals("")))
{
FormConfig form=WebConfig.getInstance().findForm(formName);
if(form!=null)formClass=form.getBean();
// logger.debug("创建配置文件中的表单!"+formClass);
if(formClass==null || formClass.equals(""))formClass=Globals.DEFAULT_FORM_CLASS;
try{
wf=(WebForm)Class.forName(formClass).newInstance();
wf.setProperty(WebConfig.getInstance().findForm(formName).getPropertys());
wf.setFormConfig(form);
}
catch(Exception e)
{
logger.error("创建表单错误"+formClass+e);
//throw "Can't ctead form of the type"+formClass;
}
}
else
{
wf=new WebForm();
}
// logger.debug(wf.getClass().getName());
return wf;
}
/**
* 根据路径返回一个IWebAction
* @param path
* @return action
*/
public static IWebAction findAction(String path)
{
IWebAction wf=null;
String actionClass=Globals.DEFAULT_ACTION_CLASS;
if(path!=null &&(! path.equals("")))
{
Module module=WebConfig.getInstance().findModule(path);
if(module!=null)actionClass=module.getAction();
if(actionClass==null || actionClass.equals("")) actionClass=Globals.DEFAULT_ACTION_CLASS;
try{
wf=(IWebAction)Class.forName(actionClass).newInstance();
}
catch(Exception e)
{
logger.error("创建Action错误"+actionClass+e);
}
}
else
{
if(wf==null) wf=new Action();
}
return wf;
}
/**
* 根据Module返回一个一个IWebAction
* @param module
* @return 相应的Action类
*/
public static IWebAction findAction(Module module)
{
IWebAction wf=null;
String actionClass=null;
if(module!=null)actionClass=module.getAction();
if(actionClass==null || actionClass.equals(""))actionClass=Globals.DEFAULT_ACTION_CLASS;
try{
wf=(IWebAction)Class.forName(actionClass).newInstance();
}
catch(Exception e)
{
logger.error("创建表单错误"+actionClass+e);
}
return wf;
}
/**
* 根据路径返回一个Module,首先从配置文件中查找,如果配置文件中没有,则生成缺省的Module
* @param pathName
* @return 相应的Action类
*/
public static Module findModule(String pathName)
{
Module m=new Module();
String actionClass=getActionName(pathName);
m.setAction(actionClass);
m.setDefaultPage("default");
m.setPath(pathName);
Page p=new Page();
p.setName("default");
p.setType(Globals.PAGE_TEMPLATE_TYPE);
p.setUrl(pathName+"."+Globals.DEFAULT_TEMPLATE_EXT);
Map pages=new HashMap();
pages.put(p.getName(),p);
m.setPages(pages);
return m;
}
/**
* 根据url路径生成默认形式的IWebAction类全名
* @param pathName
* @return actionName
*/
public static String getActionName(String pathName)
{
String[] s=pathName.split("/");
String actionName="";
if(s!=null)
{
for(int i=0;i<s.length;i++)
{
if(s[i]!=null && (!s[i].equals("")))actionName+=s[i]+(i<s.length-1?"_":"");
}
}
String actionClass=Globals.DEFAULT_ACTTION_PACKAGE+"."+actionName+"Action";
return actionClass;
}
/**
* 一个通用reuqest数据到map的转换
* @param request
* @return Map
*/
public static Map request2map(HttpServletRequest request)
{
Map map=new HashMap();
java.util.Enumeration s=request.getParameterNames();
// System.out.println("参数个数:"+request.getParameterMap().size());
while(s.hasMoreElements())
{
String name=(String)s.nextElement();
if(!request.getParameter(name).equals(""))
{
map.put(name,request.getParameter(name));
//System.out.println(name+":"+request.getParameter(name)+"<br>");
}
}
return map;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -