diff --git a/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml b/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml new file mode 100644 index 0000000000..54550a4174 --- /dev/null +++ b/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/798277403/src/week2/array/ArrayUtil.java b/group24/798277403/src/week2/array/ArrayUtil.java new file mode 100644 index 0000000000..04c03f95e0 --- /dev/null +++ b/group24/798277403/src/week2/array/ArrayUtil.java @@ -0,0 +1,235 @@ +package week2.array; + +import java.util.Arrays; + + +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 原数组 + */ + void reverseArray(int[] origin){ + if(origin!=null && origin.length>0){ + int i, n; + for(i=0, n=origin.length-1; i0) { + int index = 0; + int length = 0; + int[] temp = new int[oldArray.length]; + for(int i=0; iarray2[j]){ + merges[index] = array2[j]; + j++; + }else{ + merges[index] = array2[j]; + i++; + j++; + split++; + } + } + while(i0) { + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + stringBuffer.append(array[i]); + if (i != array.length - 1) { + stringBuffer.append(seperator); + } + } + return stringBuffer.toString(); + }else{ + return null; + } + } + +} diff --git a/group24/798277403/src/week2/array/ArrayUtilTest.java b/group24/798277403/src/week2/array/ArrayUtilTest.java new file mode 100644 index 0000000000..77c99242fa --- /dev/null +++ b/group24/798277403/src/week2/array/ArrayUtilTest.java @@ -0,0 +1,92 @@ +package week2.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Random; + +/** + * Created by zhouliang on 2017-03-13. + */ +public class ArrayUtilTest { + private int[] array; + private ArrayUtil arrayUtil ; + private int SIZE = 11; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + array = new int[SIZE]; + Random random = new Random(); + + for(int i=0; i 配置,以及execute的返回值, 确定哪一个jsp, +放到View对象的jsp字段中。 + +*/ +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = null; + Class actionClass = null; + LoginAction loginAction = null; + View view = new View(); + try { + db = documentBuilderFactory.newDocumentBuilder(); + Document document = db.parse("src/week2/litestruts/struts.xml"); + NodeList nodeList = document.getElementsByTagName("action"); + + //遍历每一个action节点 + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + //获取action节点的所有属性集合 + NamedNodeMap attrs = node.getAttributes(); + //获取name结点的值 + String nodeName = attrs.getNamedItem("name").getNodeValue(); + + if(nodeName.equals(actionName)){ + //获取LoginAction实例 + actionClass = Class.forName(attrs.getNamedItem("class").getNodeValue()); + loginAction = (LoginAction) actionClass.newInstance(); + + //设置用户名密码属性 + Set> entrySet = parameters.entrySet(); + for (Map.Entry entry : entrySet) { + if (entry.getKey().equals("name")) { + loginAction.setName(entry.getValue()); + } + if (entry.getKey().equals("password")) { + loginAction.setPassword(entry.getValue()); + } + } + + //执行execute()方法 + String result = loginAction.execute(); + + //将message封装到view + String message = loginAction.getMessage(); + Map map = new HashMap(); + map.put("message",message); + view.setParameters(map); + + //解析对应的result节点 + NodeList childNodes = node.getChildNodes(); + //遍历childNodes获取每个节点的节点名和节点值 + for (int k = 0; k < childNodes.getLength(); k++) { + Node childNode = childNodes.item(k); + //区分出text类型的node以及element类型的node + if (childNode.getNodeType() == Node.ELEMENT_NODE) { + NamedNodeMap attributes = childNode.getAttributes(); + String nodeValue = attributes.getNamedItem("name").getNodeValue(); + if(nodeValue.equals(result)){ + view.setJsp(childNode.getTextContent()); + } + } + + } + + } + + } + } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + + return view; + } + +/* DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = null; + Class actionClass = null; + LoginAction loginAction = null; + try { + db = documentBuilderFactory.newDocumentBuilder(); + Document document = db.parse("src/week2/litestruts/struts.xml"); + NodeList nodeList = document.getElementsByTagName("action"); + System.out.println("一共有" + nodeList.getLength() + "个结点"); + //遍历每一个action节点 + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + //获取action节点的所有属性集合 + NamedNodeMap attrs = node.getAttributes(); + //遍历action的属性 + for (int j = 0; j < attrs.getLength(); j++) { + //通过item(index)方法获取book节点的某一个属性 + Node attr = attrs.item(j); + String name = attrs.getNamedItem("name").getNodeValue(); + System.out.println("++++++++++"+name); + //获取属性名 + System.out.print("属性名:" + attr.getNodeName()); + //获取属性值 + System.out.println("--属性值" + attr.getNodeValue()); + if(attr.getNodeName().equals(actionName)){ + actionClass = Class.forName(attr.getNodeValue()); + loginAction = (LoginAction) actionClass.newInstance(); + } + } + //解析book节点的子节点 + NodeList childNodes = node.getChildNodes(); + //遍历childNodes获取每个节点的节点名和节点值 + for (int k = 0; k < childNodes.getLength(); k++) { + //区分出text类型的node以及element类型的node + if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) { + //获取了element类型节点的节点名 + System.out.print(childNodes.item(k).getNodeName()); + //获取了element类型节点的节点值 + System.out.println("--节点值是:" + childNodes.item(k).getFirstChild().getNodeValue()); + System.out.println("--节点值是:" + childNodes.item(k).getTextContent()); + } + } + } + } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + }*/ +} \ No newline at end of file diff --git a/group24/798277403/src/week2/litestruts/StrutsTest.java b/group24/798277403/src/week2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..5c4379d912 --- /dev/null +++ b/group24/798277403/src/week2/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package week2.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +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/group24/798277403/src/week2/litestruts/View.java b/group24/798277403/src/week2/litestruts/View.java new file mode 100644 index 0000000000..01a422a808 --- /dev/null +++ b/group24/798277403/src/week2/litestruts/View.java @@ -0,0 +1,23 @@ +package week2.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/798277403/src/week2/litestruts/struts.xml b/group24/798277403/src/week2/litestruts/struts.xml new file mode 100644 index 0000000000..54550a4174 --- /dev/null +++ b/group24/798277403/src/week2/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + +