diff --git a/group16/2562124714/.idea/misc.xml b/group16/2562124714/.idea/misc.xml index 05483570e0..518265079b 100644 --- a/group16/2562124714/.idea/misc.xml +++ b/group16/2562124714/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/group16/2562124714/.idea/workspace.xml b/group16/2562124714/.idea/workspace.xml index bba44e297b..254327333b 100644 --- a/group16/2562124714/.idea/workspace.xml +++ b/group16/2562124714/.idea/workspace.xml @@ -27,125 +27,58 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - + + - - - + + + + + - - + + - - + + - - - - - - - - - - - - @@ -173,18 +106,23 @@ @@ -195,10 +133,10 @@ DEFINITION_ORDER - @@ -215,8 +153,8 @@ + - @@ -295,6 +233,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -343,7 +363,7 @@ - + @@ -436,6 +456,32 @@ + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -717,15 +625,6 @@ - - - - - - - - - - - - - - - @@ -793,34 +686,20 @@ - - - - - - - - - - - - - - - - - + + - - - - - + + + + + + @@ -843,11 +722,35 @@ - - + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + @@ -863,38 +766,38 @@ - + + + - - - - + - + - + + @@ -914,6 +817,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -931,20 +881,7 @@ - - - - - - - - - - - - - - + @@ -952,9 +889,7 @@ - - - + @@ -962,9 +897,7 @@ - - - + @@ -972,7 +905,9 @@ - + + + @@ -981,7 +916,7 @@ - + @@ -1049,20 +984,7 @@ - - - - - - - - - - - - - - + @@ -1070,9 +992,7 @@ - - - + @@ -1081,7 +1001,7 @@ - + @@ -1136,20 +1056,7 @@ - - - - - - - - - - - - - - + @@ -1180,13 +1087,6 @@ - - - - - - - @@ -1261,105 +1161,165 @@ - + - - + + - - - - - - - - - - - - + + + + + + + + - + - - + + - + - - + + + + + + + + + + - + - + - - + + + + + + + + + + + + + + + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group16/2562124714/src/Test/TestRunner.java b/group16/2562124714/src/Test/TestRunner.java index 963fb955d3..cd19d2a110 100644 --- a/group16/2562124714/src/Test/TestRunner.java +++ b/group16/2562124714/src/Test/TestRunner.java @@ -1,5 +1,6 @@ package Test; +import com.coding.basic.linklist.LRUPageFrameTest; import org.junit.runner.JUnitCore; import org.junit.runner.notification.Failure; @@ -10,7 +11,7 @@ */ public class TestRunner { public static void main(String[] args) { - org.junit.runner.Result result = JUnitCore.runClasses(StrutsTest.class); + org.junit.runner.Result result = JUnitCore.runClasses(LRUPageFrameTest.class); for (Failure failure:result.getFailures()) { System.out.println(failure.toString()); } diff --git a/group16/2562124714/src/com/coderising/jvm/loader/ClassFileLoader.java b/group16/2562124714/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..6efb40eb20 --- /dev/null +++ b/group16/2562124714/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,115 @@ +package com.coderising.jvm.loader; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + //找到文件的具体路径 + if(clzPaths.size() == 0 || className == "") + { + return null; + } + + String []path = className.split("\\."); + String MainPath = ""; + String FileName = "\\"; + + for (int i = 0; i < path.length - 1; i++) + { + FileName += path[i] + "\\"; + } + FileName += path[path.length - 1] + ".class"; + System.out.println(FileName); + + for (String item: clzPaths + ) { + if (new File(item + FileName).exists()) + { + MainPath += item; + break; + } + } + + if (MainPath == "") + { + return null; + } + + File file = new File(MainPath + FileName); + InputStream is = null; + byte[] buffer = new byte[(int)file.length()]; + try + { + System.out.println(MainPath + FileName); + is = new FileInputStream(MainPath + FileName); + // read stream data into buffer + is.read(buffer); + + return buffer; + + + + } + catch(Exception e) { + + // if any I/O error occurs + e.printStackTrace(); + return null; + } finally { + + // releases system resources associated with this stream + if(is!=null) + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + private byte[] loadClassFile(String clzFileName) { + + return null; + } + + + + public void addClassPath(String path) { + clzPaths.add(path); + + } + + public String getClassPath_V1(){ + + return null; + } + + public String getClassPath(){ + if (clzPaths.size() == 0) + { + return ""; + } + + String ClassPath = ""; + for (String item: + clzPaths) { + ClassPath += item + ";"; + } + ClassPath = ClassPath.substring(0, ClassPath.length() - 1); //干掉最后一个 ; + + return ClassPath; + } + + + + + +} diff --git a/group16/2562124714/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group16/2562124714/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..a697ae48b8 --- /dev/null +++ b/group16/2562124714/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "C:\\Users\\cs\\Desktop\\javacoding2017\\coding2017\\group16\\2562124714\\out\\production\\2562124714"; + static String path2 = "C:\\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i this.size()) + { + return null; + } + for (i = 1, node = head; i < index ; i++, node = node.next) { } return node.data; // return null; } + + public void set(int index, Object o) + { + if (o == null || index > this.size()) + { + return; + } + + Node node; + int i; + for (i = 1, node = head; i < index ; i++, node = node.next) + { + } + + node.data = o; + + } + public Object remove(int index){ Node node; int i = 0; diff --git a/group16/2562124714/src/com/coding/basic/linklist/LRUPageFrame.java b/group16/2562124714/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..3e113e0562 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,131 @@ +package com.coding.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + if (capacity <= 0) + { + capacity = 0; + + } + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + if (first == null) + { + if (capacity == 0) + { + return; + } + + Node newnode = new Node(); + newnode.pageNum = pageNum; + newnode.prev = null; + newnode.next = null; + + this.first = newnode; + this.last = newnode; + this.capacity--; + } + + //遍历 最近访问过 + for (Node item = first; item.next != null; item = item.next) + { + if (item.pageNum == pageNum && item == first) + { + return; + } + if (item.pageNum == pageNum && item == last) + { + item.prev.next = null; + last = item.prev; + first.prev = item; + item.next = first; + item.prev = null; + first = item; + return; + } + if (item.pageNum == pageNum) + { + item.prev.next = item.next; + item.next.prev = item.prev; + item.next = first; + first.prev = item; + first = item; + return; + } + } + + //没有访问过 + Node newnode = new Node(); + newnode.pageNum = pageNum; + newnode.prev = null; + newnode.next = null; + if (this.capacity > 0) + { + first.prev = newnode; + newnode.next = first; + first = newnode; + this.capacity --; + } + else + { + first.prev = newnode; + newnode.next = first; + first = newnode; + Node templast = last; + last = last.prev; + last.next = null; + templast = null; + } + + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group16/2562124714/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group16/2562124714/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..67cf36067b --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group16/2562124714/src/com/coding/basic/linklist/LinkedListUtil.java b/group16/2562124714/src/com/coding/basic/linklist/LinkedListUtil.java new file mode 100644 index 0000000000..a5a6df55bc --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/linklist/LinkedListUtil.java @@ -0,0 +1,289 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import com.coding.basic.List; + +import java.util.Objects; + +public class LinkedListUtil { + private LinkedList linkedlist = null; + + public LinkedListUtil(LinkedList linkedlistArgs) + { + linkedlist = linkedlistArgs; + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if (linkedlist == null || linkedlist.size() == 1) + { + return; + } + + int middle = linkedlist.size() / 2; + Object dataobject; + + for (int i = 0; i < middle; i ++) + { + dataobject = linkedlist.get(i + 1); + linkedlist.set(i + 1, linkedlist.get(linkedlist.size() - i)); + linkedlist.set(linkedlist.size() - i, dataobject); + } + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if (linkedlist == null || linkedlist.size() == 1) + { + return; + } + + int middle = linkedlist.size() / 2; + + for (int i = 0; i < middle; i++) + { + linkedlist.removeFirst(); + } + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if (linkedlist == null || linkedlist.size() < length + i) + { + return; + } + + for (int j = 1; j <= length; j++) + { + linkedlist.remove(i + j); + } + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if (list == null || list.size() == 0) + { + return null; + } + int[] result = new int[list.size()]; + + for (int i = 0; i < list.size(); i++) + { + int index = (int)list.get(i + 1); + if (linkedlist.get(index) != null) + { + result[i] = (int) linkedlist.get(index); + } + } + + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + if (list == null || list.size() == 0) + { + return; + } + + int count = 1; + int i = 1; + while (true) + { + + int data = (int)list.get(count); + + if ((int)linkedlist.get(i) > data) + { + if (i == 1) + { + count++; + } + else + { + i--; + continue; + } + } + else if((int)linkedlist.get(i) < data) + { + if (i == linkedlist.size()) + { + count ++; + } + else + { + i++; + continue; + + } + } + else + { + linkedlist.remove(i); + count++; + } + + if (count == list.size() + 1) //跳出条件 list 已经被遍历查找完 + { + break; + } + } + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + //每个元素向左右查找 一旦发现一个方向的值不等于该值 立刻停止该方向搜索 搜索2 - size() - 1 + if (linkedlist == null || linkedlist.size() < 2) + { + return; + } + + for (int i = 2; i < linkedlist.size(); i++) + { + Object data = (int)linkedlist.get(i); + //left + for (int j = i - 1; j > 0; j++) + { + if ((int)linkedlist.get(j) == data) + { + linkedlist.remove(j); + continue; + } + else + { + break; + } + } + + //right + for (int j = i + 1; j < linkedlist.size() + 1; j++) + { + if ((int)linkedlist.get(j) == data) + { + linkedlist.remove(j); + continue; + } + else + { + break; + } + } + } + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + //遍历到比min大的地方开始删除到比max大的地方终止 + + if (linkedlist == null || linkedlist.size() == 0 || max < min) + { + return; + } + + for (int i = 1; i <= linkedlist.size(); i++) + { + if ((int)linkedlist.get(i) <= min) + { + continue; + } + else + { + for (int j = i; j <= linkedlist.size();j++) + { + if ((int)linkedlist.get(j) <= max) + { + linkedlist.remove(j); + } + else + { + break; + } + } + } + + break; + } + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + //寻找list中的最小元素在的地方! 此处作为起点遍历 + if (list == null || list.size() == 0 || linkedlist == null || linkedlist.size() == 0) + { + return null; + } + LinkedList C = new LinkedList(); + + int count = 1; + for (int i = 1; i <= linkedlist.size(); i++) + { + if ((int)linkedlist.get(i) < (int)list.get(count)) + { + continue; + } + else + { + if ((int)linkedlist.get(i) == (int)list.get(count)) + { + C.add(list.get(count)); + count++; + } + else + { + count++; + i--; + } + } + if (count == list.size() + 1) + { + break; + } + } + + return C; + } +} diff --git a/group16/502059278/.classpath b/group16/502059278/.classpath index c0abaf014f..5b2bf0d077 100644 --- a/group16/502059278/.classpath +++ b/group16/502059278/.classpath @@ -4,5 +4,6 @@ + diff --git a/group16/502059278/.project b/group16/502059278/.project index 72a951f7c1..91470f2801 100644 --- a/group16/502059278/.project +++ b/group16/502059278/.project @@ -5,6 +5,11 @@ + + org.eclipse.wst.common.project.facet.core.builder + + + org.eclipse.jdt.core.javabuilder @@ -13,5 +18,6 @@ org.eclipse.jdt.core.javanature + org.eclipse.wst.common.project.facet.core.nature diff --git a/group16/502059278/.settings/org.eclipse.jdt.core.prefs b/group16/502059278/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..f42de363af --- /dev/null +++ b/group16/502059278/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group16/502059278/.settings/org.eclipse.wst.common.project.facet.core.xml b/group16/502059278/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000000..bc0009a455 --- /dev/null +++ b/group16/502059278/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/Configuration.java b/group16/502059278/src/cn/mark/work0226/litestruts/Configuration.java new file mode 100644 index 0000000000..54760e9832 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/Configuration.java @@ -0,0 +1,116 @@ +package cn.mark.work0226.litestruts; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + +/** + * 解析Struts配置文件 + * @author Mark + * @date 2017年3月28日 + */ +public class Configuration { + Map actions = new HashMap<>(); + + public Configuration(String fileName){ + String packageName = this.getClass().getPackage().getName(); + packageName = packageName.replace('.', '/'); +// System.out.println(packageName); + InputStream is = this.getClass().getResourceAsStream("/"+packageName+"/"+fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 解析XML文件 + * @param is 指向配置文件的文件流 + */ + private void parseXML(InputStream is) { + SAXBuilder builder = new SAXBuilder(); + try { + Document doc = builder.build(is); + Element root = doc.getRootElement(); + for( Element actionElement : root.getChildren("action") ){ + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName,clzName); + + for( Element resultElement : actionElement.getChildren("result") ){ + + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + } + this.actions.put(actionName, ac); + } + + } catch (JDOMException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public String getClassName(String action){ + ActionConfig ac = this.actions.get(action); + if (ac == null){ + return null; + } + return ac.getClzName(); + } + + public String getResultView(String action, String resultName){ + ActionConfig ac = this.actions.get(action); + if ( ac == null ){ + return null; + } + return ac.getViewName(resultName); + } + + + /** + * action标签对象 + * @author Mark + * @date 2017年3月28日 + */ + private static class ActionConfig{ + String name; + String clzName; + Map viewResult = new HashMap<>(); + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + + + public String getClzName() { + return clzName; + } + + + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + } + + public String getViewName(String resultName){ + return viewResult.get(resultName); + } + + } + +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/ReflectionUtil.java b/group16/502059278/src/cn/mark/work0226/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..62715b36cd --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/ReflectionUtil.java @@ -0,0 +1,66 @@ +package cn.mark.work0226.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + public static List getSetterMethods(Class clz) { + return getMethods(clz, "set"); + } + + public static void setParameters(Object o, Map params) { + List methods = getSetterMethods(o.getClass()); + + for (String name : params.keySet()) { + String methodName = "set" + name; + + for (Method m : methods) { + if (m.getName().equalsIgnoreCase(methodName)) { + try { + m.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + } + + public static List getGetterMethods(Class clz){ + return getMethods(clz,"get"); + } + + private static List getMethods(Class clz, String startWithName) { + List methods = new ArrayList<>(); + for ( Method m : clz.getDeclaredMethods() ){ + if ( m.getName().startsWith(startWithName) ){ + methods.add(m); + } + } + return methods; + } + + public static Map getParameterMap(Object o){ + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); + + for ( Method m : methods ){ + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + + try { + Object value = m.invoke(o); + params.put(name, value); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + return params; + } +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java b/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java index 93a449adaf..2435605630 100644 --- a/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java +++ b/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java @@ -3,6 +3,8 @@ import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -15,8 +17,11 @@ public class Struts { + + private static final Configuration CFG = new Configuration("struts.xml"); public static View runAction(String actionName, Map parameters) { + /* @@ -27,7 +32,7 @@ public static View runAction(String actionName, Map parameters) { ("name"="test" , "password"="1234") , 那就应该调用 setName和setPassword方法 - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , @@ -37,48 +42,45 @@ public static View runAction(String actionName, Map parameters) { 放到View对象的jsp字段中。 */ - String classPath = null; - String className = null; + String clzName = CFG.getClassName(actionName); - Document dom = XMLUtils.getDocument("bin"+File.separator+"struts.xml"); - Element ele = XMLUtils.getElement(dom, actionName); - Attribute classAttr = ele.attribute("class"); - classPath = classAttr.getValue(); - className = classPath.substring(classPath.lastIndexOf(".")+1); - System.out.println(className); + if (clzName == null){ + return null; + } - - - try { - Class clz = Class.forName(classPath); - System.out.println(clz.getName()); + try { + Class clz = Class.forName(clzName); + Object action = clz.newInstance(); - + ReflectionUtil.setParameters(action, parameters); + + Method m = clz.getDeclaredMethod("execute"); + String resultName = (String)m.invoke(action); + + Map params = ReflectionUtil.getParameterMap(clz); + String resultView = CFG.getResultView(actionName, resultName); + View view = new View(); + view.setParameters(params); + view.setJsp(resultName); + return view; } catch (ClassNotFoundException e) { e.printStackTrace(); - } catch (SecurityException e) { - // TODO Auto-generated catch block + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block e.printStackTrace(); - } + } catch (InvocationTargetException e) { + e.printStackTrace(); + } return null; - } - - - public static void main(String[] args) { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - Struts.runAction(actionName,params); - } + } } diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/struts.xml b/group16/502059278/src/cn/mark/work0226/litestruts/struts.xml new file mode 100644 index 0000000000..1aaa6ea5a0 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/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