From bf9669bc3b70cc5d6d0c551f853dd99af2281cce Mon Sep 17 00:00:00 2001 From: lenovo2512009 <610673813@qq.com> Date: Mon, 6 Mar 2017 22:04:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=20=E5=92=8C=E7=AC=AC=E4=BA=8C=E6=AC=A1=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group09/610673813/.classpath | 8 ++ group09/610673813/.gitignore | 1 + group09/610673813/.project | 17 +++ .../src/coding/week01/ArrayList.java | 71 +++++++++++ .../src/coding/week01/BinaryTreeNode.java | 32 +++++ .../610673813/src/coding/week01/Iterator.java | 7 ++ .../src/coding/week01/LinkedList.java | 104 ++++++++++++++++ group09/610673813/src/coding/week01/List.java | 9 ++ .../610673813/src/coding/week01/Queue.java | 35 ++++++ .../610673813/src/coding/week01/Stack.java | 39 ++++++ .../src/coding/week02/LoginAction.java | 34 ++++++ .../610673813/src/coding/week02/ReadXml.java | 70 +++++++++++ .../610673813/src/coding/week02/Struts.java | 115 ++++++++++++++++++ .../src/coding/week02/StrutsTest.java | 40 ++++++ group09/610673813/src/coding/week02/View.java | 28 +++++ 15 files changed, 610 insertions(+) create mode 100644 group09/610673813/.classpath create mode 100644 group09/610673813/.gitignore create mode 100644 group09/610673813/.project create mode 100644 group09/610673813/src/coding/week01/ArrayList.java create mode 100644 group09/610673813/src/coding/week01/BinaryTreeNode.java create mode 100644 group09/610673813/src/coding/week01/Iterator.java create mode 100644 group09/610673813/src/coding/week01/LinkedList.java create mode 100644 group09/610673813/src/coding/week01/List.java create mode 100644 group09/610673813/src/coding/week01/Queue.java create mode 100644 group09/610673813/src/coding/week01/Stack.java create mode 100644 group09/610673813/src/coding/week02/LoginAction.java create mode 100644 group09/610673813/src/coding/week02/ReadXml.java create mode 100644 group09/610673813/src/coding/week02/Struts.java create mode 100644 group09/610673813/src/coding/week02/StrutsTest.java create mode 100644 group09/610673813/src/coding/week02/View.java diff --git a/group09/610673813/.classpath b/group09/610673813/.classpath new file mode 100644 index 0000000000..3f48e879d0 --- /dev/null +++ b/group09/610673813/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group09/610673813/.gitignore b/group09/610673813/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group09/610673813/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group09/610673813/.project b/group09/610673813/.project new file mode 100644 index 0000000000..a6666f301e --- /dev/null +++ b/group09/610673813/.project @@ -0,0 +1,17 @@ + + + coding + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group09/610673813/src/coding/week01/ArrayList.java b/group09/610673813/src/coding/week01/ArrayList.java new file mode 100644 index 0000000000..f157c7bb55 --- /dev/null +++ b/group09/610673813/src/coding/week01/ArrayList.java @@ -0,0 +1,71 @@ +package coding.week01; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[2]; + + public void add(Object o){ + int len = elementData.length; + if(size >= len){ + Object[] new_elmentData = new Object[len*2]; + System.arraycopy(elementData, 0, new_elmentData, 0, size); + elementData = new_elmentData; + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + Object[] new_elementData = new Object[size+1]; + System.arraycopy(elementData, 0, new_elementData, 0, index); + System.arraycopy(elementData, index, new_elementData, index+1, size-index); + new_elementData[index] = o; + elementData = new_elementData; + size++; + } + + public Object get(int index){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + Object oldElement = elementData[index]; + if((index+1) != size){ + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + } + elementData[size-1] = null; + size --; + return oldElement; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + @Override + public String toString() { + String s = "{"; + for (int i = 0; i < size; i++) { + if(i == (size -1)){ + s += elementData[i] + "}"; + }else{ + s += elementData[i]+","; + } + } + return s; + } +} diff --git a/group09/610673813/src/coding/week01/BinaryTreeNode.java b/group09/610673813/src/coding/week01/BinaryTreeNode.java new file mode 100644 index 0000000000..da86e61d8f --- /dev/null +++ b/group09/610673813/src/coding/week01/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package coding.week01; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group09/610673813/src/coding/week01/Iterator.java b/group09/610673813/src/coding/week01/Iterator.java new file mode 100644 index 0000000000..a9b96bf891 --- /dev/null +++ b/group09/610673813/src/coding/week01/Iterator.java @@ -0,0 +1,7 @@ +package coding.week01; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group09/610673813/src/coding/week01/LinkedList.java b/group09/610673813/src/coding/week01/LinkedList.java new file mode 100644 index 0000000000..5e9ef79aea --- /dev/null +++ b/group09/610673813/src/coding/week01/LinkedList.java @@ -0,0 +1,104 @@ +package coding.week01; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + head = new Node(new Object(),null,null); + } + + public void add(Object o){ + Node last = head; + for (int i = 0; i < size; i++) { + last = last.next; + } + Node newNode = new Node(o,null,last); + last.next = newNode; + size++; + } + public void add(int index , Object o){ + Node oldNode = getNode(index); + Node newNode = new Node(o, oldNode, oldNode.prev); + oldNode.prev.next = newNode; + oldNode.prev = newNode; + size ++; + } + public Object get(int index){ + Node node = getNode(index); + return node.data; + } + + private Node getNode(int index){ + Node n = head.next; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + public Object remove(int index){ + Node node = getNode(index); + Object o =node.data; + Node prevNode = node.prev; + Node nextNode = node.next; + prevNode.next = nextNode; + nextNode.prev = prevNode; + node.next = node.prev =null; + node.data = null; + size --; + return o; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + Object o = remove(0); + return o; + } + public Object removeLast(){ + Object o = remove(size); + return o; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + Node prev; + + public Node(Object o,Node next,Node prev){ + this.data = o; + this.next = next; + this.prev = prev; + } + } + + @Override + public String toString() { + String s = "{"; + Node n = head; + for (int i = 0; i < size; i++) { + n = n.next; + if(i == (size -1)){ + s += n.data; + }else{ + s += n.data + ","; + } + } + s += "}"; + return s; + } +} diff --git a/group09/610673813/src/coding/week01/List.java b/group09/610673813/src/coding/week01/List.java new file mode 100644 index 0000000000..01ade190ab --- /dev/null +++ b/group09/610673813/src/coding/week01/List.java @@ -0,0 +1,9 @@ +package coding.week01; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group09/610673813/src/coding/week01/Queue.java b/group09/610673813/src/coding/week01/Queue.java new file mode 100644 index 0000000000..39b413b5fe --- /dev/null +++ b/group09/610673813/src/coding/week01/Queue.java @@ -0,0 +1,35 @@ +package coding.week01; + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + if(isEmpty()){ + throw new RuntimeException("已为空"); + } + Object o = list.get(0); + list.remove(0); + return o; + } + + public boolean isEmpty(){ + if(size() == 0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return list.size(); + } + + @Override + public String toString() { + return list.toString(); + } +} diff --git a/group09/610673813/src/coding/week01/Stack.java b/group09/610673813/src/coding/week01/Stack.java new file mode 100644 index 0000000000..040c878602 --- /dev/null +++ b/group09/610673813/src/coding/week01/Stack.java @@ -0,0 +1,39 @@ +package coding.week01; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(isEmpty()){ + throw new RuntimeException("已为空"); + } + Object o = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return o; + } + + public Object peek(){ + Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + int size = elementData.size(); + if(size == 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group09/610673813/src/coding/week02/LoginAction.java b/group09/610673813/src/coding/week02/LoginAction.java new file mode 100644 index 0000000000..896bc8c8cb --- /dev/null +++ b/group09/610673813/src/coding/week02/LoginAction.java @@ -0,0 +1,34 @@ +package coding.week02; + +public class LoginAction { + + private String name; + private String passWord; + private String message; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getPassWord() { + return passWord; + } + public void setPassWord(String passWord) { + this.passWord = passWord; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + 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"; + } +} diff --git a/group09/610673813/src/coding/week02/ReadXml.java b/group09/610673813/src/coding/week02/ReadXml.java new file mode 100644 index 0000000000..2a3441da99 --- /dev/null +++ b/group09/610673813/src/coding/week02/ReadXml.java @@ -0,0 +1,70 @@ +package coding.week02; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class ReadXml { + + private Document document = null; + private HashMap hashMap; + + public ReadXml(String filename) { + try { + document = new SAXReader().read((filename)); + hashMap = new HashMap(); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public String parseXml(String actionName) { + + // List actions = document.selectNodes("//struts/action"); + String className = null; + Element root = document.getRootElement(); + List actions = root.elements("action"); + if (actions.isEmpty()) { + return null; + } + for (Iterator iter = actions.iterator(); iter.hasNext();) { + Element element = (Element) iter.next(); + Attribute attr1 = element.attribute("name"); + if (attr1.getValue().equals(actionName)) { + Attribute attr2 = element.attribute("class"); + className = attr2.getValue(); + for (Iterator iterator = element.elementIterator(); iterator + .hasNext();) { + Element childElement = (Element) iterator.next(); + Attribute childAttribute = childElement.attribute("name"); + hashMap.put(childAttribute.getValue(), + childElement.getText()); + } + } + + } + return className; + } + + public String getJsp(String result) { + if (result == null) { + return null; + } + String string_jsp = null; + if (!hashMap.isEmpty()) { + for (String string : hashMap.keySet()) { + if (result.equals(string)) { + string_jsp = hashMap.get(string); + } + } + } + return string_jsp; + } +} diff --git a/group09/610673813/src/coding/week02/Struts.java b/group09/610673813/src/coding/week02/Struts.java new file mode 100644 index 0000000000..96cfe18a24 --- /dev/null +++ b/group09/610673813/src/coding/week02/Struts.java @@ -0,0 +1,115 @@ +package coding.week02; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Struts { + + private static final String NAME = "name"; + private static final String PASSWORD = "password"; + private static String excuteString; + private static Object object = null; + private static Class actionClass = null; + + + + + /* + + 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字段中。 + + */ + @SuppressWarnings("unchecked") + public static View runAction(String actionName, + Map parameters) { + + View view = new View(); + ReadXml readXml = new ReadXml("E:\\struts.xml"); + String classNameString = readXml.parseXml(actionName); + object = initAction(classNameString); + + excuteMethod(parameters); + + view.setParameterMap(setMapParameter()); + String jspResult = readXml.getJsp(excuteString); + view.setJsp(jspResult); + + return view; + } + + public static Object initAction(String classNameString) { +// System.out.println(classNameString); + try { + actionClass = Class.forName(classNameString); + } catch (ClassNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + Object newObject = null; + try { + newObject = actionClass.newInstance(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return newObject; + } + + public static void excuteMethod(Map parameters) { + + try { + Method methodOfName = actionClass + .getMethod("setName", String.class); + methodOfName.invoke(object, parameters.get(NAME)); + // + Method methodOfPassword = actionClass.getMethod("setPassWord", + String.class); + methodOfPassword.invoke(object, parameters.get(PASSWORD)); + + Method excuteMethod = actionClass.getMethod("execute"); + excuteString = (String) excuteMethod.invoke(object); + + } catch (Exception e) { + // TODO: handle exception + } + } + + public static Map setMapParameter() { + + Method[] getterMethods = actionClass.getMethods(); + HashMap hashMap = new HashMap<>(); + + for (int i = 0; i < getterMethods.length; i++) { + String getterName = getterMethods[i].getName(); + if (getterName.startsWith("get")) { + try { + String value = (String) getterMethods[i].invoke(object); + hashMap.put(getterName.substring(3).toLowerCase(), value); + //System.out.println("----" + getterName.substring(2)); + } catch (Exception e) { + // TODO: handle exception + } + + } + } + return hashMap; + } +} diff --git a/group09/610673813/src/coding/week02/StrutsTest.java b/group09/610673813/src/coding/week02/StrutsTest.java new file mode 100644 index 0000000000..53c8509e4b --- /dev/null +++ b/group09/610673813/src/coding/week02/StrutsTest.java @@ -0,0 +1,40 @@ +package coding.week02; + +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"); //�����Ԥ��IJ�һ�� + + 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/group09/610673813/src/coding/week02/View.java b/group09/610673813/src/coding/week02/View.java new file mode 100644 index 0000000000..01504286e6 --- /dev/null +++ b/group09/610673813/src/coding/week02/View.java @@ -0,0 +1,28 @@ +package coding.week02; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameter; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameter; + } + + public View setParameterMap(Map parameter) { + this.parameter = parameter; + return this; + } + +} From 2437b6c7e75a7e0f7322ffbe5ac86710a34ac330 Mon Sep 17 00:00:00 2001 From: lenovo2512009 <610673813@qq.com> Date: Mon, 6 Mar 2017 22:20:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group09/610673813/src/coding/week02/ReadXml.java | 2 -- group09/610673813/src/coding/week02/Struts.java | 7 +------ 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/group09/610673813/src/coding/week02/ReadXml.java b/group09/610673813/src/coding/week02/ReadXml.java index 2a3441da99..8215840d4d 100644 --- a/group09/610673813/src/coding/week02/ReadXml.java +++ b/group09/610673813/src/coding/week02/ReadXml.java @@ -20,14 +20,12 @@ public ReadXml(String filename) { document = new SAXReader().read((filename)); hashMap = new HashMap(); } catch (DocumentException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } public String parseXml(String actionName) { - // List actions = document.selectNodes("//struts/action"); String className = null; Element root = document.getRootElement(); List actions = root.elements("action"); diff --git a/group09/610673813/src/coding/week02/Struts.java b/group09/610673813/src/coding/week02/Struts.java index 96cfe18a24..f5cd7859e1 100644 --- a/group09/610673813/src/coding/week02/Struts.java +++ b/group09/610673813/src/coding/week02/Struts.java @@ -57,17 +57,14 @@ public static Object initAction(String classNameString) { try { actionClass = Class.forName(classNameString); } catch (ClassNotFoundException e1) { - // TODO Auto-generated catch block e1.printStackTrace(); } Object newObject = null; try { newObject = actionClass.newInstance(); } catch (InstantiationException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { - // TODO Auto-generated catch block e.printStackTrace(); } return newObject; @@ -88,7 +85,7 @@ public static void excuteMethod(Map parameters) { excuteString = (String) excuteMethod.invoke(object); } catch (Exception e) { - // TODO: handle exception + } } @@ -103,9 +100,7 @@ public static Map setMapParameter() { try { String value = (String) getterMethods[i].invoke(object); hashMap.put(getterName.substring(3).toLowerCase(), value); - //System.out.println("----" + getterName.substring(2)); } catch (Exception e) { - // TODO: handle exception } }