From 3b183e2834dcd9a3178bd2151dd357eed4b72b3e Mon Sep 17 00:00:00 2001 From: xiongrui233 Date: Sun, 19 Mar 2017 20:02:34 +0800 Subject: [PATCH 1/2] complete struts --- .../src/com/coderising/array/ArrayUtil.java | 96 +++++++++++++++ .../coderising/litestruts/LoginAction.java | 39 ++++++ .../src/com/coderising/litestruts/Struts.java | 112 ++++++++++++++++++ .../com/coderising/litestruts/StrutsTest.java | 62 ++++++++++ .../src/com/coderising/litestruts/View.java | 23 ++++ .../src/com/coderising/litestruts/struts.xml | 11 ++ .../627559964/src/com/coding/basic/List.java | 1 + 7 files changed, 344 insertions(+) create mode 100644 group22/627559964/src/com/coderising/array/ArrayUtil.java create mode 100644 group22/627559964/src/com/coderising/litestruts/LoginAction.java create mode 100644 group22/627559964/src/com/coderising/litestruts/Struts.java create mode 100644 group22/627559964/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group22/627559964/src/com/coderising/litestruts/View.java create mode 100644 group22/627559964/src/com/coderising/litestruts/struts.xml diff --git a/group22/627559964/src/com/coderising/array/ArrayUtil.java b/group22/627559964/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f4b5e8649b --- /dev/null +++ b/group22/627559964/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +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){ + + } + + /** + * 现在有如下的?个数组: 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){ + return null; + } + + /** + * 给定两个已经排序好的整形数组? 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){ + return null; + } + /** + * 把一个已经存满数据的数组 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){ + return null; + } + + /** + * 斐波那契数列为: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){ + return null; + } + + /** + * 返回小于给定?大?max的所有素数数? + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * ?谓?完数?, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定?个最大?max? 返回?个数组, 数组中是小于max 的所有完? + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数? array给连接起? + * 例如array= [3,8,9], seperator = "-" + * 则返回?为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group22/627559964/src/com/coderising/litestruts/LoginAction.java b/group22/627559964/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..eef38d702e --- /dev/null +++ b/group22/627559964/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/group22/627559964/src/com/coderising/litestruts/Struts.java b/group22/627559964/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..5eef643ff1 --- /dev/null +++ b/group22/627559964/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,112 @@ +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.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + + 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字段中?? + + */ + + //创建view对象 + View view = new View(); + + //读取xml文件的Document对象 + SAXReader reader = new SAXReader(); + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + + //获取根节? + Element root = document.getRootElement(); + //根节点不是struts?,结束方法 + if (!root.getName().equals("struts")) { + return null; + } + //获取action匹配actionName的节? + List children = root.elements("action"); + Element targetElement = null; + for (Element element : children) { + System.out.println("name:" + element.attributeValue("name")); + System.out.println("class" + element.attributeValue("class")); + if (element.attributeValue("name").equals(actionName)) { + targetElement = element; + } + } + //没有name参数?,结束方法 + if (targetElement.attributeCount() <= 0) { + return null; + } + + Class clazz = Class.forName(targetElement.attributeValue("class")); + Object obj = clazz.newInstance(); + Method setName = clazz.getDeclaredMethod("setName", String.class); + Method setPassword = clazz.getDeclaredMethod("setPassword", String.class); + Method execute = clazz.getDeclaredMethod("execute"); + setName.invoke(obj, parameters.get("name")); + setPassword.invoke(obj, parameters.get("password")); + String remsg = (String) execute.invoke(obj); + System.out.println("结果?" + remsg); + + Map parameter = new HashMap(); + Method[] gets = clazz.getDeclaredMethods(); + for (Method method : gets) { + String methodName = method.getName(); + String name = methodName.substring(0,3); + if (name.equals("get")) { + Method getxxx = clazz.getDeclaredMethod(methodName); + String xxx = methodName.substring(3, methodName.length()).toLowerCase(); + String temp = (String) getxxx.invoke(obj); + parameter.put(xxx, temp); + } + } + List targetChilren = targetElement.elements(); + for (Element element : targetChilren) { + String resultName = element.attributeValue("name"); + System.out.println(resultName); + if ("success".equalsIgnoreCase(resultName)) { + view.setJsp(element.getText()); + continue; + } + if ("fail".equalsIgnoreCase(resultName)) { + view.setJsp(element.getText()); + continue; + } + } + view.setParameters(parameter); + + return view; + } + +} diff --git a/group22/627559964/src/com/coderising/litestruts/StrutsTest.java b/group22/627559964/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..37d6840016 --- /dev/null +++ b/group22/627559964/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,62 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = Struts.runAction(actionName,params); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + 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 = null; + try { + view = Struts.runAction(actionName,params); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(view.getJsp()); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group22/627559964/src/com/coderising/litestruts/View.java b/group22/627559964/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group22/627559964/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/group22/627559964/src/com/coderising/litestruts/struts.xml b/group22/627559964/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..171848ecd1 --- /dev/null +++ b/group22/627559964/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group22/627559964/src/com/coding/basic/List.java b/group22/627559964/src/com/coding/basic/List.java index d04fcb7df6..6f8deaac0a 100644 --- a/group22/627559964/src/com/coding/basic/List.java +++ b/group22/627559964/src/com/coding/basic/List.java @@ -13,4 +13,5 @@ public interface List { public int size(); public Iterator iterator(); + } \ No newline at end of file From 8b04bbf2649dab58be0476fd3903ac35346bd294 Mon Sep 17 00:00:00 2001 From: xiongrui233 Date: Sun, 19 Mar 2017 22:13:02 +0800 Subject: [PATCH 2/2] complete ArrayUtil --- .../src/com/coderising/array/ArrayUtil.java | 164 ++++++++++++++---- .../com/coderising/array/TestArrayUtil.java | 69 ++++++++ 2 files changed, 202 insertions(+), 31 deletions(-) create mode 100644 group22/627559964/test/com/coderising/array/TestArrayUtil.java diff --git a/group22/627559964/src/com/coderising/array/ArrayUtil.java b/group22/627559964/src/com/coderising/array/ArrayUtil.java index f4b5e8649b..e97b5f7a7c 100644 --- a/group22/627559964/src/com/coderising/array/ArrayUtil.java +++ b/group22/627559964/src/com/coderising/array/ArrayUtil.java @@ -1,96 +1,198 @@ package com.coderising.array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + public class ArrayUtil { /** - * 给定?个整形数组a , 对该数组的?进行置? - 例如? a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 给定一个整形数组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){ - + public static int[] reverseArray(int[] origin){ + int size = origin.length; + int[] result = new int[size]; + for (int i = 0; i < size; i++) { + int temp = origin[size-1-i]; + result[i] = temp; + } + return result; } /** - * 现在有如下的?个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的?存入一个新的数组,生成的新数组为: + * 现在有如下的一个数组: 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){ - return null; + public static int[] removeZero(int[] oldArray){ + int size = oldArray.length; + int countZero = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + countZero ++; + } + } + int[] result = new int[size - countZero]; + for (int i = 0, j = 0; i < size; i++) { + int temp = oldArray[i]; + if (temp != 0) { + result[j] = temp; + j ++; + } + } + return result; } /** - * 给定两个已经排序好的整形数组? a1和a2 , 创建?个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] ? a3 为[3,4,5,6,7,8] , 注意? 已经消除了重? + * 给定两个已经排序好的整形数组, 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){ - return null; + public static int[] merge(int[] array1, int[] array2){ + int[] result = new int[array1.length + array2.length]; + System.arraycopy(array1, 0, result, 0, array1.length); + System.arraycopy(array2, 0, result, array1.length, array2.length); + Arrays.sort(result); + return result; } /** * 把一个已经存满数据的数组 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){ - return null; + public static int[] grow(int[] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; } /** - * 斐波那契数列为:1?1?2?3?5?8?13?21...... ,给定一个最大?, 返回小于该?的数列 - * 例如? max = 15 , 则返回的数组应该? [1?1?2?3?5?8?13] + * 斐波那契数列为: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){ - return null; + public static int[] fibonacci(int max){ + List resultList = new ArrayList(); + int temp = 0; + for (int i = 1; i <= max; i++) { + temp = getFibo(i); + if (temp > max) { + break; + } + resultList.add(temp); + } + return getArrayFromList(resultList); + } + /** + * 计算斐波那契数列 + * @param i + * @return + */ + private static int getFibo(int i) { + if (i == 1 || i == 2) { + return 1; + } else { + return getFibo(i - 1) + getFibo(i - 2); + } + } + + /** + * 通过list过得int[]数组 + * @param list + * @return + */ + private static int[] getArrayFromList(List list) { + int[] result = new int[list.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = list.get(i); + } + return result; } /** - * 返回小于给定?大?max的所有素数数? + * 返回小于给定最大值max的所有素数数组 * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] * @param max * @return */ - public int[] getPrimes(int max){ - return null; + public static int[] getPrimes(int max){ + List result = new ArrayList(); + boolean isPrime = true; + for (int i = 2; i < max; i++) { + for (int j = 2; j < i; j++) { + if (i % j == 0) { + isPrime = false; + break; + } + } + if (isPrime) { + result.add(i); + } + isPrime = true; + } + + return getArrayFromList(result); } /** - * ?谓?完数?, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定?个最大?max? 返回?个数组, 数组中是小于max 的所有完? + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 * @param max * @return */ - public int[] getPerfectNumbers(int max){ - return null; + public static int[] getPerfectNumbers(int max){ + List result = new ArrayList(); + for (int i = 1; i <= max; i++) { + int temp = 0; + for (int j = 1; j < i/2 + 1; j++) { + if(i%j == 0) { + temp += j; + } + } + if (temp == i) { + result.add(i); + } + } + return getArrayFromList(result); } /** - * 用seperator 把数? array给连接起? + * 用seperator 把数组 array给连接起来 * 例如array= [3,8,9], seperator = "-" - * 则返回?为"3-8-9" + * 则返回值为"3-8-9" * @param array * @param s * @return */ - public String join(int[] array, String seperator){ - return null; + public static String join(int[] array, String seperator){ + List result = Arrays.asList(array); + int size = array.length; + StringBuffer rs = new StringBuffer(); + for (int i = 0; i < size; i++) { + rs.append(result.get(0)[i]); + if (i != size-1) { + rs.append(seperator); + } + } + return rs.toString(); } -} +} \ No newline at end of file diff --git a/group22/627559964/test/com/coderising/array/TestArrayUtil.java b/group22/627559964/test/com/coderising/array/TestArrayUtil.java new file mode 100644 index 0000000000..f597900492 --- /dev/null +++ b/group22/627559964/test/com/coderising/array/TestArrayUtil.java @@ -0,0 +1,69 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Test; + +import com.sun.scenario.effect.Merge; + +public class TestArrayUtil { + + @Test + public void testReverseArray() { + try { + int[] number = {1,2,3}; + Assert.assertEquals("[3, 2, 1]", Arrays.toString(ArrayUtil.reverseArray(number))); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testRemoveZero() { + int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + Assert.assertEquals("[1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5]", Arrays.toString(ArrayUtil.removeZero(oldArr))); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + Assert.assertEquals("[3, 4, 5, 5, 6, 7, 7, 8]", Arrays.toString(ArrayUtil.merge(a1, a2))); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int size = 3; + Assert.assertEquals("[2, 3, 6, 0, 0, 0]", Arrays.toString(ArrayUtil.grow(oldArray, size))); + } + + @Test + public void testFibonacci() { + int max = 15; + Assert.assertEquals("[1, 1, 2, 3, 5, 8, 13]", Arrays.toString(ArrayUtil.fibonacci(max))); + } + + @Test + public void testGetPrimes() { + int max = 23; + System.out.println(Arrays.toString(ArrayUtil.getPrimes(max))); + Assert.assertEquals("[2, 3, 5, 7, 11, 13, 17, 19]", Arrays.toString(ArrayUtil.getPrimes(max))); + } + + @Test + public void testGetPerfectNumbers() { + Assert.assertEquals("[6, 28, 496]", Arrays.toString(ArrayUtil.getPerfectNumbers(1000))); + } + + @Test + public void testJoin() { + int[] array= {3,8,9}; + String seperator = "-"; + Assert.assertEquals("3-8-9", ArrayUtil.join(array, seperator)); + } +}