From 7566142eaae72a635a8930dfcb0f569ea152d210 Mon Sep 17 00:00:00 2001 From: wenGQ Date: Sat, 18 Mar 2017 16:44:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bin/com/github/mrwengq/sec/struts.xml | 11 + .../src/com/github/mrwengq/sec/ArrayUtil.java | 252 ++++++++++++++++++ .../com/github/mrwengq/sec/ArrayUtilTest.java | 93 +++++++ .../com/github/mrwengq/sec/LoginAction.java | 39 +++ .../src/com/github/mrwengq/sec/Struts.java | 134 ++++++++++ .../com/github/mrwengq/sec/StrutsTest.java | 43 +++ .../src/com/github/mrwengq/sec/View.java | 23 ++ .../src/com/github/mrwengq/sec/struts.xml | 11 + 8 files changed, 606 insertions(+) create mode 100644 group22/1193590951/githubitem/bin/com/github/mrwengq/sec/struts.xml create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtil.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtilTest.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/LoginAction.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/Struts.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/StrutsTest.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/View.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/struts.xml diff --git a/group22/1193590951/githubitem/bin/com/github/mrwengq/sec/struts.xml b/group22/1193590951/githubitem/bin/com/github/mrwengq/sec/struts.xml new file mode 100644 index 0000000000..404aa778e1 --- /dev/null +++ b/group22/1193590951/githubitem/bin/com/github/mrwengq/sec/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtil.java b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtil.java new file mode 100644 index 0000000000..3b037d2cc9 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtil.java @@ -0,0 +1,252 @@ +package com.github.mrwengq.sec; + +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 len = origin.length; + int temp = 0; + for(int i = len ; i>0;i--){ //len - i 是从小到大 + if(len - i > (int) (len-1)/2){ + break; + } + temp = origin[len-i]; + origin[len-i] = origin[i-1]; //i-1是从大到小 + origin[i-1] = 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 removeNum =0; + for(int i = 0;iarray1.length-1){ + newArray[i] = array2[index2++]; + break; + } + if(index2>array2.length-1){ + newArray[i] = array1[index1++]; + break; + } + if(array1[index1]>array2[index2]){ + newArray[i] = array2[index2++]; + }else if(array1[index1]max){ + break; + } + if(len-1>temp.length){ + temp = copyAddArray(temp); + } + temp[len] = next; + len++; + + } + int[] fbn = new int[len]; + System.arraycopy(temp, 0, fbn, 0, len); + return fbn; + } + private int[] copyAddArray(int elementData[]) { //对数组扩容 增加量为原长度3/4 + int ob[] = new int[elementData.length+(elementData.length * 3) / 4]; + System.arraycopy(elementData ,0, ob, 0,elementData.length); + return ob; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max<3){ + return null; + } + int[] temp = new int[100]; + int index = 0; + int i = 2;//最小素数 + while(itemp.length-1){ //判断空间是否充足,否扩容 + temp = copyAddArray(temp); + } + temp[index++] = i; + i++; + } + int[] prime = new int[index]; + System.arraycopy(temp, 0, prime, 0, index); + return prime; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <2){ + return null; + } + + int[] temp = new int[100]; + int index = 0; + int i = 2; + while(itemp.length-1){ //判断空间是否充足,否扩容 + temp = copyAddArray(temp); + } + temp[index++] = i; + i++; + } + if(0==index){ + return null; + } + int[] perfectNum = new int[index]; + System.arraycopy(temp, 0, perfectNum, 0, index); + return perfectNum; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder stb = new StringBuilder(); + for(int i = 0; i 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ +public class Struts { + + public static View runAction(String actionName, Map parameters) { + View view = new View(); + //0.读取xml文件 + Document doc = createDOC("struts.xml"); + Class actionClass= queryClass(actionName, doc); + //1.实例化对象调用set方法 + Set paramNames = parameters.keySet(); //取出调用方法所用参数名称 + Iterator iter = paramNames.iterator(); + Object ob = null; + try { + ob = actionClass.newInstance(); + while(iter.hasNext()){ + String temp = iter.next(); + String methodName = "set"+temp.replaceFirst( temp.substring(0,1),temp.substring(0,1).toUpperCase());//方法名称 + String methodParam = parameters.get(temp); //方法参数 + Method me= actionClass.getMethod(methodName,String.class); + me.invoke(ob,methodParam); + } + //2.调用execute反方法取出返回值 + String actionMethod = (String)actionClass.getMethod("execute").invoke(ob); + //3.调用所有的get,并保存返回值 + Map map = new HashMap(); + Method[] methods = actionClass.getMethods(); + for(Method method : methods){ + String methodName = method.getName(); + if(methodName.substring(0,3).equals("get")){ + Object value = method.invoke(ob); + String key = methodName.substring(3,methodName.length()).toLowerCase(); + map.put(key, value); + } + } + view.setParameters(map);//将数据保存到view中 + //4.设置返回结果视图 + String jsp = queryResult(actionName,doc,actionMethod); + view.setJsp(jsp); + } catch (Exception e) { + e.printStackTrace(); + } + + return view; + } + private static String queryResult(String actionName,Document doc,String actionMethod){ + String reView = null; + Element el = queryActionElement(actionName,doc);//查找action元素 + List resultEl = el.elements("result"); + for(Element rel : resultEl){ + Attribute att = rel.attribute("name"); + if(att.getValue().equals(actionMethod)){ + reView = rel.getText(); + } + } + return reView; + } + //查找对应的action元素 + private static Element queryActionElement(String actionName, Document doc){ + Element root = doc.getRootElement(); + Element actionElement = null; + List list = root.elements("action"); + for(Element el : list){ + Attribute att = el.attribute("name"); + if(att.getValue().equals(actionName)){ + actionElement = el; + } + } + return actionElement; + + } + //查找action类 + private static Class queryClass(String actionName, Document doc) { + Element el = queryActionElement(actionName,doc);//查找action元素 + Attribute att = el.attribute("class"); + String className = att.getValue(); + + Class actionClass = null; + try { + actionClass = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return actionClass; + } + //获取document对象 + private static Document createDOC(String fileName){ + + //使用dom4j的读取xml文件 + SAXReader sr = new SAXReader(); + URL fileUrl = Struts.class.getResource(fileName); + Document doc = null; + try { + doc = sr.read(fileUrl); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return doc; + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/sec/StrutsTest.java b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/StrutsTest.java new file mode 100644 index 0000000000..2b848ce991 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.mrwengq.sec; + +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/1193590951/githubitem/src/com/github/mrwengq/sec/View.java b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/View.java new file mode 100644 index 0000000000..1c09f0ba33 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/View.java @@ -0,0 +1,23 @@ +package com.github.mrwengq.sec; + +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/1193590951/githubitem/src/com/github/mrwengq/sec/struts.xml b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/struts.xml new file mode 100644 index 0000000000..404aa778e1 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + +