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

📄 actionprocesserimpl.java

📁 类似struts2的mvc框架
💻 JAVA
字号:
/**
 * 
 */
package pp.mvc.core.impl;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import pp.mvc.core.ActionProcesser;
import pp.mvc.core.ObjectFactory;
import pp.mvc.domain.ActionDefine;
import pp.mvc.domain.Result;
import pxb.ioc.Inject;

/**
 * @author panhuizi
 * 
 */
public class ActionProcesserImpl implements ActionProcesser {

	private ObjectFactory objectFactory;

	/*
	 * (non-Javadoc)
	 * 
	 * @see pp.mvc.core.ActionProcesser#processAction(pp.mvc.domain.ActionDefine,
	 *      javax.servlet.http.HttpServletRequest)
	 */
	@SuppressWarnings("unchecked")
	public Result processAction(ActionDefine actionDefine,
			HttpServletRequest request) {
		try {
			Object actionObject = this.objectFactory.getObject(actionDefine
					.getType());
			Class<?> actionClass = actionObject.getClass();
			Map<String, String[]> params = request.getParameterMap();
			if (params != null && params.size() > 0) {
				for (String key : params.keySet()) {
					String[] value = params.get(key);
					String upperFirstLetter = key.substring(0, 1).toUpperCase();
					String setMethodName = "set" + upperFirstLetter
							+ key.substring(1);
					Method method = null;
					try {
						method = actionClass.getMethod(setMethodName,
								new Class[] { String.class });
					} catch (NoSuchMethodException e) {
						throw new RuntimeException(actionDefine.getType() + "."
								+ setMethodName + "(" + String.class.getName()
								+ ")" + "不存在");
					}
					if (method != null) {
						String filedValue;
						StringBuilder sb = new StringBuilder();
						if (value != null && value.length > 1) {
							for (String s : value) {
								if (s != null) {
									sb.append(s + ",");
								}
							}
							if (sb.length() == 0) {
								filedValue = "";
							}
							filedValue = sb.substring(0, sb.length() - 1)
									.toString();
						} else {
							filedValue = value[0];
						}
						method.invoke(actionObject,
								(Object[]) new String[] { filedValue });
					}
				}
			}

			Method method = null;
			try {
				method = actionClass.getMethod(actionDefine.getMethod());
			} catch (Exception e) {
				throw new RuntimeException(actionDefine.getType() + "."
						+ actionDefine.getMethod() + "()" + "不存在");
			}
			String resultString = (String) method.invoke(actionObject);
			Result result = actionDefine.getResults().get(resultString);
			result.setParameters(params);

			/* 版本一:不够人性化 */
			// Field[] fields = actionClass.getDeclaredFields();
			// Method[] methods = actionClass.getMethods();
			// for (Field field : fields) {
			// String fieldName = field.getName();
			// String upperFirstLetter = fieldName.substring(0, 1)
			// .toUpperCase();
			// String getMethodName = "get" + upperFirstLetter
			// + fieldName.substring(1);
			// for (Method m : methods) {
			// if (getMethodName.equals(m.getName())) {
			// field.setAccessible(true);
			// try {
			// request.setAttribute(field.getName(), field
			// .get(actionObject));
			// } catch (IllegalArgumentException e) {
			// e.printStackTrace();
			// } catch (IllegalAccessException e) {
			// e.printStackTrace();
			// }
			// }
			// }
			// }
			/* 版本二: 感觉比较人性化*/
			Method[] methods = actionClass.getMethods();
			for (Method m : methods) {
				String methodName = m.getName();
				if (methodName.startsWith("get")
						&& Modifier.isPublic(m.getModifiers())) {
					String upperFirstLetter = methodName.substring(3, 4)
							.toLowerCase();
					String fieldName = upperFirstLetter
							+ methodName.substring(4);
					try {
						request.setAttribute(fieldName, m.invoke(actionObject));
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}

			return result;
		} catch (Exception e) {
			throw new RuntimeException("[" + actionDefine.getType() + "]不存在");
		}
	}

	@Inject
	public void setObjectFactory(ObjectFactory objectFactory) {
		this.objectFactory = objectFactory;
	}

}

⌨️ 快捷键说明

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