这个是用在一个类似struts结构的框架上,小日这边的cozy
当从页面接收到控件的值设置到对应的state里边,省去了些好多state.setXXX(request.getParameter());
public void setXXXXX(Request request, StateBean state, String stateName)
throws Exception {
Enumeration e = request.getParameterNames();
Method[] meths = Class.forName(stateName).getMethods();
if (meths == null || meths.length == 0) {
return;
}
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String setMethod = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method meth = null;
Object[] argv = new Object[1];
for (int i = 0; i < meths.length; i++) {
if (meths[i].getName().equals(setMethod)) {
meth = meths[i];
break;
}
}
if (meth == null) {
continue;
}
Class[] paramType = meth.getParameterTypes();
if (paramType.length != 1) {
continue;
}
if (paramType[0].isArray() && paramType[0].getName().indexOf("java.lang.String") >= 0) {
argv[0] = request.getParameters(name);
} else if (paramType[0].newInstance() instanceof String) {
argv[0] = request.getParameter(name);
} else if (paramType[0].newInstance() instanceof StringList) {
argv[0] = CmnUtil.splitStringToList(request.getParameter(name), ",");
} else {
continue;
}
meth.invoke(state, argv);
}
}