From a7be88a66b10a87898a73443e528ac190d25adbb Mon Sep 17 00:00:00 2001
From: AllenLink <3168592570@qq.com>
Date: Sun, 5 Mar 2017 18:49:55 +0800
Subject: [PATCH] Ok
The second,s homework.
---
group10/3314793852/second/.classpath | 7 +
group10/3314793852/second/.gitignore | 1 +
group10/3314793852/second/.project | 17 ++
.../org.eclipse.core.resources.prefs | 4 +
.../.settings/org.eclipse.jdt.core.prefs | 11 +
.../src/com/coderising/array/ArrayUtil.java | 236 ++++++++++++++++++
.../second/src/com/coderising/array/Test.java | 63 +++++
.../coderising/litestruts/DealWithInfo.java | 90 +++++++
.../coderising/litestruts/LoginAction.java | 39 +++
.../com/coderising/litestruts/SAXGetInfo.java | 42 ++++
.../src/com/coderising/litestruts/Struts.java | 165 ++++++++++++
.../com/coderising/litestruts/StrutsTest.java | 43 ++++
.../src/com/coderising/litestruts/View.java | 23 ++
.../com/coderising/litestruts/struts.out.xml | 0
.../src/com/coderising/litestruts/struts.xml | 11 +
15 files changed, 752 insertions(+)
create mode 100644 group10/3314793852/second/.classpath
create mode 100644 group10/3314793852/second/.gitignore
create mode 100644 group10/3314793852/second/.project
create mode 100644 group10/3314793852/second/.settings/org.eclipse.core.resources.prefs
create mode 100644 group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs
create mode 100644 group10/3314793852/second/src/com/coderising/array/ArrayUtil.java
create mode 100644 group10/3314793852/second/src/com/coderising/array/Test.java
create mode 100644 group10/3314793852/second/src/com/coderising/litestruts/DealWithInfo.java
create mode 100644 group10/3314793852/second/src/com/coderising/litestruts/LoginAction.java
create mode 100644 group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java
create mode 100644 group10/3314793852/second/src/com/coderising/litestruts/Struts.java
create mode 100644 group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java
create mode 100644 group10/3314793852/second/src/com/coderising/litestruts/View.java
create mode 100644 group10/3314793852/second/src/com/coderising/litestruts/struts.out.xml
create mode 100644 group10/3314793852/second/src/com/coderising/litestruts/struts.xml
diff --git a/group10/3314793852/second/.classpath b/group10/3314793852/second/.classpath
new file mode 100644
index 0000000000..373dce4005
--- /dev/null
+++ b/group10/3314793852/second/.classpath
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/group10/3314793852/second/.gitignore b/group10/3314793852/second/.gitignore
new file mode 100644
index 0000000000..ae3c172604
--- /dev/null
+++ b/group10/3314793852/second/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/group10/3314793852/second/.project b/group10/3314793852/second/.project
new file mode 100644
index 0000000000..fab8d7f04c
--- /dev/null
+++ b/group10/3314793852/second/.project
@@ -0,0 +1,17 @@
+
+
+ 2017Learning
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs b/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000000..5855257b83
--- /dev/null
+++ b/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,4 @@
+eclipse.preferences.version=1
+encoding//src/com/coderising/litestruts=UTF-8
+encoding//src/com/coderising/litestruts/Struts.java=UTF-8
+encoding/=UTF-8
diff --git a/group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs b/group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000000..3a21537071
--- /dev/null
+++ b/group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,11 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.8
diff --git a/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java b/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java
new file mode 100644
index 0000000000..5742ea09f9
--- /dev/null
+++ b/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java
@@ -0,0 +1,236 @@
+
+ package com.coderising.array;
+
+ 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 size=origin.length-1;
+ int[] arr=new int[origin.length];
+ for(int i=0;iarray1[j]&&array1[i]!=0&&array1[j]!=0){
+ int temp;
+ temp=array1[i];
+ array1[i]=array1[j];
+ array1[j]=temp;
+ }
+ if(array1[i]==array1[j]&&array1[i]!=0&&array1[j]!=0){
+ array1[j]=0;
+ }
+ }
+ }
+
+ System.out.println(Arrays.toString(array1));
+
+ int[] array3=removeZero(array1); //除零操作。
+ return array3;
+ }
+ /**
+ * 把一个已经存满数据的数组 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[] newArr=new int[oldArray.length+size];
+ for(int i=0;i2){
+ arr[0]=1;
+ arr[1]=1;
+ while(second aMap=new HashMap(); //一个用来存储。
+
+ private ArrayList> list=new ArrayList>();; //List集合用来保存Map集合。
+
+ private String currentTag;
+
+ @Override
+ public void startElement(String uri, String localName, String qName, Attributes attributes)
+ throws SAXException {
+ currentTag =qName;
+
+ if("action".equals(currentTag)){
+ key=attributes.getValue(0); //action的name属性。
+ value=attributes.getValue(1); //action的class属性。
+
+ //在将属性成对的保存到一个Map集合中。
+ aMap.put(key, value);
+
+ //保存后将中间变量变为null.
+ key=null;
+ value=null;
+ }
+
+ if("result".equals(currentTag)){
+ key=attributes.getValue(0); //result的name属性。
+ }
+
+ }
+
+
+ @Override
+ public void characters(char[] ch, int start, int length) throws SAXException {
+ if("result".equals(currentTag)){
+ String name=new String(ch,start,length);
+ value=name;
+ //将属性成对的保存到一个Map集合中。
+
+ aMap.put(key, value);
+
+ //保存后将中间变量清空。
+ key=null;
+ value=null;
+ currentTag=null;
+ }
+
+ }
+
+
+ @Override
+ public void endElement(String uri, String localName, String qName) throws SAXException {
+ if("action".equals(qName)){
+
+ list.add(aMap);
+
+
+ aMap=new HashMap();
+
+
+ }
+
+ }
+
+ //返回list集合。
+ public ArrayList> getDate(){
+ return list;
+ }
+ }
diff --git a/group10/3314793852/second/src/com/coderising/litestruts/LoginAction.java b/group10/3314793852/second/src/com/coderising/litestruts/LoginAction.java
new file mode 100644
index 0000000000..dcdbe226ed
--- /dev/null
+++ b/group10/3314793852/second/src/com/coderising/litestruts/LoginAction.java
@@ -0,0 +1,39 @@
+package com.coderising.litestruts;
+
+/**
+ * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
+ * @author liuxin
+ *
+ */
+public class LoginAction{
+ private String name ;
+ private String password;
+ private String message;
+
+ public String getName() {
+ return name;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ 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";
+ }
+
+ public void setName(String name){
+ this.name = name;
+ }
+ public void setPassword(String password){
+ this.password = password;
+ }
+ public String getMessage(){
+ return this.message;
+ }
+}
diff --git a/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java b/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java
new file mode 100644
index 0000000000..52dd35961d
--- /dev/null
+++ b/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java
@@ -0,0 +1,42 @@
+ /*
+ *该类用于读取struts.xml文件中的数据,并把它存储到ActionType类的对象中去。
+ */
+ package com.coderising.litestruts;
+
+ import java.io.File;
+ import java.io.FileInputStream;
+ import java.io.IOException;
+ import java.io.InputStream;
+ import java.util.ArrayList;
+ import java.util.HashMap;
+
+ import javax.xml.parsers.ParserConfigurationException;
+ import javax.xml.parsers.SAXParser;
+ import javax.xml.parsers.SAXParserFactory;
+
+ import org.xml.sax.InputSource;
+ import org.xml.sax.SAXException;
+ import org.xml.sax.XMLReader;
+
+
+
+ public class SAXGetInfo {
+
+ public ArrayList> getDate() throws SAXException, IOException, ParserConfigurationException{
+ //创建解析工厂
+ SAXParserFactory factory=SAXParserFactory.newInstance();
+
+ //创建解析器
+ SAXParser parser=factory.newSAXParser();
+
+ //文件地址
+ String fileName="E:/CODING2017/Code/coding2017/group10/3314793852/second/src/com/coderising/litestruts/struts.xml";
+ //设置内容处理器
+ DealWithInfo handler=new DealWithInfo();
+ parser.parse(fileName, handler);
+
+ ArrayList> list=handler.getDate();
+ return list; //将保存在list集合中的配置信息返回。
+ }
+
+ }
diff --git a/group10/3314793852/second/src/com/coderising/litestruts/Struts.java b/group10/3314793852/second/src/com/coderising/litestruts/Struts.java
new file mode 100644
index 0000000000..5312535300
--- /dev/null
+++ b/group10/3314793852/second/src/com/coderising/litestruts/Struts.java
@@ -0,0 +1,165 @@
+
+ package com.coderising.litestruts;
+
+ import java.io.IOException;
+ import java.util.ArrayList;
+ import java.util.HashMap;
+ import java.util.Iterator;
+ import java.util.Map;
+
+ import javax.xml.parsers.ParserConfigurationException;
+
+ import org.xml.sax.SAXException;
+
+ import java.lang.reflect.InvocationTargetException;
+ import java.lang.reflect.Method;
+
+
+
+ public class Struts {
+
+ public static View runAction(String actionName, Map parameters) {
+ View view=new View();
+
+ String execute=null; //execute返回值。
+ String placeOfJsp=null; //JSP地址。
+ String classname=getClassName(actionName);
+
+ Class> classAction = null;
+ Object obj=null;
+ try {
+ classAction=Class.forName(classname); //根据类名反射实例化class类。
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+
+ //ClassAction类使用newInstance();方法调用LoginAction类的默认构造方法创建LoginAction类。
+ try {
+ obj=classAction.newInstance(); //Object类型的对象。
+ } catch (InstantiationException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ }
+
+ //调用对象的setter方法,设置用户名和密码,即把name和password的值设置到当前LoginAction的对象中。
+ try {
+ setter(obj,"name",parameters.get("name"),String.class);
+ setter(obj,"password",parameters.get("password"),String.class);
+ } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
+ | InvocationTargetException e) {
+ e.printStackTrace();
+ }
+
+ //调用对象的exectue方法。
+ try{
+ Method method=classAction.getMethod("execute");
+ execute=(String) method.invoke(obj);
+ }catch(Exception e){
+ e.printStackTrace();
+ }
+
+ //调用对象的所有getter方法,把值设置到view对象的parameters属性中。
+ Map para=new HashMap();
+ try{
+ para.put("name", getter(obj,"name"));
+ para.put("password", getter(obj,"password"));
+ para.put("message", getter(obj,"message"));
+ }catch(Exception e){
+ e.printStackTrace();
+ }
+ view.setParameters(para);
+
+ //根据execute返回值和配置,将JSP地址赋值给View对象的jsp成员中。
+ placeOfJsp=getResultName(actionName,execute);
+ view.setJsp(placeOfJsp);
+
+ //System.out.println(view.getJsp()+" "+view.getParameters());
+ return view;
+ }
+
+
+ /*
+ * 第一个参数为操作对象,第二个参数为操作的数据的数据成员,第三个参数为set的值,第四个参数为参数的类型
+ */
+ private static void setter(Object obj, String att, Object value, Class> type) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ //实例化一个set方法
+ Method method=obj.getClass().getMethod("set"+initStr(att), type);
+ method.invoke(obj, value);
+ }
+
+ /*
+ * getter方法
+ */
+ private static String getter(Object obj,String att) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
+ Method method=obj.getClass().getMethod("get"+initStr(att));
+ method.invoke(obj);
+ return (String) method.invoke(obj);
+ }
+
+ /*
+ * 根据java的命名规则,数据成员的第一个单词都要小写,其他单词的首字母大写。
+ * 所以把set和get方法后面的单词的首字母大写,如setName中的N
+ * 和getName中的N.
+ */
+ private static String initStr(String old) {
+ String str=old.substring(0,1).toUpperCase()+old.substring(1);
+ //把首字母大写。
+ return str;
+ }
+
+ public static void main(String args[]){
+ String actionName = "login";
+ Map params = new HashMap();
+ params.put("name","test");
+ params.put("password","1234");
+ runAction(actionName,params);
+ }
+
+ //查找action类,查找result时,必须要通过查找action才行。
+ private static String getClassName(String actionName){
+ String className=getData(actionName).get(actionName);
+
+ return className;
+ }
+
+ //查找result时,必须要通过查找action才行。
+ private static String getResultName(String actionName,String result){
+ String placeOfJsp=getData(actionName).get(result);
+
+ return placeOfJsp;
+ }
+
+ //从配置信息中获取数据,获取
+ private static HashMap getData(String actionName) {
+ SAXGetInfo a=new SAXGetInfo();
+ ArrayList> x;
+ HashMap y=null;
+ Object[] arr=new Object[10];
+ int size=0;
+
+ try {
+ x = a.getDate();
+ Iterator it=x.iterator();
+ while(it.hasNext()){
+ arr[size]=(Object)it.next();
+ size++;
+
+ }
+ } catch (SAXException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (ParserConfigurationException e) {
+ e.printStackTrace();
+ }
+ if(actionName.equals("login")){
+ y=(HashMap) arr[0];
+ }
+ if(actionName.equals("logout")){
+ y=(HashMap) arr[1];
+ }
+ return y;
+ }
+
+ }
diff --git a/group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java b/group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java
new file mode 100644
index 0000000000..b8c81faf3c
--- /dev/null
+++ b/group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java
@@ -0,0 +1,43 @@
+package com.coderising.litestruts;
+
+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/group10/3314793852/second/src/com/coderising/litestruts/View.java b/group10/3314793852/second/src/com/coderising/litestruts/View.java
new file mode 100644
index 0000000000..07df2a5dab
--- /dev/null
+++ b/group10/3314793852/second/src/com/coderising/litestruts/View.java
@@ -0,0 +1,23 @@
+package com.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/group10/3314793852/second/src/com/coderising/litestruts/struts.out.xml b/group10/3314793852/second/src/com/coderising/litestruts/struts.out.xml
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/group10/3314793852/second/src/com/coderising/litestruts/struts.xml b/group10/3314793852/second/src/com/coderising/litestruts/struts.xml
new file mode 100644
index 0000000000..07f80b6476
--- /dev/null
+++ b/group10/3314793852/second/src/com/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