diff --git a/group24/330657387/.classpath b/group24/330657387/.classpath index 3e0fb272a8..80c86612f0 100644 --- a/group24/330657387/.classpath +++ b/group24/330657387/.classpath @@ -3,5 +3,6 @@ + diff --git a/group24/330657387/src/main/week02/litestruts/CustomException.java b/group24/330657387/src/main/week02/litestruts/CustomException.java new file mode 100644 index 0000000000..e1de4648d7 --- /dev/null +++ b/group24/330657387/src/main/week02/litestruts/CustomException.java @@ -0,0 +1,21 @@ +package main.week02.litestruts; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class CustomException extends RuntimeException { + + public CustomException(IOException e) { + super(e.getMessage()); + } + + public CustomException(JDOMException e) { + super(e.getMessage()); + } + + public CustomException(String msg) { + super(msg); + } + +} diff --git a/group24/330657387/src/main/week02/litestruts/LoginAction.java b/group24/330657387/src/main/week02/litestruts/LoginAction.java new file mode 100644 index 0000000000..300c29e4d2 --- /dev/null +++ b/group24/330657387/src/main/week02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package main.week02.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/group24/330657387/src/main/week02/litestruts/Struts.java b/group24/330657387/src/main/week02/litestruts/Struts.java new file mode 100644 index 0000000000..07ecadddec --- /dev/null +++ b/group24/330657387/src/main/week02/litestruts/Struts.java @@ -0,0 +1,79 @@ +package main.week02.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, + Map parameters) throws Exception, + NoSuchFieldException { + + /* + * + * 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字段中。 + */ + + XmlSaxUtil config = new XmlSaxUtil("struts.xml"); + String classname = config.getClassName(actionName); + // 1 通过反射存入parameter + // 没有这个类名就报错 + if (null == classname) { + throw new CustomException("没有这个action啊!"); + } + // 通过反射获取实例 + Class controllerClass = Class.forName(classname); + // 创建实例 + Object controller = controllerClass.newInstance(); + // 获取类中声明的全部成员变量 + Field[] fields = controllerClass.getDeclaredFields(); + // 备用 + Method m; + // 得到该action所有result的结果合集 + + // 为controller注入变量 + for (String key : parameters.keySet()) { + for (Field f : fields) { + if (f.getName() == key) { + m = controllerClass.getMethod("set" + + key.replace(key.substring(0, 1),key.substring(0, 1).toUpperCase()), String.class); + m.invoke(controller, parameters.get(key)); + break; + } + } + } + + // 2 通过反射调用excute 获取返回值 + m = controllerClass.getMethod("execute"); + String result = (String) m.invoke(controller); + + // 3 把message放到View对象的parameters + View view = new View(); + Map viewParam = new HashMap(); + + // 新建并传入View的viewParam属性值 + m = controllerClass.getMethod("getMessage"); + viewParam.put("message", (String) m.invoke(controller)); + view.setParameters(viewParam); + + // 传入jsp路径 + view.setJsp(config.getResultView(actionName, result)); + + return view; + } + +} diff --git a/group24/330657387/src/main/week02/litestruts/StrutsTest.java b/group24/330657387/src/main/week02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..1cc90265f7 --- /dev/null +++ b/group24/330657387/src/main/week02/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package main.week02.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + 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 Exception { + 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/group24/330657387/src/main/week02/litestruts/View.java b/group24/330657387/src/main/week02/litestruts/View.java new file mode 100644 index 0000000000..c333d79d25 --- /dev/null +++ b/group24/330657387/src/main/week02/litestruts/View.java @@ -0,0 +1,23 @@ +package main.week02.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/group24/330657387/src/main/week02/litestruts/XmlDomUtil.java b/group24/330657387/src/main/week02/litestruts/XmlDomUtil.java new file mode 100644 index 0000000000..d02319bc18 --- /dev/null +++ b/group24/330657387/src/main/week02/litestruts/XmlDomUtil.java @@ -0,0 +1,24 @@ +package main.week02.litestruts; + +public class XmlDomUtil { + /*// 0 读取xml文档的元素值 + // 获取工厂 + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + // 产生解析器 + DocumentBuilder builder = factory.newDocumentBuilder(); + // 解析xml文档,得到代表文档的document对象 + Document document = builder.parse(new File( + "./src/main/week02/litestruts/struts.xml")); + // 根据标签名获取节点列表 + NodeList actionList = document.getElementsByTagName("action");*/ + + /* HashMap actionMap = new HashMap();*/ + + /*for (int i = 0; i < actionList.getLength(); i++) { + // 获取节点列表里的节点 + Element action = (Element) actionList.item(i); + // 获取节点的属性值 + actionMap.put(action.getAttribute("name"), + action.getAttribute("class")); + }*/ +} diff --git a/group24/330657387/src/main/week02/litestruts/XmlSaxUtil.java b/group24/330657387/src/main/week02/litestruts/XmlSaxUtil.java new file mode 100644 index 0000000000..78f117a5f1 --- /dev/null +++ b/group24/330657387/src/main/week02/litestruts/XmlSaxUtil.java @@ -0,0 +1,115 @@ +package main.week02.litestruts; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + +public class XmlSaxUtil { + + Map actions = new HashMap<>(); + + public XmlSaxUtil(String fileName) { + // 得到包名 + String packageName = this.getClass().getPackage().getName(); + // 把包名转化成路径的一部分 + packageName = packageName.replace('.', '/'); + // 基于流的sax方式解析,需要先给出流,从类的同地址下找文件。 + InputStream is = this.getClass().getResourceAsStream( + "/" + packageName + "/" + fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + throw new CustomException(e); + } + } + + private void parseXML(InputStream is) { + // 解析器工厂 + SAXBuilder builder = new SAXBuilder(); + + try { + // 获取xml文件对象 + Document doc = builder.build(is); + // 根节点 + Element root = doc.getRootElement(); + + for (Element actionElement : root.getChildren("action")) { + + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + + for (Element resultElement : actionElement + .getChildren("result")) { + + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + + } + + this.actions.put(actionName, ac); + } + + } catch (JDOMException e) { + throw new CustomException(e); + + } catch (IOException e) { + throw new CustomException(e); + + } + + } + + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if (ac == null) { + return null; + } + return ac.getClassName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if (ac == null) { + return null; + } + return ac.getViewName(resultName); + } + + private static class ActionConfig { + + String name; + String clzName; + Map viewResult = new HashMap<>(); + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + + public String getClassName() { + return clzName; + } + + public void addViewResult(String name, String viewName) { + viewResult.put(name, viewName); + } + + public String getViewName(String resultName) { + return viewResult.get(resultName); + } + } + +} diff --git a/group24/330657387/src/main/week02/litestruts/struts.xml b/group24/330657387/src/main/week02/litestruts/struts.xml new file mode 100644 index 0000000000..2bbb2f3620 --- /dev/null +++ b/group24/330657387/src/main/week02/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/330657387/src/main/week02/practice/ArrayUtil.java b/group24/330657387/src/main/week02/practice/ArrayUtil.java index 3ab82a31e8..d174fdbe27 100644 --- a/group24/330657387/src/main/week02/practice/ArrayUtil.java +++ b/group24/330657387/src/main/week02/practice/ArrayUtil.java @@ -1,5 +1,10 @@ package main.week02.practice; +import java.util.ArrayList; +import java.util.Iterator; + +import com.sun.xml.internal.ws.org.objectweb.asm.Label; + public class ArrayUtil { /** @@ -40,7 +45,7 @@ public int[] removeZero(int[] oldArray) { } } int[] newArray = new int[i]; - System.arraycopy(oldArray, 0, newArray, 0,newArray.length); + System.arraycopy(oldArray, 0, newArray, 0, newArray.length); return newArray; } @@ -54,7 +59,56 @@ public int[] removeZero(int[] oldArray) { */ public int[] merge(int[] array1, int[] array2) { - return null; + if (array1.length == 0) { + return array2; + } + if (array2.length == 0) { + return array1; + } + int n = 0; + int[] merge = new int[array1.length + array2.length]; + int i = 0, j = 0; + while (i < array1.length || j < array2.length) { + if (array1[i] == array2[j]) { + merge[n] = array1[i]; + n++; + i++; + j++; + if (i == array1.length) { + System.arraycopy(array2, j, merge, n, array2.length - j); + n += array2.length - j; + break; + } + if (j == array2.length) { + System.arraycopy(array1, i, merge, n, array1.length - i); + n += array1.length - i; + break; + } + continue; + } + if (array1[i] < array2[j]) { + merge[n] = array1[i]; + n++; + i++; + if (i == array1.length) { + System.arraycopy(array2, j, merge, n, array2.length - j); + n += array2.length - j; + break; + } + } else { + merge[n] = array2[j]; + n++; + j++; + if (j == array2.length) { + System.arraycopy(array1, i, merge, n, array1.length - i); + n += array1.length - i; + break; + } + } + } + int[] res = new int[n]; + System.arraycopy(merge, 0, res, 0, n); + return res; } /** @@ -67,7 +121,9 @@ public int[] merge(int[] array1, int[] array2) { * @return */ public int[] grow(int[] oldArray, int size) { - return null; + int[] res = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, res, 0, oldArray.length); + return res; } /** @@ -78,7 +134,24 @@ public int[] grow(int[] oldArray, int size) { * @return */ public int[] fibonacci(int max) { - return null; + if (max <= 1) { + return new int[0]; + } + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + int index = 1; + while (list.get(index) + list.get(index - 1) < max) { + list.add(list.get(index) + list.get(index - 1)); + index++; + } + Iterator iter = list.iterator(); + int[] res = new int[list.size()]; + int i = 0; + while (iter.hasNext()) { + res[i++] = iter.next(); + } + return res; } /** @@ -88,7 +161,22 @@ public int[] fibonacci(int max) { * @return */ public int[] getPrimes(int max) { - return null; + if (max <= 2) { + return new int[0]; + } + int n = 2; + int arr[] = new int[max]; + int j = 0; + a: for (; n < max; n++) { + for (int i = 2; i < n / 2 + 1; i++) { + if (n % i == 0) + continue a; + } + arr[j++] = n; + } + int[] res = new int[j]; + System.arraycopy(arr, 0, res, 0, j); + return res; } /** @@ -98,7 +186,23 @@ public int[] getPrimes(int max) { * @return */ public int[] getPerfectNumbers(int max) { - return null; + if (max <= 6) { + return new int[0]; + } + int n = 6, sum = 0, j = 0; + int[] arr = new int[max]; + for (; n < max; n++, sum = 0) { + for (int i = 1; i < n / 2 + 1; i++) { + if (n % i == 0) + sum += i; + + } + if (sum == n) + arr[j++] = n; + } + int[] res = new int[j]; + System.arraycopy(arr, 0, res, 0, j); + return res; } /** @@ -109,7 +213,17 @@ public int[] getPerfectNumbers(int max) { * @return */ public String join(int[] array, String seperator) { - return null; + if (array.length == 0) { + return ""; + } + StringBuilder sb = new StringBuilder(); + int i; + for (i = 0; i < array.length - 1; i++) { + sb.append(array[i]); + sb.append(seperator); + } + sb.append(array[i]); + return sb.toString(); } } diff --git a/group24/330657387/src/test/week02/practice/ArrayUtilTest.java b/group24/330657387/src/test/week02/practice/ArrayUtilTest.java index 1bf97fced3..0e29853068 100644 --- a/group24/330657387/src/test/week02/practice/ArrayUtilTest.java +++ b/group24/330657387/src/test/week02/practice/ArrayUtilTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.*; import java.util.Arrays; +import java.util.HashMap; import main.week02.practice.ArrayUtil; @@ -48,32 +49,63 @@ public void testRemoveZero() { @Test public void testMerge() { - fail("Not yet implemented"); + int[][] array1 = {{0,1,2,5,6,9,11}, + {1,2,3}, + {}}; + int[][] array2 = {{0,3,8,15,16,20,50},{1},{}}; + for(int i=0;i<3;i++){ + System.out.println("前:"+Arrays.toString(array1[i])+Arrays.toString(array2[i])); + System.out.println("后:"+Arrays.toString(util.merge(array1[i], array2[i]))); + } } @Test public void testGrow() { - fail("Not yet implemented"); + int[][] origin = {{1,20,3,65,4,6,9,7}, + {}, + {1,0}, + {1,0,2,3}, + {23,0,0,32}}; + for(int[] a : origin){ + System.out.println("前:"+Arrays.toString(a)); + System.out.println("后:"+Arrays.toString(util.grow(a, 3))); + } } @Test public void testFibonacci() { - fail("Not yet implemented"); + int[] origin = {1,2,3,65,4,6,9,7}; + for(int a : origin){ + System.out.println(Arrays.toString(util.fibonacci(a))); + } } @Test public void testGetPrimes() { - fail("Not yet implemented"); + int[] origin = {1,2,3,65,4,6,9,7}; + for(int a : origin){ + System.out.println(Arrays.toString(util.getPrimes(a))); + } } @Test public void testGetPerfectNumbers() { - fail("Not yet implemented"); + int[] origin = {1,2,3,65,4,6,999,7}; + for(int a : origin){ + System.out.println(Arrays.toString(util.getPerfectNumbers(a))); + } } @Test public void testJoin() { - fail("Not yet implemented"); + int[][] origin = {{1,20,3,65,4,6,9,7}, + {}, + {1,0}, + {1,0,2,3}, + {23,0,0,32}}; + for(int[] a : origin){ + System.out.println(util.join(a , "=")); + } } } diff --git "a/group24/330657387/\345\215\232\345\256\242\345\234\260\345\235\200.txt" "b/group24/330657387/\345\215\232\345\256\242\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..76ae770288 --- /dev/null +++ "b/group24/330657387/\345\215\232\345\256\242\345\234\260\345\235\200.txt" @@ -0,0 +1,10 @@ +һܲhttp://www.cnblogs.com/sargeles/p/6605493.html +ڶܲhttp://www.cnblogs.com/sargeles/p/6605945.html +ܲ +ܲ +ܲ +ܲ +ܲ +ڰܲ +ھܲ +ʮܲ