Skip to content

Commit 6c540cb

Browse files
committed
2月26日作业
1 parent 088a0bd commit 6c540cb

File tree

7 files changed

+253
-6
lines changed

7 files changed

+253
-6
lines changed

liuxin/.classpath

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5-
<classpathentry kind="output" path="bin"/>
6-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.coderising.array;
2+
3+
public class ArrayUtil {
4+
5+
/**
6+
* 给定一个整形数组a , 对该数组的值进行置换
7+
例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7]
8+
如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
9+
* @param origin
10+
* @return
11+
*/
12+
public void reverseArray(int[] origin){
13+
14+
}
15+
16+
/**
17+
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
18+
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
19+
* {1,3,4,5,6,6,5,4,7,6,7,5}
20+
* @param oldArray
21+
* @return
22+
*/
23+
24+
public int[] removeZero(int[] oldArray){
25+
return null;
26+
}
27+
28+
/**
29+
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的
30+
* 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
31+
* @param array1
32+
* @param array2
33+
* @return
34+
*/
35+
36+
public int[] merge(int[] array1, int[] array2){
37+
return null;
38+
}
39+
/**
40+
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
41+
* 注意,老数组的元素在新数组中需要保持
42+
* 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
43+
* [2,3,6,0,0,0]
44+
* @param oldArray
45+
* @param size
46+
* @return
47+
*/
48+
public int[] grow(int [] oldArray, int size){
49+
return null;
50+
}
51+
52+
/**
53+
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列
54+
* 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13]
55+
* max = 1, 则返回空数组 []
56+
* @param max
57+
* @return
58+
*/
59+
public int[] fibonacci(int max){
60+
return null;
61+
}
62+
63+
/**
64+
* 返回小于给定最大值max的所有素数数组
65+
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
66+
* @param max
67+
* @return
68+
*/
69+
public int[] getPrimes(int max){
70+
return null;
71+
}
72+
73+
/**
74+
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
75+
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
76+
* @param max
77+
* @return
78+
*/
79+
public int[] getPerfectNumbers(int max){
80+
return null;
81+
}
82+
83+
/**
84+
* 用seperator 把数组 array给连接起来
85+
* 例如array= [3,8,9], seperator = "-"
86+
* 则返回值为"3-8-9"
87+
* @param array
88+
* @param s
89+
* @return
90+
*/
91+
public String join(int[] array, String seperator){
92+
return null;
93+
}
94+
95+
96+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.coderising.litestruts;
2+
3+
/**
4+
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
5+
* @author liuxin
6+
*
7+
*/
8+
public class LoginAction{
9+
private String name ;
10+
private String password;
11+
private String message;
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public String getPassword() {
18+
return password;
19+
}
20+
21+
public String execute(){
22+
if("test".equals(name) && "1234".equals(password)){
23+
this.message = "login successful";
24+
return "success";
25+
}
26+
this.message = "login failed,please check your user/pwd";
27+
return "fail";
28+
}
29+
30+
public void setName(String name){
31+
this.name = name;
32+
}
33+
public void setPassword(String password){
34+
this.password = password;
35+
}
36+
public String getMessage(){
37+
return this.message;
38+
}
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.coderising.litestruts;
2+
3+
import java.util.Map;
4+
5+
6+
7+
public class Struts {
8+
9+
public static View runAction(String actionName, Map<String,String> parameters) {
10+
11+
/*
12+
13+
0. 读取配置文件struts.xml
14+
15+
1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)
16+
据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是
17+
("name"="test" , "password"="1234") ,
18+
那就应该调用 setName和setPassword方法
19+
20+
2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"
21+
22+
3. 通过反射找到对象的所有getter方法(例如 getMessage),
23+
通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} ,
24+
放到View对象的parameters
25+
26+
4. 根据struts.xml中的 <result> 配置,以及execute的返回值, 确定哪一个jsp,
27+
放到View对象的jsp字段中。
28+
29+
*/
30+
31+
return null;
32+
}
33+
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.coderising.litestruts;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
10+
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coderising.litestruts;
2+
3+
import java.util.Map;
4+
5+
public class View {
6+
private String jsp;
7+
private Map parameters;
8+
9+
public String getJsp() {
10+
return jsp;
11+
}
12+
public View setJsp(String jsp) {
13+
this.jsp = jsp;
14+
return this;
15+
}
16+
public Map getParameters() {
17+
return parameters;
18+
}
19+
public View setParameters(Map parameters) {
20+
this.parameters = parameters;
21+
return this;
22+
}
23+
}
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.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.action.LogoutAction">
8+
<result = "success">/jsp/welcome.jsp</result>
9+
<result = "error">/jsp/error.jsp</result>
10+
</action>
11+
</struts>

0 commit comments

Comments
 (0)