From 7996ecb0b66771b9dc2eac098a835259a58336b5 Mon Sep 17 00:00:00 2001 From: gongxun Date: Sat, 4 Mar 2017 20:50:50 +0800 Subject: [PATCH] part2 -- parsing the configuration file of struts --- group17/785396327/3.5/struts/LoginAction.java | 39 +++++++++ .../785396327/3.5/struts/LoginActionTest.java | 36 ++++++++ group17/785396327/3.5/struts/Struts.java | 86 +++++++++++++++++++ group17/785396327/3.5/struts/StrutsUtils.java | 30 +++++++ group17/785396327/3.5/struts/View.java | 26 ++++++ group17/785396327/3.5/struts/struts.xml | 11 +++ 6 files changed, 228 insertions(+) create mode 100644 group17/785396327/3.5/struts/LoginAction.java create mode 100644 group17/785396327/3.5/struts/LoginActionTest.java create mode 100644 group17/785396327/3.5/struts/Struts.java create mode 100644 group17/785396327/3.5/struts/StrutsUtils.java create mode 100644 group17/785396327/3.5/struts/View.java create mode 100644 group17/785396327/3.5/struts/struts.xml diff --git a/group17/785396327/3.5/struts/LoginAction.java b/group17/785396327/3.5/struts/LoginAction.java new file mode 100644 index 0000000000..3fc624ff90 --- /dev/null +++ b/group17/785396327/3.5/struts/LoginAction.java @@ -0,0 +1,39 @@ +package struts; + +/** + * Created by IBM on 2017/3/4. + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + 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/group17/785396327/3.5/struts/LoginActionTest.java b/group17/785396327/3.5/struts/LoginActionTest.java new file mode 100644 index 0000000000..a2ce2379a5 --- /dev/null +++ b/group17/785396327/3.5/struts/LoginActionTest.java @@ -0,0 +1,36 @@ +package struts; + +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Assert; +import org.junit.Test; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by IBM on 2017/3/4. + */ +public class LoginActionTest { + + @Test + public void fun1() throws DocumentException { + InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("struts/struts.xml"); + SAXReader reader = new SAXReader(); + Element rootElement = reader.read(resourceAsStream).getRootElement(); + Element selectedElement = (Element) rootElement.selectSingleNode("//action[@name='login']"); + System.out.println(selectedElement.attributeValue("class")); + + } + + @Test + public void fun2() { + Map parameters = new HashMap(); + parameters.put("name", "test"); + parameters.put("password", "1234"); + View view = Struts.runAction("login", parameters); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + } +} diff --git a/group17/785396327/3.5/struts/Struts.java b/group17/785396327/3.5/struts/Struts.java new file mode 100644 index 0000000000..c351b4473e --- /dev/null +++ b/group17/785396327/3.5/struts/Struts.java @@ -0,0 +1,86 @@ +package struts; + +import org.dom4j.Element; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by william on 2017/3/4. + */ +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + Element root = StrutsUtils.getRoot("struts/struts.xml"); + View view = null; + if (root != null) { + Element selectedEle = (Element) root.selectSingleNode("//action[@name='" + actionName + "']"); + if (selectedEle != null) { + Class clazz = genClass(selectedEle.attributeValue("class")); + Object target = setValue(parameters, clazz); + String result; + try { + result = (String) clazz.getMethod("execute").invoke(target); + } catch (Exception e) { + throw new RuntimeException("invoke execute have some error", e); + } + Map response = getValue(clazz, target); + view = new View(); + view.setParameters(response); + Element selectedResult = (Element) root.selectSingleNode("//action[@name='" + actionName + "']//result[@name='" + result + "']"); + view.setJsp(selectedResult == null ? null : selectedResult.getText()); + } + } + return view; + } + + + private static Class genClass(String className) { + Class clazz = null; + try { + clazz = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return clazz; + } + + private static Object setValue(Map parameters, Class clazz) { + try { + Object target = clazz.newInstance(); + if (!StrutsUtils.isEmpty(parameters)) { + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + if (!StrutsUtils.isEmpty(key)) { + String setterName = new StringBuilder("set").append(key.substring(0, 1).toUpperCase()).append(key.substring(1, key.length())).toString(); + clazz.getMethod(setterName, String.class).invoke(target, entry.getValue()); + } + } + } + return target; + } catch (Exception e) { + throw new RuntimeException("create class instance have some error ", e); + } + } + + private static Map getValue(Class clazz, Object target) { + Map resultsMap = new HashMap(); + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + String fieldName = method.getName(); + if (fieldName.startsWith("get") && !fieldName.equals("getClass")) { + try { + Object value = method.invoke(target); + resultsMap.put(new StringBuilder(fieldName.substring(3, 4)).append(fieldName.substring(4, fieldName.length())).toString(), value); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return resultsMap; + } + + +} diff --git a/group17/785396327/3.5/struts/StrutsUtils.java b/group17/785396327/3.5/struts/StrutsUtils.java new file mode 100644 index 0000000000..dfe5fe614f --- /dev/null +++ b/group17/785396327/3.5/struts/StrutsUtils.java @@ -0,0 +1,30 @@ +package struts; + +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.InputStream; + +/** + * Created by IBM on 2017/3/4. + */ +public class StrutsUtils { + + public static Element getRoot(String filePath) { + try { + InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); + SAXReader saxReader = new SAXReader(); + return saxReader.read(resourceAsStream).getRootElement(); + } catch (DocumentException e) { + throw new RuntimeException("初始化文件异常", e); + } + } + + public static boolean isEmpty(Object obj) { + if (obj instanceof String) + return obj == null || obj.equals(""); + else + return obj == null; + } +} diff --git a/group17/785396327/3.5/struts/View.java b/group17/785396327/3.5/struts/View.java new file mode 100644 index 0000000000..a02a5d1216 --- /dev/null +++ b/group17/785396327/3.5/struts/View.java @@ -0,0 +1,26 @@ +package struts; + +import java.util.Map; + +/** + * Created by IBM on 2017/3/4. + */ +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group17/785396327/3.5/struts/struts.xml b/group17/785396327/3.5/struts/struts.xml new file mode 100644 index 0000000000..0c69b730f3 --- /dev/null +++ b/group17/785396327/3.5/struts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file