package tools;

import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

public class JspUtils {
	
	/**
	 * 将request的表单数据存放到一个java类中,即存到java bean
	 * @param request   
	 * @param beanClass 例子:User.class
	 * @return
	 */
	public static <T> T request2Bean(HttpServletRequest request,Class<T> beanClass)
	{
		try {
			T bean=beanClass.newInstance();
			Enumeration e=request.getParameterNames();
			while(e.hasMoreElements())
			{
				String name=(String)e.nextElement();
				String value=request.getParameter(name);
				BeanUtils.setProperty(bean,name,value);
			}
			return bean;
		} 
		catch (Exception e) {
			new RuntimeException();
		}
		return null; 
	}
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


您的十分满意是我追求的宗旨。

您的一点建议是我后续的动力。











本文转载:CSDN博客