Skip to content

Commit 6bfc756

Browse files
committed
完成解析struts.xml作业
1 parent e7357dd commit 6bfc756

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

group17/102228177/work2_26/src/com/coderising/litestruts/Dom4jParseXmlUtil.java renamed to group17/102228177/work2_26/src/com/coderising/litestruts/Dom4jUtil.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import java.util.List;
99
import java.util.Map;
1010

11-
import org.dom4j.Attribute;
1211
import org.dom4j.Document;
13-
import org.dom4j.DocumentException;
1412
import org.dom4j.Element;
1513
import org.dom4j.io.SAXReader;
1614

@@ -19,12 +17,12 @@
1917
* @author ren
2018
*
2119
*/
22-
public class Dom4jParseXmlUtil {
20+
public class Dom4jUtil {
2321

2422
/**
25-
* 获取根节点
26-
* @param path
27-
* @return Element
23+
* 传入文件路径解析xml文件获取根节点
24+
* @param path xml文件的绝对路径
25+
* @return Element 根节点
2826
*/
2927
public static Element parseXml(String path){
3028
InputStream is = null;
@@ -38,9 +36,12 @@ public static Element parseXml(String path){
3836
//获取根节点对象
3937
Element rootElement = document.getRootElement();
4038
return rootElement;
41-
} catch (FileNotFoundException | DocumentException e) {
39+
} catch (FileNotFoundException e) {
4240
e.printStackTrace();
43-
}finally{
41+
} catch (Exception e) {
42+
e.printStackTrace();
43+
}
44+
finally{
4445
if( is!=null ){
4546
try {
4647
is.close();
@@ -68,23 +69,33 @@ public static Map<String, String> getAttribute(Element element){
6869
return map;
6970
}
7071

71-
public static void getNode(Element element){
72+
/**
73+
* 根据传入的Action名返回结果JSP
74+
* @param element 根节点
75+
* @param actionName 标签name属性的value
76+
* @return map 封装返回结果jsp的map对象
77+
*/
78+
public static Map<String, String> getJspMap(Element element,String actionName){
79+
Map<String, String> map = new HashMap<String, String>();
7280
List<Element> actions = element.elements();
7381
for (Element action : actions) {
74-
List<Element> results = action.elements();
75-
for (Element result : results) {
76-
String name = result.attributeValue("name");
77-
String text = result.getText();
78-
System.out.println(name+":"+text);
82+
if(actionName.equals(action.attributeValue("name"))){
83+
List<Element> results = action.elements();
84+
for (Element result : results) {
85+
String name = result.attributeValue("name");
86+
String text = result.getText();
87+
map.put(name, text);
88+
}
7989
}
8090
}
91+
return map;
8192
}
8293

8394
public static void main(String[] args) {
84-
String path = Dom4jParseXmlUtil.class.getResource("").getPath()+"struts.xml";
95+
String path = Dom4jUtil.class.getResource("").getPath()+"struts.xml";
8596
System.out.println(path);
8697
Element element = parseXml(path);
8798
Map<String, String> attribute = getAttribute(element);
88-
getNode(element);
99+
System.out.println(getJspMap(element,"login").get("success"));
89100
}
90101
}

group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static View runAction(String actionName, Map<String,String> parameters) {
3333
3434
*/
3535
String path = Struts.class.getResource("").getPath()+"struts.xml";
36-
Element element = Dom4jParseXmlUtil.parseXml(path);
37-
Map<String, String> attribute = Dom4jParseXmlUtil.getAttribute(element);
36+
Element element = Dom4jUtil.parseXml(path);
37+
Map<String, String> attribute = Dom4jUtil.getAttribute(element);
3838
String className = attribute.get(actionName);
3939
try {
4040
Class clazz = Class.forName(className);
@@ -58,22 +58,22 @@ public static View runAction(String actionName, Map<String,String> parameters) {
5858
}
5959
View view = new View();
6060
view.setParameters(map);
61-
61+
Map<String, String> result = Dom4jUtil.getJspMap(element, actionName);
62+
view.setJsp(result.get(str));
63+
return view;
6264
} catch (ClassNotFoundException e) {
6365
e.printStackTrace();
6466
} catch (Exception e) {
6567
e.printStackTrace();
6668
}
67-
6869
return null;
6970
}
7071

7172
public static void main(String[] args) {
7273
String actionName = "login";
73-
7474
Map<String,String> params = new HashMap<String,String>();
7575
params.put("name","test");
7676
params.put("password","1234");
7777
runAction(actionName, params);
78-
}
78+
}
7979
}

group17/102228177/work2_26/src/com/coderising/litestruts/StrutsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public void testLoginActionSuccess() {
2424

2525
View view = Struts.runAction(actionName,params);
2626

27-
// Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
28-
// Assert.assertEquals("login successful", view.getParameters().get("message"));
27+
Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
28+
Assert.assertEquals("login successful", view.getParameters().get("message"));
2929
}
3030

31-
// @Test
31+
@Test
3232
public void testLoginActionFailed() {
3333
String actionName = "login";
3434
Map<String,String> params = new HashMap<String,String>();

0 commit comments

Comments
 (0)