diff --git a/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java b/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java new file mode 100644 index 0000000000..d4e62075d9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java @@ -0,0 +1,218 @@ +package com.github.fei9009.coderising0226.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import com.sun.org.apache.bcel.internal.generic.ISTORE; + +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 i = 0, j = origin.length-1; + while(i < j){ + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + i++; + j--; + } + } + + /** + * 现在有如下的一个数组: 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){ + if (oldArray == null || oldArray.length == 0){ + return oldArray; + } + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + count++; + } + } + int[] newArray = new int[count]; + int j = 0; + for (int i = 0; i <= oldArray.length - 1; i++) { + if (oldArray[i] == 0) { + continue; + } + newArray[j++] = oldArray[i]; + } + 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 int[] merge(int[] array1, int[] array2){ + if (array1 == null && array2 == null) { + return null; + } + if (array1 == null) { + return array2; + } + if (array2 == null) { + return array1; + } + int i=0, j=0, k=0; + int len1 = array1.length; + int len2 = array2.length; + int[] mergeArr = new int[len1+len2]; + while(true){ + if(i == len1 || j == len2) + break; + if(array1[i]array2[j]){ + mergeArr[k++] = array2[j++]; + }else{ + mergeArr[k++] = array1[i++]; + j++; + } + } + + for(;i list = new ArrayList<>(); + int f1 = 1, f2 = 1, f3; + list.add(f1); + list.add(f2); + while (f1 +f2 < max) { + f3 = f1 + f2; + list.add(f3); + f1 = f2; + f2 = f3; + } + int[] result = new int[list.size()]; + int j = 0; + for(Integer i : list) { + result[j++] = i; + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] a = new int[max]; + int k=0; + for (int z = 1; ztype){ + try { + Method met = obj.getClass().getMethod("set" + initStr(att), type); + met.invoke(obj, value); + }catch (Exception e){ + e.printStackTrace(); + } + } + + private static String getter(Object obj, String att){ + try { + Method met = obj.getClass().getMethod("get" + initStr(att)); + return (String)met.invoke(obj); + }catch (Exception e){ + e.printStackTrace(); + } + return null; + } + private static String initStr(String name) { + name = name.substring(0, 1).toUpperCase() + name.substring(1); + return name; + } + + private static String toLowerString(String name) { + name = name.substring(0, 1).toLowerCase() + name.substring(1); + return name; + } + + + 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字段中。 + + */ + SAXReader saxReader = new SAXReader(); + try { + Document document = saxReader.read(new File("struts.xml")); + Element root = document.getRootElement(); + + + boolean flag = false; + String className = ""; + Element action = null; + for (Iterator iter = root.elementIterator(); iter.hasNext();) + { + Element e = (Element) iter.next(); + if(e.attributeValue("name").equals(actionName)){ + flag = true; + className = e.attributeValue("class"); + action = e; + break; + } + } + if(!flag) + throw new Exception(actionName+"未定义"); + + Class clz = Class.forName(className); + Constructor c = clz.getConstructor(); + Object obj = c.newInstance(); + + for (String in : parameters.keySet()) { + setter(obj,in,parameters.get(in), String.class); + } + Method exc = clz.getDeclaredMethod("execute"); + String res = (String)exc.invoke(obj); + + //获取所有getter方法 + //Get the methods + Method[] methods = clz.getDeclaredMethods(); + //Loop through the methods and print out their names + Map params = new HashMap(); + + for (Method method : methods) { + String name = method.getName(); + if(name.substring(0,3).equals("get")){ + params.put(toLowerString(name.substring(3)),getter(obj,toLowerString(name.substring(3)))); + } + } + View view = new View(); + view.setParameters(params); + //step 4 + flag = false; + Element result = null; + List actionChildList = action.elements("result"); + for (Iterator iter = action.elementIterator(); iter.hasNext();){ + Element e = (Element) iter.next(); + if(e.attributeValue("name").equals(res)){ + flag = true; + result = e; + break; + } + } + if(!flag) + throw new Exception(res+"undefined"); + view.setJsp(result.getText()); + return view; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java new file mode 100644 index 0000000000..c5ee5f10a9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.fei9009.coderising0226.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/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java new file mode 100644 index 0000000000..9332493c42 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.fei9009.coderising0226.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/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/struts.xml b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/struts.xml new file mode 100644 index 0000000000..68098b57e3 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/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