diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java b/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java index 1005f35a29..904daea228 100644 --- a/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java +++ b/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java @@ -14,9 +14,21 @@ public String getName() { return name; } + public void setName(String name){ + this.name = name; + } + public String getPassword() { return password; } + + public void setPassword(String password){ + this.password = password; + } + + public String getMessage(){ + return this.message; + } public String execute(){ if("test".equals(name) && "1234".equals(password)){ @@ -26,14 +38,4 @@ public String execute(){ this.message = "login failed,please check your user/pwd"; return "fail"; } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } } diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java b/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java index 44cc35bf01..cb20956a88 100644 --- a/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java +++ b/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java @@ -1,34 +1,57 @@ package com.coderising.litestruts; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; import java.util.Map; - - +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - + public static View runAction(String actionName, Map parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + //创建SAXReader读取器,专门用于读取xml + SAXReader saxReader = new SAXReader(); + Document document = null; + try { + document = saxReader.read(new File("D:/DemoSpace/coding2017/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + //根据节点名称找节点 + Node node = root.selectSingleNode("action[@name='"+actionName+"']"); + String classPath = ((Element) node).attributeValue("class"); + //根据类名反射实例化 + Class onwClass = Class.forName(classPath); + Object o = onwClass.newInstance(); + Method setName = onwClass.getMethod("setName",String.class); + Method setPassword = onwClass.getMethod("setPassword",String.class); + Method execute = onwClass.getMethod("execute"); + Method getName = onwClass.getMethod("getName"); + Method getPassword = onwClass.getMethod("getPassword"); + Method getMessage = onwClass.getMethod("getMessage"); + setName.invoke(o,parameters.get("name")); + setPassword.invoke(o,parameters.get("password")); + String result = (String) execute.invoke(o); + //组装params参数 + HashMap map = new HashMap<>(); + String name = (String) getName.invoke(o); + String password = (String) getPassword.invoke(o); + String message = (String) getMessage.invoke(o); + map.put("name", name); + map.put("password", password); + map.put("message", message); + //组装view数据 + View view = new View(); + view.setParameters(map); + //根据execute的返回值,找对应的jsp页面路径 + String jspPath = node.valueOf("//result[@name='"+result+"']"); + view.setJsp(jspPath); + return view; + } catch (DocumentException e) { + e.printStackTrace(); + } return null; - } - + } } diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java index a44c1878ac..2a8ed76022 100644 --- a/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java +++ b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java @@ -1,19 +1,16 @@ package com.coderising.litestruts; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.junit.Assert; import org.junit.Test; - - - - public class StrutsTest { @Test - public void testLoginActionSuccess() { + public void testLoginActionSuccess() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { String actionName = "login"; @@ -29,7 +26,7 @@ public void testLoginActionSuccess() { } @Test - public void testLoginActionFailed() { + public void testLoginActionFailed() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { String actionName = "login"; Map params = new HashMap(); params.put("name","test"); diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml index 99063bcb0c..e22003fc12 100644 --- a/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml +++ b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml @@ -1,11 +1,11 @@ - + /jsp/homepage.jsp /jsp/showLogin.jsp - /jsp/welcome.jsp - /jsp/error.jsp + /jsp/welcome.jsp + /jsp/error.jsp - \ No newline at end of file + \ No newline at end of file