From 8bbcd7fb7ed635e2df920075b46a20735333b67f Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Mon, 6 Mar 2017 19:34:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=BA=E4=B9=8E=E6=84=8F=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataStructure/src/main/java/com/Main.java | 5 ++- .../coderising/litestruts/.Struts.java.swp | Bin 12288 -> 12288 bytes .../com/coderising/litestruts/Struts.java | 34 ++++++++++++++++-- .../com/coderising/litestruts/XmlUtil.java | 26 ++++++++++++++ .../src/main/resources/struts.xml | 11 ++++++ group11/1178243325/DataStructure/struts.xml | 4 +-- 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 group11/1178243325/DataStructure/src/main/resources/struts.xml diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java index c5d56779c1..f5e5a36ebd 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/Main.java +++ b/group11/1178243325/DataStructure/src/main/java/com/Main.java @@ -1,5 +1,6 @@ package com; +import java.util.*; import com.coderising.litestruts.*; import com.coderising.array.*; public class Main { @@ -48,6 +49,8 @@ public static void main(String[] args) { System.out.print("\njoin测试:"); int[] jArray = new int[]{2, 3, 8}; System.out.print(ArrayUtil.join(jArray, "-")); - Struts.test(); + Map map = new HashMap<>(); + Struts.runAction("login", map); + } } diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp index 5ff0f0622e0264939929151bb724ed26c12a7bb7..1f45a5f25e9bf8dcf03addb1f9a3a0a4c6fe0a2f 100644 GIT binary patch delta 1315 zcmaKrJ!lj`6vroMJ~lDtt4ly9Tf{wbY%CPPi)SH1PCOOz!Ihb8CYMY0cGsD?Boe{} zEbTnB6bs9MXj;=5tnH#Af}&P}AOx)RKRX+<7u16v_jc!f{NH=Co3opgy+i|;`q@;s zCKW`Lsdg7Wj!RX4T2*|v%oWn$onuQSYD&6mRo~81=4pvHP(Diqmo;CR5=Ms`)s$mb zxsnDFjln;NYHG$rMu}kA$SgXq@Us;=!_APft6!3T7pV?Efaxec|GnRi|_47UBht zU(ToZiEL)CLkpqM(N1dF`dLe8?74+jCGODm?boAi#~Q<9PcBxfQ79B$Vk^w`Sx|Nb ZU+^&@*R-A^X@{ErN|FnkYwV8YegiP0edhoG delta 216 zcmZojXh@JsG6?hZRWR2xVE_UF28Io5wnb#LFfjOYZeg?GZE)ZV=;xj6>RST<&)O^K diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java index a44153bf00..b3bd421435 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java @@ -1,7 +1,8 @@ package com.coderising.litestruts; import java.util.Map; - +import java.util.HashMap; +import java.lang.reflect.Method; public class Struts { public static View runAction(String actionName, Map parameters) { @@ -9,8 +10,6 @@ public static View runAction(String actionName, Map parameters) { /* 0. 读取配置文件struts.xml*/ - String targetClassName = XmlUtil.parseXML("struts.xml", actionName); - /* 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 @@ -27,6 +26,35 @@ public static View runAction(String actionName, Map parameters) { 放到View对象的jsp字段中。 */ + try { + String targetClassName = XmlUtil.parseXML("struts.xml", actionName); + Class targetClass = Class.forName(targetClassName); + + Method setName = targetClass.getMethod("setName", String.class); + Method setPassword = targetClass.getMethod("setPassword", String.class); + Object object = targetClass.newInstance(); + + setName.invoke(object, parameters.get("name")); + setPassword.invoke(object, parameters.get("password")); + + Method execute = targetClass.getMethod("execute"); + String result = (String)execute.invoke(object); + + Method getMessage = targetClass.getMethod("getMessage"); + String message = (String)getMessage.invoke(object); + + Map params = new HashMap(); + params.put("message", message); + String jspUrl = XmlUtil.getJspUrl("struts.xml", actionName, result); + View view = new View(); + view.setJsp(jspUrl); + view.setParameters(params); + return view; + + } catch (Exception e) { + e.printStackTrace(); + } + return null; } diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java index 46c673daf9..d200452cc8 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java @@ -30,4 +30,30 @@ public static String parseXML(String filePath, String actionName) { return null; } + public static String getJspUrl(String filePath, String actionName, String resultName) { + try { + File file = new File(filePath); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { + Element element = (Element)iter.next(); + Attribute nameAttr = element.attribute("name"); + if (nameAttr.getValue().equals(actionName)) { + for (Iterator ite = element.elementIterator("result"); ite.hasNext();) { + Element ele = (Element)ite.next(); + Attribute resultAttr = ele.attribute("name"); + if (resultAttr.getValue().equals(resultName)) { + return ele.getText(); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + } diff --git a/group11/1178243325/DataStructure/src/main/resources/struts.xml b/group11/1178243325/DataStructure/src/main/resources/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1178243325/DataStructure/struts.xml b/group11/1178243325/DataStructure/struts.xml index ae0ce37fd8..0582b7d4ea 100644 --- a/group11/1178243325/DataStructure/struts.xml +++ b/group11/1178243325/DataStructure/struts.xml @@ -1,10 +1,10 @@ - + /jsp/homepage.jsp /jsp/showLogin.jsp - + /jsp/welcome.jsp /jsp/error.jsp