Skip to content

Commit 4ced179

Browse files
committed
Merge branch 'dev'
2 parents 88390c3 + 3325338 commit 4ced179

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

group20/404130810/src/com/coderising/litestruts/Struts.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,18 @@ public static View runAction(String actionName, Map<String, String> params) {
3333
*
3434
*/
3535
View view = new View();
36-
Node classNode = readActionInConfig(actionName);
37-
List<String> rtnList = invokeMethod(classNode.getNodeValue(), params);
36+
String className = readActionInConfig(actionName);
37+
List<String> rtnList = invokeMethod(className, params);
3838
String result = rtnList.get(0);
3939
String msg = rtnList.get(1);
4040
view.setParameters(buildViewParams(msg));
41-
view.setJsp(buildViewJsp(result,classNode));
42-
43-
System.out.println(rtnList);
44-
41+
view.setJsp(buildViewJsp(result,actionName));
4542
return view;
4643
}
4744

4845

4946

50-
private static Node readActionInConfig(String actionName) {
47+
private static String readActionInConfig(String actionName) {
5148
StrutsUtil util = new StrutsUtil();
5249
return util.invokedAction(actionName);
5350
}
@@ -84,16 +81,9 @@ private static Map buildViewParams(String msg) {
8481
return viewParams;
8582
}
8683

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");
84+
private static String buildViewJsp(String result, String actionName) {
85+
StrutsUtil util = new StrutsUtil();
86+
return util.invokeResult(actionName,result);
9787
}
9888

9989
}

group20/404130810/src/com/coderising/litestruts/test/StrutsTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
import com.coderising.litestruts.view.View;
1111

1212

13-
14-
15-
1613
public class StrutsTest {
1714

1815
@Test

group20/404130810/src/com/coderising/litestruts/utils/StrutsUtil.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.File;
44
import java.net.URL;
5+
import java.util.ArrayList;
6+
import java.util.List;
57

68
import javax.xml.parsers.DocumentBuilder;
79
import javax.xml.parsers.DocumentBuilderFactory;
@@ -20,30 +22,20 @@ public class StrutsUtil {
2022

2123
public final static String CONFIG_ATTR_CLASS = "class";
2224

23-
public Node invokedAction(String actionName){
25+
public String invokedAction(String actionName){
2426
if(null != actionName && !"".equals(actionName)){
2527
Document doc = generateDoc();
2628
NodeList nodeList = doc.getElementsByTagName(CONFIG_NODE_ACTION);
2729
for (int i = 0; i < nodeList.getLength(); i++) {
2830
String actionNameConfiged = nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue();
2931
if(actionName.equals(actionNameConfiged)){
30-
return nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_CLASS);
32+
return nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_CLASS).getNodeValue();
3133
}
3234
}
3335
}
3436
throw new RuntimeException("actionName can't be found");
3537
}
3638

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-
4739
private Document generateDoc(){
4840
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
4941
Document doc = null;
@@ -60,9 +52,25 @@ private File lookupConfigFile(){
6052
URL url = getClass().getResource(CONFIG_PATH);
6153
return new File(url.getPath());
6254
}
63-
64-
public static void main(String[] args) {
65-
new StrutsUtil().readXML();
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");
6674
}
6775

6876
}

0 commit comments

Comments
 (0)