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

📄 commdispatchaction.java

📁 Spring+acegi+ext2.0+mysql 开发 acegi做权限控制 ext2.0做前台显示 spring 做控制层 mysql 做数据库
💻 JAVA
字号:
package com.mypro.pub.util;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.actions.DispatchAction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.DelegatingActionUtils;

public abstract class CommDispatchAction extends DispatchAction {
	protected WebApplicationContext getAppContext() {
		WebApplicationContext context = DelegatingActionUtils
				.findRequiredWebApplicationContext(this.getServlet(), null);
		return context;
	}
	public Object getObjByRequest(HttpServletRequest request, Object object) {
		Object obj = object;
		try {
			// 得到obj的类型
			Class cl = obj.getClass();
			// 得到cl中的所有方法
			Method[] methods = cl.getMethods();
			// 得到方法的大小
			int methodSize = methods.length;
			// 定义了一个变量返回的类型
			Class returnType = null;
			// 定义了我们需要的set方法的名称
			String methodName = null;
			// 定义了Bean中所有变量的名称
			String fieldName = null;
			// 定义了一个变量对象 --这个变量是需要复制的变量
			Object parameter = null;
			// 定义了一个简单的时间格式化对象
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for (int i = 0; i < methodSize; i++) {
				// 只得到方法名中含有get的方法 并且非getClass方法
				if (methods[i].getName().startsWith("get")
						&& !methods[i].getName().equals("getClass")) {
					methodName = "set" + methods[i].getName().substring(3);
					fieldName = lowerFirstChar(methods[i].getName()
							.substring(3));
					returnType = methods[i].getReturnType();
					// 定义了需要调用的变量
					Method localMethod = cl.getMethod(methodName, returnType);
					// 需要手动判断parameter的类型 进行强制转换
					// String , Integer ,Date ,Double
					if (returnType.getSimpleName().equals("String")) {
						parameter = request.getParameter(fieldName) != null ? (String) request
								.getParameter(fieldName)
								: "";
					} else if (returnType.getSimpleName().equals("Integer")) {
						parameter = request.getParameter(fieldName) != null ? Integer
								.parseInt(request.getParameter(fieldName))
								: 0;
					} else if (returnType.getSimpleName().equals("Date")) {
						parameter = request.getParameter(fieldName) != null ? sdf
								.parse(request.getParameter(fieldName))
								: null;
					} else if (returnType.getSimpleName().equals("Double")) {
						parameter = request.getParameter(fieldName) != null ? Double
								.parseDouble(request.getParameter(fieldName))
								: 0.0;
					}
					localMethod.invoke(obj, parameter);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
	/**
	 * 得到变量名的第一个字母小写
	 * 
	 * @param str
	 * @return
	 */
	public static String lowerFirstChar(String str) {
		String first = (str.substring(0, 1)).toLowerCase();
		String other = str.substring(1);
		return first + other;
	}

}

⌨️ 快捷键说明

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