Skip to content

Commit 54b3e1d

Browse files
committed
Mar 5th Homework
1 parent baf1e68 commit 54b3e1d

File tree

6 files changed

+289
-0
lines changed

6 files changed

+289
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
Node classNode = readActionInConfig(actionName);
37+
List<String> rtnList = invokeMethod(classNode.getNodeValue(), params);
38+
String result = rtnList.get(0);
39+
String msg = rtnList.get(1);
40+
view.setParameters(buildViewParams(msg));
41+
view.setJsp(buildViewJsp(result,classNode));
42+
43+
System.out.println(rtnList);
44+
45+
return view;
46+
}
47+
48+
49+
50+
private static Node readActionInConfig(String actionName) {
51+
StrutsUtil util = new StrutsUtil();
52+
return util.invokedAction(actionName);
53+
}
54+
55+
private static List<String> invokeMethod(String className, Map<String, String> params) {
56+
57+
List<String> rtnList = new ArrayList<String>();
58+
try {
59+
String name = params.get("name");
60+
String password = params.get("password");
61+
// Invoke set method
62+
Class<?> actionClass = Class.forName(className);
63+
Method setNameMethod = actionClass.getMethod("setName", String.class);
64+
Method setPasswordMethod = actionClass.getMethod("setPassword", String.class);
65+
Object action = actionClass.newInstance();
66+
setNameMethod.invoke(action, name);
67+
setPasswordMethod.invoke(action, password);
68+
// Invoke execute method and add to the return List as first element
69+
Method executeMethod = actionClass.getMethod("execute");
70+
rtnList.add(executeMethod.invoke(action).toString());
71+
// Invoke getMessage method and add to the return List as second element
72+
Method getMessageMethod = actionClass.getMethod("getMessage");
73+
rtnList.add(getMessageMethod.invoke(action).toString());
74+
75+
} catch (Exception e) {
76+
e.printStackTrace();
77+
}
78+
return rtnList;
79+
}
80+
81+
private static Map buildViewParams(String msg) {
82+
Map viewParams = new HashMap();
83+
viewParams.put("message", msg);
84+
return viewParams;
85+
}
86+
87+
88+
private static String buildViewJsp(String result, Node classNode) {
89+
90+
91+
for (int i = 0; i < classNode.getChildNodes().getLength(); i++) {
92+
if(result.equals(classNode.getChildNodes().item(i).getNodeName())){
93+
return classNode.getChildNodes().item(i).getNodeValue();
94+
}
95+
}
96+
throw new RuntimeException("result Jsp can't be found");
97+
}
98+
99+
}
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
14+
15+
16+
public class StrutsTest {
17+
18+
@Test
19+
public void testLoginActionSuccess() {
20+
21+
String actionName = "login";
22+
23+
Map<String,String> params = new HashMap<String,String>();
24+
params.put("name","test");
25+
params.put("password","1234");
26+
27+
28+
View view = Struts.runAction(actionName,params);
29+
30+
Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
31+
Assert.assertEquals("login successful", view.getParameters().get("message"));
32+
}
33+
34+
@Test
35+
public void testLoginActionFailed() {
36+
String actionName = "login";
37+
Map<String,String> params = new HashMap<String,String>();
38+
params.put("name","test");
39+
params.put("password","123456"); //ÃÜÂëºÍÔ¤ÉèµÄ²»Ò»ÖÂ
40+
41+
View view = Struts.runAction(actionName,params);
42+
43+
Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp());
44+
Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message"));
45+
}
46+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.coderising.litestruts.utils;
2+
3+
import java.io.File;
4+
import java.net.URL;
5+
6+
import javax.xml.parsers.DocumentBuilder;
7+
import javax.xml.parsers.DocumentBuilderFactory;
8+
9+
import org.w3c.dom.Document;
10+
import org.w3c.dom.Node;
11+
import org.w3c.dom.NodeList;
12+
13+
public class StrutsUtil {
14+
15+
public final static String CONFIG_PATH = "../struts.xml";
16+
17+
public final static String CONFIG_NODE_ACTION = "action";
18+
19+
public final static String CONFIG_ATTR_NAME = "name";
20+
21+
public final static String CONFIG_ATTR_CLASS = "class";
22+
23+
public Node invokedAction(String actionName){
24+
if(null != actionName && !"".equals(actionName)){
25+
Document doc = generateDoc();
26+
NodeList nodeList = doc.getElementsByTagName(CONFIG_NODE_ACTION);
27+
for (int i = 0; i < nodeList.getLength(); i++) {
28+
String actionNameConfiged = nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue();
29+
if(actionName.equals(actionNameConfiged)){
30+
return nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_CLASS);
31+
}
32+
}
33+
}
34+
throw new RuntimeException("actionName can't be found");
35+
}
36+
37+
38+
public void readXML(){
39+
Document doc = generateDoc();
40+
NodeList nodeList = doc.getElementsByTagName("result");
41+
System.out.println(nodeList.item(0).getParentNode().getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue());
42+
System.out.println(nodeList.item(0).getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue());
43+
System.out.println(nodeList.item(0).getTextContent());
44+
System.out.println("Read XML Finished");
45+
}
46+
47+
private Document generateDoc(){
48+
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
49+
Document doc = null;
50+
try {
51+
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
52+
doc = docBuilder.parse (lookupConfigFile());
53+
} catch (Exception e) {
54+
e.printStackTrace();
55+
}
56+
return doc;
57+
}
58+
59+
private File lookupConfigFile(){
60+
URL url = getClass().getResource(CONFIG_PATH);
61+
return new File(url.getPath());
62+
}
63+
64+
public static void main(String[] args) {
65+
new StrutsUtil().readXML();
66+
}
67+
68+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coderising.litestruts.view;
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+
}

0 commit comments

Comments
 (0)