?? utiltool.java
字號:
package tools;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import action.MyForm;
import config.MyBean;
import config.MyConfig;
import config.MyMap;
import exception.ActionHasError;
import exception.FormHasError;
import exception.ForwardHasError;
public class UtilTool {
// 進行匹配
public static MyForm InsertForm(HttpServletRequest request, MyConfig config)
throws ActionHasError,FormHasError,ForwardHasError {
String uri = request.getRequestURI();
// 得到path
String thePath = UtilTool.getRequestPath(uri);
System.out.println(thePath);
// 根據config,thePath 得到formClass 的名字
String formClass = getFormClass(config, thePath);
// 得到前臺組件的name集合
List pelist = getParamElements(request);
// 根據formClass得到該類的實例
MyForm myform = null;
try {
System.out.println(formClass);
myform = (MyForm) Class.forName(formClass).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(222);
// 開始填充對象
return doInsert(request, pelist, myform);
}
// 得到path
public static String getRequestPath(String uri) {
String temp = uri.substring(uri.indexOf("/") + 1, uri.length());
return temp.substring(temp.indexOf("/") , temp.length() - 3);
}
// 得到Form組件數組
private static List getParamElements(HttpServletRequest request) {
Enumeration enum = request.getParameterNames();
List list = new ArrayList();
while (enum.hasMoreElements()) {
String e = (String) enum.nextElement();
list.add(e);
}
return list;
}
private static MyForm doInsert(HttpServletRequest request, List pelist,
MyForm myform)throws FormHasError{
Field[] field = myform.getClass().getDeclaredFields();
for (int i = 0; i < field.length; i++) {
for (int j = 0; j < pelist.size(); j++) {
String fieldstr = field[i].getName();
String pestr = pelist.get(j).toString();
if (fieldstr.equals(pestr)) {
try {
System.out.println("前臺組件名字:"+pestr);
System.out.println(myform.toString());
field[i].setAccessible(true);
field[i].set(myform, request.getParameter(pestr));
} catch (Exception e) {
e.printStackTrace();
throw new FormHasError();
}
}
}
}
return myform;
}
private static String getFormClass(MyConfig config, String thePath) throws ActionHasError,FormHasError{
MyMap[] mymap = config.getMymap();
MyBean[] mybean = config.getMybean();
System.out.println(mymap[0].getName());
System.out.println(mymap[0].getType());
System.out.println(mymap[0].getPath());
System.out.println(mybean[0].getName());
System.out.println(mybean[0].getType());
System.out.println(thePath);
System.out.println("-----------");
for (int i = 0; i < mymap.length; i++) {
if (thePath != null && thePath.equals(mymap[i].getPath())) {
String formname = mymap[i].getName();
for (int j = 0; j < mybean.length; j++) {
if (formname != null && formname.equals(mybean[j].getName())) {
String formclass = mybean[j].getType();
System.out.println(111);
return formclass;
}
}
}
}
return null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -