Skip to content

Commit 84c2847

Browse files
authored
Merge pull request onlyliuxin#22 from conf1102/master
Mar 5th Homework
2 parents 1785ccd + b818b24 commit 84c2847

File tree

7 files changed

+409
-0
lines changed

7 files changed

+409
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.coderising.array;
2+
3+
import java.util.Arrays;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
public class ArrayUtil {
8+
9+
/**
10+
* 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a =
11+
* [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
12+
*
13+
* @param origin
14+
* @return
15+
*/
16+
public void reverseArray(int[] origin) {
17+
int[] reversedArray = new int[origin.length];
18+
for (int i = origin.length - 1, j = 0; i >= 0 && j < origin.length; i--, j++) {
19+
reversedArray[j] = origin[i];
20+
}
21+
origin = reversedArray;
22+
}
23+
24+
/**
25+
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
26+
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5}
27+
*
28+
* @param oldArray
29+
* @return
30+
*/
31+
32+
public int[] removeZero(int[] oldArray) {
33+
int zeroCount = 0;
34+
for (int i = 0; i < oldArray.length; i++) {
35+
if(oldArray[i] == 0){
36+
zeroCount ++;
37+
}
38+
}
39+
int[] removedZeroArray = new int[oldArray.length - zeroCount];
40+
41+
for (int i = 0,j = 0; i < oldArray.length; i++,j++) {
42+
if(oldArray[i] == 0){
43+
j--;
44+
continue;
45+
}
46+
removedZeroArray[j] = oldArray[i];
47+
}
48+
return removedZeroArray;
49+
}
50+
51+
/**
52+
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 =
53+
* [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
54+
*
55+
* @param array1
56+
* @param array2
57+
* @return
58+
*/
59+
60+
public int[] merge(int[] array1, int[] array2) {
61+
return null;
62+
}
63+
64+
/**
65+
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
66+
* 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
67+
* [2,3,6,0,0,0]
68+
*
69+
* @param oldArray
70+
* @param size
71+
* @return
72+
*/
73+
public int[] grow(int[] oldArray, int size) {
74+
return null;
75+
}
76+
77+
/**
78+
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 ,
79+
* 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 []
80+
*
81+
* @param max
82+
* @return
83+
*/
84+
public int[] fibonacci(int max) {
85+
return null;
86+
}
87+
88+
/**
89+
* 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
90+
*
91+
* @param max
92+
* @return
93+
*/
94+
public int[] getPrimes(int max) {
95+
return null;
96+
}
97+
98+
/**
99+
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
100+
*
101+
* @param max
102+
* @return
103+
*/
104+
public int[] getPerfectNumbers(int max) {
105+
return null;
106+
}
107+
108+
/**
109+
* 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9"
110+
*
111+
* @param array
112+
* @param s
113+
* @return
114+
*/
115+
public String join(int[] array, String seperator) {
116+
return null;
117+
}
118+
119+
public static void main(String[] args) {
120+
int[] origin = { 0, 1, 2, 0, 12 };
121+
new ArrayUtil().removeZero(origin);
122+
123+
}
124+
125+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.coderising.litestruts;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import org.w3c.dom.Node;
10+
11+
import com.coderising.litestruts.utils.StrutsUtil;
12+
import com.coderising.litestruts.view.View;
13+
14+
public class Struts {
15+
16+
public static View runAction(String actionName, Map<String, String> params) {
17+
18+
/*
19+
*
20+
* 0. 读取配置文件struts.xml
21+
*
22+
* 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)
23+
* 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" ,
24+
* "password"="1234") , 那就应该调用 setName和setPassword方法
25+
*
26+
* 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"
27+
*
28+
* 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如
29+
* {"message": "登录成功"} , 放到View对象的parameters
30+
*
31+
* 4. 根据struts.xml中的 <result> 配置,以及execute的返回值, 确定哪一个jsp,
32+
* 放到View对象的jsp字段中。
33+
*
34+
*/
35+
View view = new View();
36+
String className = readActionInConfig(actionName);
37+
List<String> rtnList = invokeMethod(className, params);
38+
String result = rtnList.get(0);
39+
String msg = rtnList.get(1);
40+
view.setParameters(buildViewParams(msg));
41+
view.setJsp(buildViewJsp(result,actionName));
42+
return view;
43+
}
44+
45+
46+
47+
private static String readActionInConfig(String actionName) {
48+
StrutsUtil util = new StrutsUtil();
49+
return util.invokedAction(actionName);
50+
}
51+
52+
private static List<String> invokeMethod(String className, Map<String, String> params) {
53+
54+
List<String> rtnList = new ArrayList<String>();
55+
try {
56+
String name = params.get("name");
57+
String password = params.get("password");
58+
// Invoke set method
59+
Class<?> actionClass = Class.forName(className);
60+
Method setNameMethod = actionClass.getMethod("setName", String.class);
61+
Method setPasswordMethod = actionClass.getMethod("setPassword", String.class);
62+
Object action = actionClass.newInstance();
63+
setNameMethod.invoke(action, name);
64+
setPasswordMethod.invoke(action, password);
65+
// Invoke execute method and add to the return List as first element
66+
Method executeMethod = actionClass.getMethod("execute");
67+
rtnList.add(executeMethod.invoke(action).toString());
68+
// Invoke getMessage method and add to the return List as second element
69+
Method getMessageMethod = actionClass.getMethod("getMessage");
70+
rtnList.add(getMessageMethod.invoke(action).toString());
71+
72+
} catch (Exception e) {
73+
e.printStackTrace();
74+
}
75+
return rtnList;
76+
}
77+
78+
private static Map buildViewParams(String msg) {
79+
Map viewParams = new HashMap();
80+
viewParams.put("message", msg);
81+
return viewParams;
82+
}
83+
84+
private static String buildViewJsp(String result, String actionName) {
85+
StrutsUtil util = new StrutsUtil();
86+
return util.invokeResult(actionName,result);
87+
}
88+
89+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.coderising.litestruts.action;
2+
3+
/**
4+
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
5+
*
6+
* @author liuxin
7+
*
8+
*/
9+
public class LoginAction {
10+
private String name;
11+
private String password;
12+
private String message;
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public String getPassword() {
19+
return password;
20+
}
21+
22+
public String execute() {
23+
if ("test".equals(name) && "1234".equals(password)) {
24+
this.message = "login successful";
25+
return "success";
26+
}
27+
this.message = "login failed,please check your user/pwd";
28+
return "fail";
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public void setPassword(String password) {
36+
this.password = password;
37+
}
38+
39+
public String getMessage() {
40+
return this.message;
41+
}
42+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<struts>
3+
<action name="login" class="com.coderising.litestruts.action.LoginAction">
4+
<result name="success">/jsp/homepage.jsp</result>
5+
<result name="fail">/jsp/showLogin.jsp</result>
6+
</action>
7+
<action name="logout" class="com.coderising.litestruts.action.LogoutAction">
8+
<result name="success">/jsp/welcome.jsp</result>
9+
<result name="error">/jsp/error.jsp</result>
10+
</action>
11+
</struts>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.coderising.litestruts.test;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import com.coderising.litestruts.Struts;
10+
import com.coderising.litestruts.view.View;
11+
12+
13+
public class StrutsTest {
14+
15+
@Test
16+
public void testLoginActionSuccess() {
17+
18+
String actionName = "login";
19+
20+
Map<String,String> params = new HashMap<String,String>();
21+
params.put("name","test");
22+
params.put("password","1234");
23+
24+
25+
View view = Struts.runAction(actionName,params);
26+
27+
Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
28+
Assert.assertEquals("login successful", view.getParameters().get("message"));
29+
}
30+
31+
@Test
32+
public void testLoginActionFailed() {
33+
String actionName = "login";
34+
Map<String,String> params = new HashMap<String,String>();
35+
params.put("name","test");
36+
params.put("password","123456"); //ÃÜÂëºÍÔ¤ÉèµÄ²»Ò»ÖÂ
37+
38+
View view = Struts.runAction(actionName,params);
39+
40+
Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp());
41+
Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message"));
42+
}
43+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.coderising.litestruts.utils;
2+
3+
import java.io.File;
4+
import java.net.URL;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import javax.xml.parsers.DocumentBuilder;
9+
import javax.xml.parsers.DocumentBuilderFactory;
10+
11+
import org.w3c.dom.Document;
12+
import org.w3c.dom.Node;
13+
import org.w3c.dom.NodeList;
14+
15+
public class StrutsUtil {
16+
17+
public final static String CONFIG_PATH = "../struts.xml";
18+
19+
public final static String CONFIG_NODE_ACTION = "action";
20+
21+
public final static String CONFIG_ATTR_NAME = "name";
22+
23+
public final static String CONFIG_ATTR_CLASS = "class";
24+
25+
public String invokedAction(String actionName){
26+
if(null != actionName && !"".equals(actionName)){
27+
Document doc = generateDoc();
28+
NodeList nodeList = doc.getElementsByTagName(CONFIG_NODE_ACTION);
29+
for (int i = 0; i < nodeList.getLength(); i++) {
30+
String actionNameConfiged = nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue();
31+
if(actionName.equals(actionNameConfiged)){
32+
return nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_CLASS).getNodeValue();
33+
}
34+
}
35+
}
36+
throw new RuntimeException("actionName can't be found");
37+
}
38+
39+
private Document generateDoc(){
40+
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
41+
Document doc = null;
42+
try {
43+
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
44+
doc = docBuilder.parse (lookupConfigFile());
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
return doc;
49+
}
50+
51+
private File lookupConfigFile(){
52+
URL url = getClass().getResource(CONFIG_PATH);
53+
return new File(url.getPath());
54+
}
55+
56+
57+
public String invokeResult(String actionName, String result) {
58+
Document doc = generateDoc();
59+
NodeList nodeList = doc.getElementsByTagName("result");
60+
List<Node> subNodeList = new ArrayList<Node>();
61+
for (int i = 0; i < nodeList.getLength(); i++) {
62+
Node parentNode = nodeList.item(i).getParentNode();
63+
if(parentNode.getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue().equals(actionName)){
64+
subNodeList.add(nodeList.item(i));
65+
}
66+
}
67+
for (int i = 0; i < subNodeList.size(); i++) {
68+
Node node = subNodeList.get(i);
69+
if(node.getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue().equals(result)){
70+
return node.getTextContent();
71+
}
72+
}
73+
throw new RuntimeException("result can't be found");
74+
}
75+
76+
}

0 commit comments

Comments
 (0)