アクション実行方法
2601 ワード
dom 4 jでxmlタグを解析し、タグ属性とテキスト内容を集合に追加し、メソッドを作成し、actionに転送して対応するidを検索し、クラスパスとメソッドをどのように取得するか、反射実行メソッドを利用する
FrameUtil.java
UserDao1.java
FrameUtil.java
public class FrameUtil {
List list = new ArrayList();
public static void main(String[] args) throws Exception {
FrameUtil f = new FrameUtil();
f.executeMethod("2", f.jie());
}
private List jie() throws Exception {
// TODO Auto-generated method stub
Element root = new SAXReader().read(new File("./src/studentInfo.xml"))
.getRootElement();
Iterator it = root.elementIterator();
while (it.hasNext()) {
Student stu = new Student();
Element elStu = it.next();
stu.setId(elStu.attribute("id").getValue());
stu.setClassName(elStu.attribute("className").getValue());
stu.setMethod(elStu.attribute("method").getValue());
Iterator it2 = elStu.elementIterator();
while (it2.hasNext()) {
Element zi = it2.next();
if ("name".equals(zi.getName())) {
stu.setName(zi.getTextTrim());
}
if ("college".equals(zi.getName())) {
stu.setCollage(zi.getTextTrim());
}
if ("telephone".equals(zi.getName())) {
stu.setTelephone(zi.getTextTrim());
}
if ("notes".equals(zi.getName())) {
stu.setNotes(zi.getTextTrim());
}
}
list.add(stu);
}
for (Student s : list) {
System.out.println(s.getId() + " , " + s.getClassName() + " , "
+ s.getMethod() + " , " + s.getName() + " , "
+ s.getCollage() + " , " + s.getTelephone() + " , "
+ s.getNotes());
}
return list;
}
public Object executeMethod(String action, List para)
throws Exception {
String cname = "";
String method = "";
for (Student stu : list) {
if (stu.getId().equals(action))// id action
{
//
cname = stu.getClassName();
method = stu.getMethod();
break;//
}
}
Class> cl = Class.forName(cname);//
Object obj = cl.newInstance();//
Method me = cl.getDeclaredMethod(method, List.class);//
Object objReturn = me.invoke(obj, para);//
return objReturn;
}
}
UserDao1.java
public class UserDao1 {
public void add() {
System.out.println(" add ");
}
public String select(List