From d1f9d2acfa44c9667b3dfd5c9394bc5cc48c69fe Mon Sep 17 00:00:00 2001 From: liyiliang <735371210@qq.com> Date: Sun, 19 Mar 2017 17:34:59 +0800 Subject: [PATCH] week2 --- .../src/task2/week2/array/ArrayUtil.java | 252 ++++++++++++++++++ .../src/task2/week2/struts/LoginAction.java | 39 +++ .../src/task2/week2/struts/Struts.java | 139 ++++++++++ .../src/task2/week2/struts/StrutsTest.java | 44 +++ .../src/task2/week2/struts/View.java | 23 ++ .../src/task2/week2/struts/struts.out.xml | 0 .../src/task2/week2/struts/struts.xml | 11 + 7 files changed, 508 insertions(+) create mode 100644 group22/735371210/src/task2/week2/array/ArrayUtil.java create mode 100644 group22/735371210/src/task2/week2/struts/LoginAction.java create mode 100644 group22/735371210/src/task2/week2/struts/Struts.java create mode 100644 group22/735371210/src/task2/week2/struts/StrutsTest.java create mode 100644 group22/735371210/src/task2/week2/struts/View.java create mode 100644 group22/735371210/src/task2/week2/struts/struts.out.xml create mode 100644 group22/735371210/src/task2/week2/struts/struts.xml diff --git a/group22/735371210/src/task2/week2/array/ArrayUtil.java b/group22/735371210/src/task2/week2/array/ArrayUtil.java new file mode 100644 index 0000000000..22492679fa --- /dev/null +++ b/group22/735371210/src/task2/week2/array/ArrayUtil.java @@ -0,0 +1,252 @@ +package task2.week2.array; + +public class ArrayUtil { + + public static void main(String[] args){ + ArrayUtil t=new ArrayUtil(); + int[] intArray={1,3,4}; + int[] array1={1,4,5,8}; + int[] array2={3,5,9}; + + String str=t.join(intArray,"-"); + + for(int i:t.merge(array1,array2)){ + + System.out.println(i); + } + + + } + /** + * 给定一个整形数组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 temp=0; + for(int i=0;i2){ + array[0]=1; + array[1]=1; + int i=2; + + while(array[i-1]=max){ + array[i-1]=0; + } + } + + + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + int[] newArray = new int[max]; + if (max > 2) { + + int size = 0, j = 0; + + for (int i = 2; i < max; i++) { + for (j = 2; j < i / 2 + 1; j++) { + + if (i % j == 0) { + + break; + + } + + } + + if (j == i / 2 + 1) { + newArray[size++] = i; + } + + } + + } + + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] prefectArray=new int[max]; + int k=0; + + for(int i=1;i parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + /* + + 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字段中。 + + */ + String jsp=""; + + Map viewParams=new HashMap(); + + Element ele=getNode(actionName); + + String className=ele.attributeValue("class"); + + Class actionClass=Class.forName(className); + + Object action=actionClass.newInstance(); + + for(Map.Entry entry :parameters.entrySet()){ + + + Method m=actionClass.getMethod("set"+entry.getKey().substring(0,1).toUpperCase()+entry.getKey().substring(1), String.class); + + m.invoke(action, entry.getValue()); + + + } + + Method execute =actionClass.getMethod("execute"); + String result=(String)execute.invoke(action); + + System.out.println(result); + + Method[] methods=actionClass.getDeclaredMethods(); + + for(Method m : methods){ + if(m.getName().startsWith("get")){ + String value =(String) m.invoke(action); + + String key=m.getName().substring(3); + key=key.substring(0,1).toLowerCase()+key.substring(1); + + viewParams.put(key,value); + + + } + } + + for(Element e:(List) ele.elements("result")){ + + if(e.attributeValue("name").equals(result)){ + + jsp=e.getText(); + } + + } + + View view=new View(); + view.setJsp(jsp); + + view.setParameters(viewParams); + + + return view; + } + + + public static Element getNode(String actionName){ + Element ele=null; + + SAXReader reader =new SAXReader(); + + InputStream in =Struts.class.getResourceAsStream("struts.xml"); + + try { + Document doc =reader.read(in); + Element root =doc.getRootElement(); + + List childNode= root.elements(); + + + for(Element e:childNode){ + + if(e.attributeValue("name").equals(actionName)){ + ele=e; + break; + } + + } + + + } catch (DocumentException ex) { + + ex.printStackTrace(); + } + + return ele; + + + + } + + + +} + diff --git a/group22/735371210/src/task2/week2/struts/StrutsTest.java b/group22/735371210/src/task2/week2/struts/StrutsTest.java new file mode 100644 index 0000000000..4c9ad36231 --- /dev/null +++ b/group22/735371210/src/task2/week2/struts/StrutsTest.java @@ -0,0 +1,44 @@ +package task2.week2.struts; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + 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, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + 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/735371210/src/task2/week2/struts/View.java b/group22/735371210/src/task2/week2/struts/View.java new file mode 100644 index 0000000000..9f5851bc8f --- /dev/null +++ b/group22/735371210/src/task2/week2/struts/View.java @@ -0,0 +1,23 @@ +package task2.week2.struts; + +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/735371210/src/task2/week2/struts/struts.out.xml b/group22/735371210/src/task2/week2/struts/struts.out.xml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group22/735371210/src/task2/week2/struts/struts.xml b/group22/735371210/src/task2/week2/struts/struts.xml new file mode 100644 index 0000000000..b47a193398 --- /dev/null +++ b/group22/735371210/src/task2/week2/struts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + +