From 72f97ceebc43364f27f547758bcdd3396ee3d226 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Tue, 28 Feb 2017 19:54:59 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E7=83=AD=E8=BA=AB=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/array/.ArrayUtil.java.swp | Bin 0 -> 16384 bytes .../java/com/coderising/array/ArrayUtil.java | 229 ++++++++++++++++++ .../coderising/litestruts/LoginAction.java | 39 +++ .../com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + 7 files changed, 379 insertions(+) create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/array/.ArrayUtil.java.swp create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/array/.ArrayUtil.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/.ArrayUtil.java.swp new file mode 100644 index 0000000000000000000000000000000000000000..562760a67f7d3e5b0175ec4f8b8c72c563fdb0e7 GIT binary patch literal 16384 zcmeI3e{38_700&?&|V1vQKV9V)Jag%ICu7W@9a2f;;26&pdhLsl(wol(8gXn>z?n- z-8ny-VyF2X|44)L+ew?BBn@_)Ha{FYX_Nw`LZB)LwLqmRgxj+tN`E0#6^h{d-t4UR zoU`5X7gE;I$G+X2c{B6o&6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Qteyq{to-_1@?xg>z7T^psNCV}?3 zGugh*>_FS#nlrhbC+Jy!2g;4R-_jys7Z#{jYf<^bs`>(ZFRy>)mf=EZrI*Szk-sg6 zL_OO~q1~BGq#m2LYUz5WG~Jmn;4&C68FBe40WkjWrMMq0#r@fErx2h{E4zp5;~@)xw}sOiA%Lr^+Avk+Oxf7B z4ZBVQb?p=9aHbfJRh!StkWzL@nT4~^s^dmFEm#xuJ$MR28jb45jXG0QCmTxWOZV~{ z^Ci4<%+culAH>=71Ds3g{2zA?gzw^ve+9e*PJ``W6{rQ@0$&GrgAZ}Ge-r!({0jU6 z{2c57?VtrL230@@#UKbMFYq?b|IdSiU z@2X&)5~~HHa&IY5YESn6{w6DqeH^*iloM&?-N*2#F9kw$mg+`RBcpREMARp(g!hmd z63&C+K_jpq=x0)~wDv)YP!ZOp)>(VRW=&C63hj7OWYP3; zfD_bWq&3qZVxrE>6f=p5~k-{nu)zHVeC z^;xg-CMHfHY1~g3Uckttki>%U{sw3*n*VY6 zJyEO|1@o>Wd3%s_h2%MDUUZrb`Vjlu`RSX;2Bh!gg0w$4+lP=f k=khyL)tqy)pg0=5d>IHKavgF5o<)es9Mf&mOcZJV1$= 0 && j < length; i--, j++) + origin[j] = temp[i]; + } + + /** + * 现在有如下的一个数组: 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 static int[] removeZero(int[] oldArray){ + if (oldArray == null) { + return null; + } + + int zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeroCount++; + } + int[] newArray = new int[oldArray.length-zeroCount]; + for (int i = 0, j = 0; i < oldArray.length && j < newArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, 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 static int[] merge(int[] array1, int[] array2){ + if (array1 == null && array2 == null) + return null; + int index1 = 0, index2 = 0; + int[] array3 = new int[array1.length + array2.length]; + int index = 0; + while (index1 != array1.length && index2 != array2.length) { + if (array1[index1] < array2[index2]) { + array3[index++] = array1[index1++]; + } else if (array1[index1] > array2[index2]) { + array3[index++] = array2[index2++]; + } else if (array1[index1] == array2[index2]){ + array3[index++] = array1[index1++]; + index2++; + } + } + + if (index1 == array1.length && index2 != array2.length) { + for (int i = index2; i < array2.length; i++) + array3[index++] = array2[i]; + } else if (index2 == array2.length && index1 != array1.length) { + for (int i = index1; i < array1.length; i++) { + array3[index++] = array1[i]; + } + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array3[i]; + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + if (size <= 0) + return null; + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max < 1) + return null; + if (max == 1) + return null; + int[] array = new int[max]; + int i = 0; + int value = fibonaccis(i+1); + while ( value < max) { + array[i++] = value; + value = fibonaccis(i+1); + } + int[] newArray = new int[i]; + for (int j = 0; j < newArray.length; j++) { + newArray[j] = array[j]; + } + return newArray; + } + + private static int fibonaccis(int n) { + if (n <=0) + return 0; + if (n == 1 || n ==2 ) + return 1; + return fibonaccis(n-1)+fibonaccis(n-2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if (max <= 1) { + return null; + } + int[] array = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + if (i == 2 || i == 3 || i == 5 || i == 7) + array[index++] = i; + if (i%2 !=0 && i%3 != 0 && i%5 != 0 && i%7 != 0) + array[index++] = i; + } + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + if (max <= 0) + return null; + int[] array = new int[max]; + int index = 0; + for (int i = 1; i < max; i++) { + if (isPerfectNumber(i)) + array[index++] = i; + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array[i]; + + return newArray; + } + + private static boolean isPerfectNumber(int n) { + int sum = 0; + int i = 1; + while (i < n) { + if (n%i == 0) + sum += i; + i++; + } + if (sum == n) + return true; + return false; + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + if (array == null) + return null; + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length-1) + str.append(array[i]); + else + str.append(array[i] + seperator); + } + return str.toString(); + } + + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/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/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..44cc35bf01 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +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字段中。 + + */ + + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +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 = 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/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/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/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + From 03857fb81f1a9b1bd8a8481e48fdbce357271de8 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Tue, 28 Feb 2017 19:55:21 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataStructure/src/main/java/com/Main.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 group11/1178243325/DataStructure/src/main/java/com/Main.java diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java new file mode 100644 index 0000000000..7b27f11aec --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/Main.java @@ -0,0 +1,51 @@ +package com; + +import com.coderising.array.*; +public class Main { + public static void main(String[] args) { + int[] array = {1, 2, 3, 4, 5}; + System.out.print("reverseArray测试:"); + ArrayUtil.reverseArray(array); + for (int i : array) + System.out.print(i + " "); + System.out.print("\nremoveZero测试:"); + + int[] oldArray = {1, 3, 4, 5, 0, 0, 8 , 0, 9}; + oldArray = ArrayUtil.removeZero(oldArray); + for (int i : oldArray) { + System.out.print(i + " "); + } + + System.out.print("\nmerge测试:"); + int[] a1 = {3, 5,8}; + int[] a2 = {4, 5, 6,7}; + int[] arrays = ArrayUtil.merge(a1, a2); + for (int i : arrays) + System.out.print(i + " "); + + System.out.print("\ngrow测试:"); + + int[] growArray = ArrayUtil.grow(a1, 5); + for (int i : growArray) + System.out.print(i + " "); + + System.out.print("\nfibonacci测试"); + int[] fArray = ArrayUtil.fibonacci(1); + System.out.print(fArray); + System.out.println(); + fArray = ArrayUtil.fibonacci(15); + for (int i : fArray) + System.out.print(i + " "); + System.out.print("\ngetPrimes测试:"); + int[] primesArray = ArrayUtil.getPrimes(23); + for (int i : primesArray) + System.out.print(i + " "); + System.out.print("\ngetPerfectNumbers测试:"); + int[] pArray = ArrayUtil.getPerfectNumbers(100); + for (int i : pArray) + System.out.print(i + " "); + System.out.print("\njoin测试:"); + int[] jArray = new int[]{2, 3, 8}; + System.out.print(ArrayUtil.join(jArray, "-")); + } +} From fb798cf9fd84f1f26d6d5a8b048378d01bb96a18 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Wed, 1 Mar 2017 19:57:15 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E8=AF=BB=E5=8F=96xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/DataStructure/build.gradle | 12 +++- .../DataStructure/src/main/java/com/Main.java | 2 + .../.Struts.java.swp} | Bin 16384 -> 12288 bytes .../com/coderising/litestruts/Struts.java | 10 +-- .../com/coderising/litestruts/XmlUtil.java | 33 ++++++++++ .../src/main/java/com/coding/Main.java | 58 ------------------ .../java/com/coding/basic/BinaryTreeNode.java | 9 +-- group11/1178243325/DataStructure/struts.xml | 11 ++++ 8 files changed, 67 insertions(+), 68 deletions(-) rename group11/1178243325/DataStructure/src/main/java/com/coderising/{array/.ArrayUtil.java.swp => litestruts/.Struts.java.swp} (55%) create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/Main.java create mode 100644 group11/1178243325/DataStructure/struts.xml diff --git a/group11/1178243325/DataStructure/build.gradle b/group11/1178243325/DataStructure/build.gradle index 5c3694d8ec..9c6bc859e6 100644 --- a/group11/1178243325/DataStructure/build.gradle +++ b/group11/1178243325/DataStructure/build.gradle @@ -1,10 +1,20 @@ + apply plugin: 'java' apply plugin: 'eclipse' jar { + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }} manifest { - attributes 'Main-Class' : 'com.coding.Main' + attributes 'Main-Class' : 'com.Main' } } +repositories { + mavenCentral() +} + +dependencies { + compile 'junit:junit:4.12' + compile 'dom4j:dom4j:1.6.1' +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java index 7b27f11aec..c5d56779c1 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 com.coderising.litestruts.*; import com.coderising.array.*; public class Main { public static void main(String[] args) { @@ -47,5 +48,6 @@ 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(); } } diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/array/.ArrayUtil.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp similarity index 55% rename from group11/1178243325/DataStructure/src/main/java/com/coderising/array/.ArrayUtil.java.swp rename to group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp index 562760a67f7d3e5b0175ec4f8b8c72c563fdb0e7..5ff0f0622e0264939929151bb724ed26c12a7bb7 100644 GIT binary patch literal 12288 zcmeI2&u<$=6vwAMVHFC3KY;1>l2m3lwu4fUwh}5xMXCV_wF)=3o5kJYAFJKjwo(ek{&8JAt8Z~IM5TGwR_5yBj52laavUsSClu(cgLA| z^WK}!%pNSiQ#~hNOY=Uxm!UnwSoP-O!08dj-q^#K?|M#NsENn7|7!PsWN4f*&zgo+ z7_`;H$!C&1i9RFa6#_SrFcOKrgUQ}hD*2-EiYd&ugcn#M@NC2PEF)(+`OW3ImJ84E z;lao{!uDY*d_%1ezOI5iK5d#M0y~F5Al`l9;9izWCVJGS_iOw3bI+dGIs2!b5&@uP9!kIVvM5=sRvV!m}jFlHqY}xHrpSKw4Q42fU)(F#bV*qx*V;ZbnJ7ovfNy_ z+L#?V4PMa=| z^ZNS7^4np4LCe{`Z)R-mCC;_R7d5#y4Qn}mMH_+@N1T+-9|$$30Byku$BN;XV;Nf@ zt8ivH{9$@yb+qjqqv^=|#_Cw)4E~Z2`it`3@~2RVz~-C2N?a~4Hb41Mu3z5HE{dS} zfn_0r2DggH@Heu$AXlcEl{OCU+v0T~0}On6)Aapw1#d9&jRjp=9+TQ(O%+9JYQLJ> zw!vT`nd;TL)hJr)L?+6e6NnBxoUY;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String targetClassName = XmlUtil.parseXML("struts.xml", actionName); + + /* 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , "password"="1234") , @@ -30,5 +30,5 @@ public static View runAction(String actionName, Map parameters) { 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 new file mode 100644 index 0000000000..46c673daf9 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java @@ -0,0 +1,33 @@ +package com.coderising.litestruts; + +import java.io.*; +import java.util.*; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; +public class XmlUtil { + + public static String parseXML(String filePath, String actionName) { + 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)) { + Attribute classAttr = element.attribute("class"); + return classAttr.getValue(); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.out.println("parse error"); + } + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java b/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java deleted file mode 100644 index 3260081ca1..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.coding; - -import com.coding.basic.*; -public class Main { - public static void main(String[] args) { - ArrayList list = new ArrayList(); - - list.add(0, "2xxx"); - list.add(1, "we"); - list.add(2, "sss"); - list.add("xing"); - list.remove(2); - System.out.println(list.get(2)); - Iterator iterator = list.iterator(); - while(iterator.hasNext()) { - System.out.println(iterator.next()); - } - System.out.println(list.size()); - - LinkedList llist = new LinkedList(); - llist.add("hu"); - llist.add("zhao"); - llist.add(2,"xing"); - llist.addFirst("身骑白马"); - llist.addLast("德州小老虎"); - llist.add(5, "sf"); - llist.remove(5); - llist.removeFirst(); - llist.removeLast(); - for (int i = 2; i >=0; i--) - System.out.print(llist.get(i)); - System.out.println(llist.size()); - - Iterator literator = llist.iterator(); - while(literator.hasNext()) { - System.out.println(literator.next()); - } - - Stack stack = new Stack(); - stack.push(1); - stack.push(2); - stack.push(3); - stack.push(4); - System.out.println(stack.peek()); - while(!stack.isEmpty()) - System.out.println(stack.pop()); - - Queue queue = new Queue(); - queue.enQueue(1); - queue.enQueue(2); - queue.enQueue(3); - System.out.println(queue.size()); - while (!queue.isEmpty()) { - System.out.println(queue.deQueue()); - } - - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java index 266eff3d56..1cf38aee30 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -9,24 +9,25 @@ public class BinaryTreeNode { public Object getData() { return data; } + public void setData(Object data) { this.data = data; } + public BinaryTreeNode getLeft() { return left; } + public void setLeft(BinaryTreeNode left) { this.left = left; } + public BinaryTreeNode getRight() { return right; } + public void setRight(BinaryTreeNode right) { this.right = right; } - public BinaryTreeNode insert(Object o){ - return null; - } - } diff --git a/group11/1178243325/DataStructure/struts.xml b/group11/1178243325/DataStructure/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + From 0270497f0e8ad83e3c281e28f9d08c3d4aa3c1a6 Mon Sep 17 00:00:00 2001 From: Pan Date: Sun, 5 Mar 2017 19:39:01 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/252308879/dataStructure/pom.xml | 10 ++ .../coding2017/parsingXML/LoginAction.java | 39 +++++ .../org/pan/coding2017/parsingXML/Struts.java | 34 ++++ .../pan/coding2017/parsingXML/StrutsTest.java | 43 +++++ .../org/pan/coding2017/parsingXML/View.java | 23 +++ .../org/pan/coding2017/utils/Dom4JUtil.java | 50 ++++++ .../org/pan/coding2017/utils/JaxpDomUtil.java | 152 ++++++++++++++++++ .../pan/coding2017/utils/JaxpSAXPUtil.java | 89 ++++++++++ .../src/main/resources/struts.xml | 11 ++ 9 files changed, 451 insertions(+) create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java create mode 100644 group11/252308879/dataStructure/src/main/resources/struts.xml diff --git a/group11/252308879/dataStructure/pom.xml b/group11/252308879/dataStructure/pom.xml index 4b756e4f0d..0bf21adba4 100644 --- a/group11/252308879/dataStructure/pom.xml +++ b/group11/252308879/dataStructure/pom.xml @@ -21,5 +21,15 @@ 4.12 test + + junit + junit + RELEASE + + + dom4j + dom4j + 1.6.1 + diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java new file mode 100644 index 0000000000..f38cbcb084 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java @@ -0,0 +1,39 @@ +package org.pan.coding2017.parsingXML; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java new file mode 100644 index 0000000000..50c5dea752 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java @@ -0,0 +1,34 @@ +package org.pan.coding2017.parsingXML; + +import java.util.Map; + + + +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字段中。 + + */ + + return null; + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java new file mode 100644 index 0000000000..ccdacc514c --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java @@ -0,0 +1,43 @@ +package org.pan.coding2017.parsingXML; + +import java.util.HashMap; +import java.util.Map; + +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 = 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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java new file mode 100644 index 0000000000..3271ab4ed1 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java @@ -0,0 +1,23 @@ +package org.pan.coding2017.parsingXML; + +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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java new file mode 100644 index 0000000000..6f5287ebaf --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java @@ -0,0 +1,50 @@ +package org.pan.coding2017.utils; + + + +import java.io.FileWriter; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; + +public class Dom4JUtil { + + public static final String PATH = "src/p1.xml"; + + + public static Document getDocuent(String xmlPath) { + + try { + //创建解析器 + SAXReader saxReader = new SAXReader(); + //得到Documment + Document document = saxReader.read(xmlPath); + return document; + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + public static void xmlWrite(Document document,String xmlPath){ + + try { + OutputFormat format = OutputFormat.createPrettyPrint(); + XMLWriter xmlWriter = new XMLWriter(new FileWriter(xmlPath),format); + xmlWriter.write(document); + xmlWriter.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java new file mode 100644 index 0000000000..8db355bbba --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java @@ -0,0 +1,152 @@ +package org.pan.coding2017.utils; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class JaxpDomUtil { + public static final String XMLPATH = "src/person.xml"; + + /** + * 通过 解析器 获取到 Document + * @return + */ + public static Document getDocument() { + try { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory + .newDocumentBuilder(); + Document document = documentBuilder.parse(XMLPATH); + return document; + } catch (Exception e) { + e.printStackTrace(); + + } + return null; + } + + /** + * 回写 XML 方法 + * @param document + */ + public static void tranFormMethod(Document document) { + try { + TransformerFactory transformerFactory = TransformerFactory + .newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.transform(new DOMSource(document), new StreamResult( + XMLPATH)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 递归调用 获取所有的元素 并打印元素名称 + * @param node + */ + private static void listElement(Node node) { + // 判断是元素类型时才打印 + if (node.getNodeType() == Node.ELEMENT_NODE) { + System.out.println(node.getNodeName()); + } + + // 获得一层子节点 + NodeList nodelist = node.getChildNodes(); + for (int i = 0; i < nodelist.getLength(); i++) { + // 得到每一个子节点 + Node nodeChild = nodelist.item(i); + + // 递归调用 + listElement(nodeChild); + } + + } + + /** + * 获取所有的 元素名称 + */ + public static void getListElement() { + + Document document = JaxpDomUtil.getDocument(); + listElement(document); + } + + /** + * 删除nan节点 + */ + public static void delSex() { + + Document document = JaxpDomUtil.getDocument(); + + // 获取元素 + Node nodeSex = document.getElementsByTagName("sex").item(0); + + // 得到父节点 + Node parent = nodeSex.getParentNode(); + + // 通过父节点删除 + parent.removeChild(nodeSex); + + // 回写XML + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 修改 sex 标签的 内容为nv + */ + public static void modifySex() { + Document document = JaxpDomUtil.getDocument(); + Node nodeSex = document.getElementsByTagName("sex").item(0); + nodeSex.setTextContent("nv"); + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 为第一个p1 增加 nv + */ + public static void addSex(){ + Document document = JaxpDomUtil.getDocument(); + Node p1Node = document.getElementsByTagName("p1").item(0); + + //通过 Document 创建 Element + Element sexElement = document.createElement("sex"); + sexElement.setTextContent("nv"); + p1Node.appendChild(sexElement); + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 查询xml中第一个name元素的值 + */ + public static void selectSin(){ + Document document = JaxpDomUtil.getDocument(); + Node nameNode = document.getElementsByTagName("name").item(0); + String name = nameNode.getTextContent(); + System.out.println(name); + } + + /** + * 查询所有name元素的值 + */ + public static void selectAll(){ + Document document = JaxpDomUtil.getDocument(); + NodeList nodeList = document.getElementsByTagName("name"); + for (int i = 0; i < nodeList.getLength(); i++) { + System.out.println(nodeList.item(i).getTextContent()); + } + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java new file mode 100644 index 0000000000..8756c8ab95 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java @@ -0,0 +1,89 @@ +package org.pan.coding2017.utils; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +public class JaxpSAXPUtil { + public static void main(String[] args) { + /* + * 1、创建解析器工厂 + * 2、创建解析器 + * 3、执行 parse 方法 + * + * 4、自己创建一个类、继承DefaultHandler + * 5、重写类里面的三个方法 + */ + + try { + SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); + SAXParser saxParser = saxParserFactory.newSAXParser(); + saxParser.parse("src/p1.xml",new MyDeafultHandler2() ); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} + +class MyDeafultHandler1 extends DefaultHandler{ + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + System.out.println("<"+qName+">"); + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + System.out.println(new String(ch,start,length)); + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + System.out.println("<"+qName+"/>"); + } + +} + +//实现获取所有的name元素的值 +class MyDeafultHandler2 extends DefaultHandler{ + + boolean flag = false; + int index = 1; + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + //判断qName 是否为 name 元素 + if("name".equals(qName) && index == 2){ + flag = true; + } + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + if(flag == true){ + System.out.println(new String(ch, start, length)); + } + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + if("name".equals(qName)){ + flag = false; + index++ ; + } + } + + +} diff --git a/group11/252308879/dataStructure/src/main/resources/struts.xml b/group11/252308879/dataStructure/src/main/resources/struts.xml new file mode 100644 index 0000000000..35830922ba --- /dev/null +++ b/group11/252308879/dataStructure/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From 3541d1fb3617c1b3388ca31f0914b15c2fe22206 Mon Sep 17 00:00:00 2001 From: SarahhLee <1059156023@qq.com> Date: Sun, 5 Mar 2017 20:40:51 +0800 Subject: [PATCH 5/6] struts and array --- group11/1059156023/Array/.classpath | 6 + group11/1059156023/Array/.gitignore | 1 + group11/1059156023/Array/.project | 17 ++ .../Array/src/com/coding/basic/ArrayUtil.java | 158 ++++++++++++++++++ group11/1059156023/struts/.classpath | 8 + group11/1059156023/struts/.gitignore | 1 + group11/1059156023/struts/.project | 17 ++ .../src/com/coding/basic/LoginAction.java | 39 +++++ .../struts/src/com/coding/basic/Struts.java | 59 +++++++ .../src/com/coding/basic/StrutsTest.java | 51 ++++++ .../struts/src/com/coding/basic/View.java | 23 +++ .../struts/src/com/coding/basic/struts.xml | 11 ++ 12 files changed, 391 insertions(+) create mode 100644 group11/1059156023/Array/.classpath create mode 100644 group11/1059156023/Array/.gitignore create mode 100644 group11/1059156023/Array/.project create mode 100644 group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java create mode 100644 group11/1059156023/struts/.classpath create mode 100644 group11/1059156023/struts/.gitignore create mode 100644 group11/1059156023/struts/.project create mode 100644 group11/1059156023/struts/src/com/coding/basic/LoginAction.java create mode 100644 group11/1059156023/struts/src/com/coding/basic/Struts.java create mode 100644 group11/1059156023/struts/src/com/coding/basic/StrutsTest.java create mode 100644 group11/1059156023/struts/src/com/coding/basic/View.java create mode 100644 group11/1059156023/struts/src/com/coding/basic/struts.xml diff --git a/group11/1059156023/Array/.classpath b/group11/1059156023/Array/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group11/1059156023/Array/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group11/1059156023/Array/.gitignore b/group11/1059156023/Array/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/Array/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/Array/.project b/group11/1059156023/Array/.project new file mode 100644 index 0000000000..1b5c14fe3f --- /dev/null +++ b/group11/1059156023/Array/.project @@ -0,0 +1,17 @@ + + + Array + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java b/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java new file mode 100644 index 0000000000..2b47957623 --- /dev/null +++ b/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java @@ -0,0 +1,158 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.HashSet; + +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){ + for (int i = 0; i < origin.length/2; i++) {//前后交换元素 + int temp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1]= temp; + } + System.out.println(Arrays.toString(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){ + int[] arr = new int[oldArray.length]; + int count = 0; + for(int i=0;i set1 = new HashSet(Arrays.asList(array1));//以array1建立集合 + HashSet set2 = new HashSet(Arrays.asList(array2)); + set1.addAll(set2);//求并集 + Integer[] arr = set1.toArray(new Integer[set1.size()]);//获取并集后的数组 + Arrays.sort(arr);//数组排序 + for(int i=0;i + + + + + + + diff --git a/group11/1059156023/struts/.gitignore b/group11/1059156023/struts/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/struts/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/struts/.project b/group11/1059156023/struts/.project new file mode 100644 index 0000000000..2c00c2049a --- /dev/null +++ b/group11/1059156023/struts/.project @@ -0,0 +1,17 @@ + + + struts + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/struts/src/com/coding/basic/LoginAction.java b/group11/1059156023/struts/src/com/coding/basic/LoginAction.java new file mode 100644 index 0000000000..23683995f2 --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group11/1059156023/struts/src/com/coding/basic/Struts.java b/group11/1059156023/struts/src/com/coding/basic/Struts.java new file mode 100644 index 0000000000..a0d26a207b --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/Struts.java @@ -0,0 +1,59 @@ +package com.coding.basic; + +import java.io.File; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.experimental.theories.Theories; + +import com.sun.corba.se.impl.orbutil.graph.Node; +import com.sun.org.apache.bcel.internal.classfile.Attribute; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException { + //创建SAXReader对象 + SAXReader reader = new SAXReader(); + //读取文件 转换成Document + Document document = reader.read(new File("src/com/coding/basic/struts.xml")); + //获取根节点元素对象 + Element root = document.getRootElement(); + //遍历根节点 + listNodes(root); + + /* + + 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字段中。 + + */ + + return null; + } + + public static void listNodes(Element node) { + String name; + List list = node.attributes(); + + } + +} diff --git a/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java b/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java new file mode 100644 index 0000000000..5bac52805d --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class StrutsTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testLoginActionSuccess() throws DocumentException { + + 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() throws DocumentException { + 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/group11/1059156023/struts/src/com/coding/basic/View.java b/group11/1059156023/struts/src/com/coding/basic/View.java new file mode 100644 index 0000000000..9e479018c8 --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/View.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +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/group11/1059156023/struts/src/com/coding/basic/struts.xml b/group11/1059156023/struts/src/com/coding/basic/struts.xml new file mode 100644 index 0000000000..b3b576c15f --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From d62eec2a68cfc43ed85af2bd099002de8edbb734 Mon Sep 17 00:00:00 2001 From: Pan Date: Mon, 6 Mar 2017 07:46:35 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/pan/coding2017/parsingXML/Struts.java | 10 ++++++++-- .../main/java/org/pan/coding2017/utils/Dom4JUtil.java | 11 +---------- .../java/org/pan/coding2017/utils/JaxpDomUtil.java | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java index 50c5dea752..2c967f14a3 100644 --- a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java @@ -1,5 +1,9 @@ package org.pan.coding2017.parsingXML; +import org.pan.coding2017.utils.JaxpDomUtil; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + import java.util.Map; @@ -27,8 +31,10 @@ public static View runAction(String actionName, Map parameters) { 放到View对象的jsp字段中。 */ - - return null; + + Document document = JaxpDomUtil.getDocument(); + NodeList action = document.getElementsByTagName("action"); + return null; } } diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java index 6f5287ebaf..f1679f6bef 100644 --- a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java @@ -1,11 +1,6 @@ package org.pan.coding2017.utils; - - import java.io.FileWriter; -import java.io.IOException; -import java.io.UnsupportedEncodingException; - import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; @@ -13,11 +8,8 @@ import org.dom4j.io.XMLWriter; public class Dom4JUtil { - - public static final String PATH = "src/p1.xml"; - - public static Document getDocuent(String xmlPath) { + public static Document getDocument(String xmlPath) { try { //创建解析器 @@ -26,7 +18,6 @@ public static Document getDocuent(String xmlPath) { Document document = saxReader.read(xmlPath); return document; } catch (DocumentException e) { - // TODO Auto-generated catch block e.printStackTrace(); } diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java index 8db355bbba..d8fe537e35 100644 --- a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java @@ -13,7 +13,7 @@ import org.w3c.dom.NodeList; public class JaxpDomUtil { - public static final String XMLPATH = "src/person.xml"; + public static final String XMLPATH = JaxpDomUtil.class.getClassLoader().getResource("struts.xml").getPath(); /** * 通过 解析器 获取到 Document