diff --git a/group22/2622819383/Task2/ArrayUtil.java b/group22/2622819383/Task2/ArrayUtil.java new file mode 100644 index 0000000000..ff947431ce --- /dev/null +++ b/group22/2622819383/Task2/ArrayUtil.java @@ -0,0 +1,272 @@ +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 lo = 0; + int hi = origin.length - 1; + while (lo < hi) + swap(origin, lo++, hi--); + } + private void swap(int[] array, int lo, int hi) { + int temp = array[lo]; + array[lo] = array[hi]; + array[hi] = temp; + } + + /** + * 现在有如下的一个数组: 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[] ret = new int[oldArray.length]; + int i = 0; + for (int j = 0; j < oldArray.length; j++) { + if (oldArray[j] != 0) + ret[i++] = oldArray[j]; + } + int[] old = ret; + ret = new int[i]; + for (int j = 0; j < i; j++) + ret[j] = old[j]; + + return ret; + } + + /** + * 给定两个已经排序好的整形数组, 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){ + int m = array1.length; //array1 —— m —— i + int n = array2.length; //array2 —— n —— j + int[] ret = new int[m + n]; // ret —— m+n —— k + int k = 0; + for (int i = 0, j = 0; i < m || j < n; ) { + if (i < m && (n <= j || array1[i] < array2[j])) ret[k++] = array1[i++]; + if (j < n && (m <= i || array2[j] < array1[i])) ret[k++] = array2[j++]; + if (i < m && j < n && array1[i] == array2[j]) { + ret[k++] = array1[i++]; + j++; + } + } + int[] old = ret; + ret = new int[k]; + for (int i = 0; i < k; i++) + ret[i] = old[i]; + + return ret; + } + /** + * 把一个已经存满数据的数组 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){ + int[] ret = new int[oldArray.length + size]; + + for (int i = 0; i < oldArray.length; i++) + ret[i] = oldArray[i]; + + return ret; + } + + /** + * 斐波那契数列为: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){ + int[] ret = new int[max / 2 + 10]; + int f = 1, g = 0, i = 0; + for ( ; f < max; i++) { + ret[i] = f; + f = g + f; + g = f - g; + } + int[] old = ret; + ret = new int[i]; + for (int j = 0; j < i; j++) + ret[j] = old[j]; + return ret; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] ret = new int[max / 3 + 10]; //节省初始化开辟的空间;ret —— i + int i = 0; //i用来向ret中添加素数 + //退化情况: max < 5的情况 + if (2 < max) { ret[i++] = 2; } + if (3 < max) { ret[i++] = 3; } + if (5 < max) { ret[i++] = 5; } + if (7 < max) { + //素数只能为6k+1、6k+5的类型 + //k的最小值:1 + //判断k的最大值:6k + 1 <= max其中6k + 5与max的比较需要自己确定 + int k = 1; + while (6 * k + 1 <= max) { + int m = 6 * k + 1; + int n = 6 * k + 5; + if(isPrime(ret, m)) ret[i++] = m; + if (max < n) break; + if (isPrime(ret, n)) ret[i++] = n; + k++; + } + }//O(n/3) * O((n^0.5) / 3) + int[] old = ret; + ret = new int[i]; + for (int j = 0; j < i; j++) + ret[j] = old[j]; + return ret; + } + + private boolean isPrime(int[] primeArray, int target) { + //O((n^0.5) / 3) + boolean isPrime = true; + int min = (int)Math.sqrt(target); + for (int i = 0; primeArray[i] <= min; i++) { + if (target % primeArray[i] == 0) { + isPrime = false; + break; + } + } + + return isPrime; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] ret = new int[48]; + int[] supportArr = getPrimes(max); + int j = 0; + for (int i = 2; i < max; i++) { + if (i % 2 != 0) continue; + if (isPerfectNumber(i, supportArr)) ret[j++] = i; + } + int[] old = ret; + ret = new int[j]; + for (int i = 0; i < j; i++) + ret[i] = old[i]; + return ret; + } + private boolean isPerfectNumber(int target, int[] supportArr) { + //套用公式:perfectNum = ( 2^p-1 ) * 2^(p-1) = ( 2^(count+1)-1 ) * ( 2^count ) + //其中p=count+1是质数、2^p-1=2^(count+1)-1也是质数 + //count: 完数中因子2的个数 + boolean isPerfectNum = true; + int count = amountOfTwo(target); + + int test0 = (int)Math.pow(2, count); + int test1 = count + 1; + int test2 = test0 * 2 - 1; + + if (count == 0) isPerfectNum = false; + else if (!isPrime(supportArr, test1)) isPerfectNum = false; + else if (!isPrime(supportArr, test2)) isPerfectNum = false; + else if (test0 * test2 != target) isPerfectNum = false; + + return isPerfectNum; + } + private int amountOfTwo(int num) { + int count = 0; + while (num % 2 == 0) { + num /= 2; + count++; + } + return count; + } + + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String ret = ""; + if (array.length < 1) return ret; + ret += array[0]; + for (int i = 1; i < array.length; i++) + ret += seperator + array[i]; + return ret; + } + + public static void main(String[] args) { + ArrayUtil au = new ArrayUtil(); + + int[] arr0 = au.fibonacci(50000000); + for (int i = 0; i < arr0.length; i++) + System.out.print(arr0[i] + " "); + // arr1 = {3,}; + //System.out.println(au.join(arr0, "-")); + //System.out.println(au.join(arr1, "-")); + + } + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group22/2622819383/Task2/litestruts/LoginAction.java b/group22/2622819383/Task2/litestruts/LoginAction.java new file mode 100644 index 0000000000..c3318361ae --- /dev/null +++ b/group22/2622819383/Task2/litestruts/LoginAction.java @@ -0,0 +1,41 @@ + + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + 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)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } +} diff --git a/group22/2622819383/Task2/litestruts/Struts.java b/group22/2622819383/Task2/litestruts/Struts.java new file mode 100644 index 0000000000..3109d1f40b --- /dev/null +++ b/group22/2622819383/Task2/litestruts/Struts.java @@ -0,0 +1,167 @@ +//本篇代码参考自学员2415980327 + + +import java.io.InputStream; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static Element parseXml(String fileName) { + InputStream input = Struts.class.getResourceAsStream(fileName); + SAXReader reader = new SAXReader(); + Document document = null; + + try { + document = reader.read(input); + Element struts = document.getRootElement(); + return struts; + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + + 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字段中。 + + */ + Element struts = parseXml("struts.xml"); + List actions = struts.elements(); + List resultRefs = new ArrayList<>(); + String actionClass = ""; + for (Element element : actions) + if (actionName.equals(element.attributeValue("name"))) { + actionClass = element.attributeValue("class"); + resultRefs = element.elements(); + break; + } + + Set attributes = parameters.keySet(); + Iterator it = attributes.iterator(); + try { + Object action = Class.forName(actionClass).newInstance(); + while (it.hasNext()) { + String attribute = it.next(); + Method method = action.getClass().getDeclaredMethod("set" + + attribute.substring(0, 1).toUpperCase() + + attribute.substring(1), String.class); + method.invoke(action, parameters.get(attribute)); + } + + Method execute = action.getClass().getDeclaredMethod("execute"); + String result = (String)execute.invoke(execute); + + Map actionParam = new HashMap(); + Method[] methods = action.getClass().getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().startsWith("get")) { + String methodName = method.getName(); + String name = methodName.substring(3, 4).toUpperCase() + methodName.substring(4); + String value = (String)method.invoke(action); + actionParam.put(name, value); + } + } + + + View view = new View(); + view.setParameters(actionParam); + for (Element element : resultRefs) { + if (result.equals(element.attributeValue("name"))) { + view.setJsp((String)element.getData()); + break; + } + } + return view; + + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) { + String actionName = "login"; + Element struts = parseXml("struts.xml"); + List actions = struts.elements(); + for (Element element : actions) { + if (actionName.equals(element.attributeValue("name"))) { + System.out.println(element.attributeValue("class")); + + for(Element element1:(List)element.elements()){ + System.out.println(element1.getData()); + } + } + } + } +} + + + + + + + + + + + + + + + + + + + + + diff --git "a/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" "b/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" new file mode 100644 index 0000000000..e6997ab49a --- /dev/null +++ "b/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" @@ -0,0 +1,125 @@ +Struts2瀹炴垬 +----------------------------chapter1 Struts2锛氱幇浠eb妗嗘灦 +涓銆乄eb搴旂敤绋嬪簭锛氬揩閫熷涔 + HTTP锛氬熀浜庢枃鏈 + 鏃犵姸鎬 + Java Servlet锛氳В鍐冲簳灞傚鎴/鏈嶅姟鍣ㄩ棶棰樸 + 灏咹TTP鍏紑缁橨ava璇█锛氭帴鍙桯TTP璇锋眰瀵硅薄锛屾鏌ユ暟鎹紝鎵ц鍚庡彴閫昏緫锛屽悜瀹㈡埛杩斿洖鍝嶅簲銆 + 楂樼骇鍔熻兘锛氫細璇濇満鍒躲 + 妗嗘灦锛氬叧娉ㄥ簲鐢ㄧ▼搴忕骇鍒殑闂銆 + HTTP瀛楃涓插埌Java鏁版嵁绫诲瀷鐨勮浆鎹紱 + 楠岃瘉鏁版嵁锛氬瓧绗︿覆鍚堟硶锛涜浆鎹㈠悗鐨刯ava绫诲瀷鍚堟硶锛 + 璁块棶涓氬姟閫昏緫锛氱壒瀹氬伐浣滄祦锛 + 璁块棶鏁版嵁灞傦細鐗瑰畾宸ヤ綔娴侊紱 + 鍛堢幇琛ㄧず灞(HTML绛)锛 + 鎻愪緵鍥介檯鍖栧拰鏈湴鍖栨敮鎸併 + +浜屻乄eb搴旂敤绋嬪簭妗嗘灦 + + 妗嗘灦鑷姩鍖栦簡甯歌浠诲姟锛 + 妗嗘灦鎻愪緵浜嗘灦鏋勮В鍐虫柟妗堬紱 + +涓夈丼truts2妗嗘灦 + + 1.Struts2姒傝锛歁VC妯″紡 + Browser鈥斺旇姹傗斺>鎺у埗鍣(FilterDisPatcher)鈥斺旇皟鐢ㄥ姩浣溾斺>妯″瀷鈥斺旈夋嫨缁撴灉鈥斺>瑙嗗浘鈥斺斿憟鐜伴〉闈⑩斺>Browser + //璇锋眰URL鍒板姩浣滅殑鏄犲皠锛氱敱XML閰嶇疆鏂囦欢鎴杍ava娉ㄨВ瀹屾垚 + //鍔ㄤ綔锛氬皢涓氬姟閫昏緫璋冪敤灏佽鍒颁竴涓崟鐙殑宸ヤ綔鍗曞厓 + 鍔ㄤ綔鏄竴涓暟鎹紶杈撶殑鍦烘墍銆 + 2.Struts宸ヤ綔鍘熺悊 + 璋冪敤鍔ㄤ綔鈥斺旀嫤鎴櫒鈥斺>鍔ㄤ綔鈥斺旇皟鐢ㄧ粨鏋溾斺>缁撴灉鈥斺旀嫤鎴櫒鈥斺>鍙戝洖缁撴灉 + | | + OGNL OGNL + | | + __________________________________ + | ActionContext(ThreadLocal) | + |ValueStack銆佽姹傘佷細璇濄... | + 鎷︽埅鍣ㄥ畬鎴愪换鍔★細鏃ュ織銆佹暟鎹獙璇併佺被鍨嬭浆鎹€佹枃浠朵笂浼犵瓑銆 + ValueStack锛氫繚绠¤姹傚垱绔嬬浉鍏崇殑鎵鏈夋暟鎹殑涓涓瓨鍌ㄥ尯鍩熴 + OGNL锛氳闂瓨鍌ㄥ湪涓ぎ瀛樺偍搴揤alueStack涓暟鎹殑宸ュ叿锛堣〃杈惧紡璇█锛夈 + ValueStack涓殑鏁版嵁璺熺潃澶勭悊璇锋眰缁忚繃鎵鏈夐樁娈碉紝璐┛妗嗘灦鐨勬暣涓繃绋嬨傝繖鏄洜涓篤alueStack瀛樺湪鍦ㄤ竴涓嚎绋嬫湰鍦板璞★紙ThreadLocal锛変腑銆 + ActionContext: 鍖呭惈鎵鏈夌殑鏁版嵁锛岃繖浜涙暟鎹瀯鎴愪簡鍔ㄤ綔鎵ц鐨勭幆澧冦 + +-------------------------chapter2 鍒濊瘑Struts +涓銆佸0鏄庢ф灦鏋勨斺斿簲鐢ㄧ▼搴忛厤缃細鍦ㄨ緝楂樼骇鍒弿杩板簲鐢ㄧ▼搴忕殑鏋舵瀯缁勪欢 + + 澹版槑閭d簺瀵硅薄浣滀负搴旂敤绋嬪簭鐨勫姩浣(action)銆佺粨鏋(result)銆佷互鍙婃嫤鎴櫒(interceptor) + 澹版槑杩囩▼涓昏鍖呮嫭鍒跺畾鍝釜Java绫诲疄鐜板摢涓帴鍙c + 涓ょ閰嶇疆 + 澹版槑鏋舵瀯鐨勪袱绉嶆柟寮 + 鏅鸿兘榛樿鍊 + +浜屻佺畝鍗曠殑HelloWorld绀轰緥 + + 閮ㄧ讲绀轰緥搴旂敤绋嬪簭 + 鎺㈢储HelloWorld搴旂敤绋嬪簭 + +涓夈佷娇鐢ㄦ敞瑙g殑HelloWorld + +-------------------------chapter3 浣跨敤Struts2鍔ㄤ綔 +涓銆丼truts鍔ㄤ綔绠浠 +浜屻佹墦鍖呭姩浣 +涓夈佸疄鐜板姩浣 +鍥涖佸悜瀵硅薄浼犻佹暟鎹 +浜斻佹渚嬬爺绌讹細鏂囦欢涓婁紶 + +-------------------------chapter4 浣跨敤鎷︽埅鍣ㄨ拷鍔犲伐浣滄祦 +-------------------------chapter5 鏁版嵁杞Щ锛歄GNL鍜岀被鍨嬭浆鎹 +-------------------------chapter6 鏋勫缓瑙嗗浘鈥斺旀爣绛 +-------------------------chapter7 UI缁勪欢鏍囩 +-------------------------chapter8 缁撴灉 +-------------------------chapter9 缁ф壙Spring鍜孒ibernate/JPA +-------------------------chapter10 鎺㈢储楠岃瘉妗嗘灦 +-------------------------chapter11 鐞嗚В鍥介檯鍖 +-------------------------chapter12 浣跨敤鎻掍欢鎵╁睍Struts2 +-------------------------chapter13 鏈浣冲疄璺 +-------------------------chapter14 浠庣粡鍏窼truts杩佺Щ +-------------------------chapter15 楂樼骇涓婚 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group22/2622819383/Task2/litestruts/StrutsTest.java b/group22/2622819383/Task2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9b188e8d5a --- /dev/null +++ b/group22/2622819383/Task2/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ + + +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/group22/2622819383/Task2/litestruts/View.java b/group22/2622819383/Task2/litestruts/View.java new file mode 100644 index 0000000000..4f2ca4ec30 --- /dev/null +++ b/group22/2622819383/Task2/litestruts/View.java @@ -0,0 +1,23 @@ + + +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/2622819383/Task2/litestruts/struts.xml b/group22/2622819383/Task2/litestruts/struts.xml new file mode 100644 index 0000000000..e5842b0b17 --- /dev/null +++ b/group22/2622819383/Task2/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + +