diff --git a/group24/121111914/.gitignore b/group24/121111914/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group24/121111914/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/TestForDell.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/TestForDell.java new file mode 100644 index 0000000000..01d2c8d5fc --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/TestForDell.java @@ -0,0 +1,10 @@ +package com.github.ipk2015.coding2017.basic.test; + +public class TestForDell { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/array/ArrayUtil.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..d1ddfb6339 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.github.ipk2015.coding2017.coderising.array; + + + +import java.util.ArrayList; +import java.util.Arrays; + +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 temp=0; + for(int i=0;i list= new ArrayList(); + for(int i=0;i=array[i+1]){ + throw new RuntimeException("不是排序好的数组"); + } + } + } + /** + * 把一个已经存满数据的数组 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){ + int[] newArray=new int[oldArray.length+size]; + for(int i=0;i list=new ArrayList(); + list.add(1); + list.add(1); + int size=2; + int lastElement=2; + while(lastElement list=new ArrayList(); + list.add(2); + for(int i=3;i list=new ArrayList(); + for(int i=2;i parameters) throws Exception { + + /* + + 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字段中。 + + */ + SAXReader reader=new SAXReader(); + Document document = reader.read("src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml"); + Element rootElement = document.getRootElement(); + + Element actionElement=findElement(rootElement,"action","name",actionName); + Class armClass = Class.forName(actionElement.attributeValue("class")); + Object newInstance = armClass.newInstance(); + + setParameters(armClass,newInstance,parameters); + + Method exectueMethod = armClass.getMethod("execute"); + String exectueMethodResult = (String) exectueMethod.invoke(newInstance); + + View view=new View(); + Map viewParameters = getViewParameters(armClass,newInstance); + view.setParameters(viewParameters); + + Element resultElement=findElement(actionElement,"result","name",exectueMethodResult); + view.setJsp(resultElement.getStringValue()); + + return view; + } + + private static Element findElement(Element parent,String elementTag,String attributeName,String actionName){ + Element actionElement = null; + String nameAttributeValue=""; + List elements = parent.elements(elementTag); + for(Element element : elements){ + nameAttributeValue = element.attributeValue(attributeName); + if(nameAttributeValue.equals(actionName)){ + actionElement=element; + break; + } + } + return actionElement; + } + private static void setParameters(Class armClass,Object newInstance,Map parameters) throws Exception{ + Set> entrySet = parameters.entrySet(); + String entryKey=""; + String firstLetter=""; + Method method=null; + for(Entry entry:entrySet){ + entryKey=entry.getKey(); + firstLetter=entryKey.substring(0, 1); + entryKey=entryKey.replace(firstLetter,firstLetter.toUpperCase()); + method = armClass.getMethod("set"+entryKey, String.class); + method.invoke(newInstance, entry.getValue()); + } + } + private static Map getViewParameters(Class armClass,Object newInstance) throws Exception{ + Map map=new HashMap(); + Object getMethodReturn=null; + String methodName=""; + Method[] methods = armClass.getMethods(); + for(int i=0;i3 && methodName.startsWith("get")){ + getMethodReturn=methods[i].invoke(newInstance); + map.put(methodName.substring(3).toLowerCase(), getMethodReturn.toString()); + } + } + return map; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/StrutsTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..3f5f9bc98e --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/StrutsTest.java @@ -0,0 +1,61 @@ +package com.github.ipk2015.coding2017.coderising.litestruts; + + + +import static org.junit.Assert.fail; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +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 = null; + try { + view = Struts.runAction(actionName,params); + } catch (Exception e) { + // TODO Auto-generated catch block + fail("发生异常"); + e.printStackTrace(); + } + 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 = null; + try { + view = Struts.runAction(actionName,params); + } catch (Exception e) { + // TODO Auto-generated catch block + fail("发生异常"); + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/View.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..26f7bddb17 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/View.java @@ -0,0 +1,25 @@ +package com.github.ipk2015.coding2017.coderising.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/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0c1df8c87c --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file