绝对秘籍:实现<jsp:setProperty name="" property="*"/>类似功能.
/**
*
* @param req HttpServletRequest
* @param bean Object In Actual,It is DTO Object or any Bean class.
* @throws Exception
* @return Object
*/
public static Object mapBean(javax.servlet.http.HttpServletRequest req, Object bean,boolean debug) throws Exception
{
SimpleDateFormat simplefmt=new SimpleDateFormat(dateFormatPattern);
SimpleDateFormat complexfmt=new SimpleDateFormat(dateAndFormatPattern);
Method[] methods = bean.getClass().getMethods();
for (int i = 0; i < methods.length; i++)
{
Method method = methods[i];
if (method.getName().startsWith("set"))
{
String propertyName = method.getName().substring(3).toLowerCase();
if(debug)System.out.println("FormBean:propertyName="+ propertyName );
String values = req.getParameter(propertyName);
Class[] ts = method.getParameterTypes();
Class t;
if(ts.length>0){t = ts[0];}else {System.out.println("FormBean:Method {"+method.getName()+"} is Invalid:no parameter");continue;}
if (values != null)
{
if(debug){System.out.println("FormBean:Method is:"+method.getName()+" is invoking");}
if (t.equals(String.class))
{
method.invoke(bean, new Object[]{values});
}else if (t.equals(Integer.class)|| t.equals(Integer.TYPE))
{
Integer tmpval = new Integer(values);
tmpval = new Integer(values);
method.invoke(bean, new Object[]{tmpval});
}else if (t.equals(Byte.class)|| t.equals(Byte.TYPE)) {
Byte tmpval = new Byte(values);
method.invoke(bean, new Object[]{tmpval});
}
else if (t.equals(Boolean.class)|| t.equals(Boolean.TYPE) ) {
Boolean tmpval = new Boolean(values);
method.invoke(bean, new Object[]{tmpval});
}
else if (t.equals(Short.class) || t.equals(Short.TYPE) ) {
Short tmpval = new Short(values);
method.invoke(bean, new Object[]{tmpval});
}
else if (t.equals(Long.class)|| t.equals(Long.TYPE)) {
Long tmpval = new Long(values);
method.invoke(bean, new Object[]{tmpval});
}
else if (t.equals(Double.class)|| t.equals(Double.TYPE)) {
Double tmpval = new Double(values);
method.invoke(bean, new Object[]{tmpval});
}
else if (t.equals(Float.class)|| t.equals(Float.TYPE)) {
Float tmpval = new Float(values);
method.invoke(bean, new Object[]{tmpval});
}
else if (t.equals(Character.class) || t.equals(Character.TYPE))
{
Character[] tmpval=new Character[values.length()];
for(int ia=0;ia<values.length();ia++)
tmpval[ia]=new Character(values.charAt(ia));
method.invoke(bean, new Object[]{tmpval});
}else if(t.equals(java.sql.Date.class))//输入时间
{
java.util.Date tmpdate=simplefmt.parse(values);
java.sql.Date tmpval = new java.sql.Date(tmpdate.getTime());
System.out.println("Date="+tmpval);
method.invoke(bean, new Object[]{tmpval});
}else if(t.equals(java.sql.Timestamp.class))//输入时间戳
{
java.util.Date tmpdate=complexfmt.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).parse(values);
java.sql.Timestamp tmpval =new java.sql.Timestamp(tmpdate.getTime());
method.invoke(bean, new Object[]{tmpval});
System.out.println("Timestamp="+tmpval);
}
}
else {
System.out.println("FormBean:"+"Method {"+method.getName()+ "} can't find Parameter");
// Object tmpval = new Integer(values);
// method.invoke(bean, new Object[]{tmpval});
}
}
}
return bean;
}