From e6342be173d0c080a811096d9c29e48813c116e1 Mon Sep 17 00:00:00 2001 From: zg Date: Mon, 6 Mar 2017 22:23:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group14/254659936/.gitignore | 1 + .../com/coderising/litestruts/struts.xml | 11 + group14/254659936/src/Main.java | 7 + .../src/com/coderising/array/ArrayUtil.java | 261 ++++++++++++++++++ .../coderising/litestruts/LoginAction.java | 39 +++ .../src/com/coderising/litestruts/Struts.java | 150 ++++++++++ .../com/coderising/litestruts/StrutsTest.java | 43 +++ .../src/com/coderising/litestruts/View.java | 23 ++ .../src/com/coderising/litestruts/struts.xml | 11 + 9 files changed, 546 insertions(+) create mode 100644 group14/254659936/out/production/254659936/com/coderising/litestruts/struts.xml create mode 100644 group14/254659936/src/com/coderising/array/ArrayUtil.java create mode 100644 group14/254659936/src/com/coderising/litestruts/LoginAction.java create mode 100644 group14/254659936/src/com/coderising/litestruts/Struts.java create mode 100644 group14/254659936/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group14/254659936/src/com/coderising/litestruts/View.java create mode 100644 group14/254659936/src/com/coderising/litestruts/struts.xml diff --git a/group14/254659936/.gitignore b/group14/254659936/.gitignore index 5561991601..50039721f2 100644 --- a/group14/254659936/.gitignore +++ b/group14/254659936/.gitignore @@ -1,6 +1,7 @@ *.iml .gradle .idea +lib /local.properties /.idea/workspace.xml /.idea/libraries diff --git a/group14/254659936/out/production/254659936/com/coderising/litestruts/struts.xml b/group14/254659936/out/production/254659936/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..8a9789665d --- /dev/null +++ b/group14/254659936/out/production/254659936/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group14/254659936/src/Main.java b/group14/254659936/src/Main.java index 4d4fbb71ef..f7d10617e3 100644 --- a/group14/254659936/src/Main.java +++ b/group14/254659936/src/Main.java @@ -1,7 +1,14 @@ +import com.coderising.litestruts.Struts; + +import java.util.HashMap; public class Main { public static void main(String[] args) { System.out.println("Hello World!"); + HashMap parameters = new HashMap<>(); + parameters.put("name", "zg"); + parameters.put("password", "123456"); + Struts.runAction("login", parameters); } } diff --git a/group14/254659936/src/com/coderising/array/ArrayUtil.java b/group14/254659936/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..2f2987465b --- /dev/null +++ b/group14/254659936/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,261 @@ +package com.coderising.array; + +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (null == origin) { + return; + } + int first = 0; + int last = origin.length - 1; + int temp; + while (first < last) { + temp = origin[first]; + origin[first] = origin[last]; + origin[last] = temp; + first++; + last++; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int[] resultArr = null; + if (null == oldArray) { + return resultArr; + } + int zeroSize = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + zeroSize++; + } + } + resultArr = new int[oldArray.length - zeroSize]; + int resultIndex = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + resultArr[resultIndex] = oldArray[i]; + resultIndex++; + } + } + return resultArr; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + if (null == array1) { + return array2; + } + if (null == array2) { + return array1; + } + int[] resultArr = new int[array1.length + array2.length]; + int index1 = 0; + int index2 = 0; + for (int i = 0; i < resultArr.length; i++) { + if (array1[index1] < array2[index2]) { + resultArr[i] = array1[index1]; + index1++; + } else { + resultArr[i] = array2[index2]; + index2++; + } + } + return resultArr; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + if (oldArray == null) { + return new int[size]; + } + int[] resultArr = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + resultArr[i] = oldArray[i]; + } + return resultArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + ArrayList tempList = new ArrayList<>(); + int resultSize = 0; + while (fibonacci(tempList, resultSize) < max) { + resultSize++; + } + int[] resultArr = new int[resultSize]; + for (int i = 0; i < resultSize; i++) { + resultArr[i] = tempList.get(i); + } + return resultArr; + } + + /** + * 返回第index个斐波那契数列,resultArr用来存已经计算过的结果 + * + * @param resultArr + * @param index + * @return + */ + private int fibonacci(ArrayList resultArr, int index) { + if (resultArr.size() > index) { + return resultArr.get(index); + } + int newResult; + if (index == 0) { + newResult = 1; + } else if (index == 1) { + newResult = 1; + } else { + newResult = (resultArr.get(index - 1) == 0 ? resultArr.get(index - 1) : fibonacci(resultArr, index - 1)) + + (resultArr.get(index - 2) == 0 ? resultArr.get(index - 2) : fibonacci(resultArr, index - 2)); + } + resultArr.add(newResult); + return newResult; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max < 2) { + return null; + } + ArrayList tempList = new ArrayList<>(); + for (int i = 2; i <= max; i++) { + if (isPrimes(tempList, i)) { + tempList.add(i); + } + } + int[] resultArr = new int[tempList.size()]; + for (int i = 0; i < tempList.size(); i++) { + resultArr[i] = tempList.get(i); + } + return resultArr; + } + + private boolean isPrimes(ArrayList primesList, int temp) { + if (temp == 2 || temp == 3) { + return true; + } + for (int i = 0; i < primesList.size(); i++) { + if (temp % primesList.get(i) == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max < 0) { + return null; + } + ArrayList tempList = new ArrayList<>(); + for (int i = 2; i <= max; i++) { + if (isPerfectNumber(i)) { + tempList.add(i); + } + } + int[] resultArr = new int[tempList.size()]; + for (int i = 0; i < tempList.size(); i++) { + resultArr[i] = tempList.get(i); + } + return resultArr; + } + + private boolean isPerfectNumber(int temp) { + if (temp == 1) { + return true; + } + int sum = 0; + for (int i = 1; i < temp; i++) { + if (temp % 1 == 0) { + if ((sum = sum + i) > temp) { + return false; + } + } + } + if (sum == temp) { + return true; + } + return false; + } + + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param separator + * @return + */ + public String join(int[] array, String separator) { + if (null == array || array.length == 0 || null == separator) { + return null; + } + StringBuilder sb = new StringBuilder(array.length * (1 + separator.length())); + int i = 1; + sb.append(array[0]); + for (; i < array.length; i++) { + sb.append(separator); + sb.append(array[i]); + } + return sb.toString(); + } + +} diff --git a/group14/254659936/src/com/coderising/litestruts/LoginAction.java b/group14/254659936/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group14/254659936/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +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/group14/254659936/src/com/coderising/litestruts/Struts.java b/group14/254659936/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..76b1d8e531 --- /dev/null +++ b/group14/254659936/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,150 @@ +package com.coderising.litestruts; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + // 0. 读取配置文件struts.xml + Element rootXmlElement = getRootXmlElement(); + + String className = getClassFromAction(actionName, rootXmlElement); + + View view = new View(); + + try { + Class actionClass = null; + actionClass = Class.forName(className); + Object actionIns = null; + actionIns = actionClass.newInstance(); + + Method[] methods = actionClass.getMethods(); + Set> entries = parameters.entrySet(); + // 遍历map,调用set方法 + for (Map.Entry map : entries) { + String key = "set" + map.getKey(); + String value = map.getValue(); + Method method = null; + for (int i = 0; i < methods.length; i++) { + if (key.equalsIgnoreCase(methods[i].getName())) { + method = methods[i]; + break; + } + } + method.invoke(actionIns, value); + System.out.println("execute set method:" + key + " " + value); + } + + // 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method method = actionClass.getMethod("execute"); + Object executeResult = method.invoke(actionIns); + System.out.println("execute method result:" + executeResult.toString()); + + // 3. 通过反射找到对象的所有getter方法(例如 getMessage), + // 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + // 放到View对象的parameters + HashMap viewParametersMap = new HashMap<>(); + String methodName; + Object methodResult; + for (int i = 0; i < methods.length; i++) { + methodName = methods[i].getName(); + if (methodName.startsWith("get")) { + methodResult = methods[i].invoke(actionIns); + viewParametersMap.put((char) (methodName.charAt(3) - 32) + + methodName.substring(3, methodName.length() - 1), + methodResult); + + System.out.println("execute get method:" + methodName + " " + methodResult); + } + } + view.setParameters(viewParametersMap); + + // 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + // 放到View对象的jsp字段中。 + view.setJsp("" + executeResult); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return view; + } + + private static Element getRootXmlElement() { + SAXReader sax = new SAXReader(); + File xmlFile = new File("src/com/coderising/litestruts/struts.xml"); + Document document = null; + try { + document = sax.read(xmlFile); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element root = document.getRootElement(); + System.out.println("root:" + root.getName()); + return root; + } + + private static String getClassFromAction(String actionName, Element root) { + if (null == actionName || actionName.isEmpty() || null == root) { + return null; + } + List elements = root.elements(); + for (Element element : elements) { + Attribute actionAttribute = element.attribute("name"); + if (actionName.equals(actionAttribute.getValue())) { + Attribute classAttribute = element.attribute("class"); + + + List elements1 = element.elements(); + elements1.get(0).attribute("name").getValue(); + elements1.get(0).getData(); + + + return classAttribute.getValue(); + } + } + return null; + } + + /* + + 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字段中。 + + */ + + +} diff --git a/group14/254659936/src/com/coderising/litestruts/StrutsTest.java b/group14/254659936/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..0bab615458 --- /dev/null +++ b/group14/254659936/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group14/254659936/src/com/coderising/litestruts/View.java b/group14/254659936/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group14/254659936/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +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/group14/254659936/src/com/coderising/litestruts/struts.xml b/group14/254659936/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..8a9789665d --- /dev/null +++ b/group14/254659936/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file