From 6e7de6c17e06508b907e90896902785948e3dc2c Mon Sep 17 00:00:00 2001 From: xiaozi Date: Mon, 20 Mar 2017 12:52:50 +0800 Subject: [PATCH] This is my second week's work. --- group24/1054283210/.classpath | 1 + .../coding2017/secondWork/.gitignore | 27 +++ .../coding2017/secondWork/ArrayUtil.java | 201 ++++++++++++++++++ .../coding2017/secondWork/LoginAction.java | 41 ++++ .../coding2017/secondWork/Struts.java | 145 +++++++++++++ .../coding2017/secondWork/StrutsTest.java | 46 ++++ .../xiaozi123/coding2017/secondWork/View.java | 23 ++ .../coding2017/secondWork/struts.xml | 11 + ...7\347\253\240\345\234\260\345\235\200.txt" | 2 + 9 files changed, 497 insertions(+) create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/.gitignore create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/ArrayUtil.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/LoginAction.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/Struts.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/StrutsTest.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/View.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/struts.xml create mode 100644 "group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/\346\226\207\347\253\240\345\234\260\345\235\200.txt" diff --git a/group24/1054283210/.classpath b/group24/1054283210/.classpath index 2d7497573f..26651e8716 100644 --- a/group24/1054283210/.classpath +++ b/group24/1054283210/.classpath @@ -3,5 +3,6 @@ + diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/.gitignore b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/.gitignore new file mode 100644 index 0000000000..2c93a035dc --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/.gitignore @@ -0,0 +1,27 @@ +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ +*.iml +rebel.* +.rebel.* + +# Idea +*.iml +*.ipr +*.iws +.idea + +target diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/ArrayUtil.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/ArrayUtil.java new file mode 100644 index 0000000000..149f196397 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/ArrayUtil.java @@ -0,0 +1,201 @@ +package com.github.xiaozi123.coding2017.secondWork; + +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 = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int[] reverseArray=new int[origin.length]; + for (int i = 0; i < reverseArray.length/2; i++) { + reverseArray[i]=origin[origin.length-i-1]; + } + + } + + /** + * 现在有如下的一个数组: 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[] newArray=new int[oldArray.length]; + + for (int i = 0,j=0; i < oldArray.length; i++,j++) { + if (oldArray[i]==0) { + i++; + } + 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){ + ArrayList list1 = new ArrayList(Arrays.asList(array1)); + ArrayList list2 = new ArrayList(Arrays.asList(array2)); + list1.removeAll(list2); + Integer[] integers=(Integer[]) list1.toArray(); + int[] intArray = new int[integers.length]; + for(int i=0; i < integers.length; i ++) + { + intArray[i] = integers[i].intValue(); + } + return intArray; + } + /** + * 把一个已经存满数据的数组 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){ + if (oldArray==null||size>=0) { + throw new IndexOutOfBoundsException("出错."); + + } + int[] resultArray=new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, resultArray, 0,oldArray.length); + return resultArray; + } + + /** + * 斐波那契数列为: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[] array=new int[max]; + for (int i = 0; i < max; i++) { + array[i]=getFibo(i);//第i项 + if (array[i]>=max) { + break; + } + } + return array; + } + + // 获取第i项 + private static int getFibo(int i) { + if (i == 1 || i == 2) + return 1; + else + return getFibo(i - 1) + getFibo(i - 2);} + + + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] array=new int[max]; + for (int i = 0,j=0; i < max; i++) { + if (isPrime(i)) { + array[j]=i; + j++; + } + + } + return array; + } + public static boolean isPrime(int a) { + boolean flag = true; + + if (a < 2) {// 素数不小于2 + return false; + } else { + + for (int i = 2; i <= Math.sqrt(a); i++) { + + if (a % i == 0) {// 若能被整除,则说明不是素数,返回false + + flag = false; + break;// 跳出循环 + } + } + } + return flag; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] array=new int[max]; + for (int i = 0,j=0; i < max; i++) { + if (isPrime(i)) { + array[j]=i; + j++; + } + + } + return array; + } + + public static boolean isPerfectNumber(int i) { + int s=0; + for(int j=1;j0&&array!=null) { + StringBuffer stringBuffer=new StringBuffer(); + stringBuffer.append(array[0]); + for (int i = 1; i < array.length; i++) { + stringBuffer.append(seperator).append(array[i]); + } + return stringBuffer.toString(); + } + return null; + } + + +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/LoginAction.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/LoginAction.java new file mode 100644 index 0000000000..6c8e4026ce --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/LoginAction.java @@ -0,0 +1,41 @@ +package com.github.xiaozi123.coding2017.secondWork; + + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/1054283210/src/com/github/xiaozi123/coding2017/secondWork/Struts.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/Struts.java new file mode 100644 index 0000000000..2f267a2491 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/Struts.java @@ -0,0 +1,145 @@ +package com.github.xiaozi123.coding2017.secondWork; + +import java.util.Map; + +import java.io.File; +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 org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import com.sun.corba.se.impl.oa.poa.ActiveObjectMap.Key; + + + +public class Struts { + + + + public static View runAction(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + + /* + + 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字段中。 + + */ + //读取xml文件 + +// String path="src/com/github/xiaozi123.coding2017.secondWork/struct.xml"; + + + + return initView(actionName, parameters); + } + + public static View initView(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + View view=new View(); + int i=0; + String[] methodNames=new String[parameters.size()]; + + for (String string : parameters.keySet()) { + methodNames[i++]="set" + +string.substring(0, 1).toUpperCase()+string.substring(1); + } + + Struts.class.getResourceAsStream("/structs.xml"); + Element element=getTargetElement(actionName); + + //通过反射实例化 + String className=element.attribute(1).getValue(); + Class clazz=Class.forName(className); + + Object object=clazz.newInstance(); + + invokeObjectSetter(parameters, methodNames, clazz, object); + + view.setParameters(createGetterMap(clazz, object)); + setViewJsp(view, element, clazz, object); + return view; + + } + + + private static void setViewJsp(View view, Element element, Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + view.setJsp(getJsp(element, executeToGetResult(clz, obj))); + } + + + private static Map createGetterMap(Class clz, Object obj) throws IllegalAccessException, InvocationTargetException { + Map map = new HashMap(); + Method[] methods = clz.getMethods(); + for (Method item : methods) { + if (item.getName().contains("get")) { + String key = item.getName().substring(3).toLowerCase(); + Object value = item.invoke(obj); + map.put(key, value); + } + } + return map; + } + + private static String executeToGetResult(Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // 反射获取方法 并且执行 + Method method = clz.getMethod("execute"); + String result = (String) method.invoke(obj); + return result; + } + + private static void invokeObjectSetter(Map parameters, + String[] methodNames, Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for (String key : methodNames) { + Method method = clz.getMethod(key, String.class); + method.invoke(obj, parameters.get(key)); + } + } + + private static Element getTargetElement(String actionName) throws DocumentException { + SAXReader reader = new SAXReader(); + InputStream inputStream =Struts.class.getResourceAsStream("/struts.xml"); + Document document = reader.read(inputStream); + Element rootNode = document.getRootElement(); + List elements = rootNode.elements(); + for (Element item : elements) { + if (actionName.equals(item.attribute(0).getValue())) { + return item; + } + } + return null; + } + + private static String getJsp(Element element, String result) { + List elements = element.elements(); + for (Element e : elements) { + if (result.equals(e.attribute(0).getValue())) { + return e.getTextTrim(); + } + } + return null; + } + +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/StrutsTest.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/StrutsTest.java new file mode 100644 index 0000000000..7e61cb97d9 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/StrutsTest.java @@ -0,0 +1,46 @@ +package com.github.xiaozi123.coding2017.secondWork; + +import java.lang.reflect.InvocationTargetException; +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() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, 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 ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, 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/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/View.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/View.java new file mode 100644 index 0000000000..5d13e67df3 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/View.java @@ -0,0 +1,23 @@ +package com.github.xiaozi123.coding2017.secondWork; + +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/1054283210/src/com/github/xiaozi123/coding2017/secondWork/struts.xml b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/struts.xml new file mode 100644 index 0000000000..0582b7d4ea --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git "a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/\346\226\207\347\253\240\345\234\260\345\235\200.txt" "b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/\346\226\207\347\253\240\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..4aa14355dc --- /dev/null +++ "b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/\346\226\207\347\253\240\345\234\260\345\235\200.txt" @@ -0,0 +1,2 @@ +简书:http://www.jianshu.com/p/8a15d1c12bc0 +CSDN:http://blog.csdn.net/qq_23038639/article/details/63252328 \ No newline at end of file