⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frameworkengine.java

📁 easyweb的使用
💻 JAVA
字号:
package com.easyjf.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

import com.easyjf.aop.EjfProxyFactory;
import com.easyjf.util.CommUtil;
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 * 5);
			df.setSizeThreshold(1024 * 20);
			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();
			// 尝试使用代理对象加载拦截器,此处需要再修改
			try {
				IWebAction temp = (IWebAction) EjfProxyFactory.getProxyAction(
						actionClass, module);
				if (temp != null) {
					wf = temp;
				}
			} catch (Exception e1) {
				// 忽略加载错误
			}
		} catch (Exception e) {
			logger.error("创建Action错误" + 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;
	}

	public static IRequestInterceptor findRequestInterceptor(String name) {
		IRequestInterceptor interceptor = (IRequestInterceptor) WebConfig
				.getInstance().getInterceptors().get(name);
		return interceptor;
	}

	public static Iterator getRequestInterceptors() {
		return WebConfig.getInstance().getInterceptors().values().iterator();
	}

	/**
	 * 根据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;
	}

	/**
	 * 把表单中传来的对象转换到实现Java对象中
	 * 
	 * @param map
	 * @param obj
	 */
	public static void form2Obj(Map map, Object obj) {
		Iterator names = map.keySet().iterator();
		while (names.hasNext()) {
			String name = (String) names.next();
			if (BeanUtilsBean.getInstance().getPropertyUtils().isWriteable(obj,
					name)) {
				try {
					Object value = format(BeanUtilsBean.getInstance()
							.getPropertyUtils().getPropertyType(obj, name), map
							.get(name));
					BeanUtilsBean.getInstance().copyProperty(obj, name, value);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 这里执行数据类型转换工作,比如把Form中传来的字符型数据转换成日期型数据
	 * 
	 * @param clz
	 * @param source
	 * @return
	 */
	private static Object format(Class clz, Object source) {
		Object ret;
		if (clz == Date.class)
			ret = CommUtil.formatDate(source.toString());
		else
			ret = source;
		return ret;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -