Skip to content

Commit

Permalink
完成解析struts.xml作业
Browse files Browse the repository at this point in the history
  • Loading branch information
Ren650119726 committed Feb 27, 2017
1 parent e7357dd commit 6bfc756
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

Expand All @@ -19,12 +17,12 @@
* @author ren
*
*/
public class Dom4jParseXmlUtil {
public class Dom4jUtil {

/**
* 获取根节点
* @param path
* @return Element
* 传入文件路径解析xml文件获取根节点
* @param path xml文件的绝对路径
* @return Element 根节点
*/
public static Element parseXml(String path){
InputStream is = null;
Expand All @@ -38,9 +36,12 @@ public static Element parseXml(String path){
//获取根节点对象
Element rootElement = document.getRootElement();
return rootElement;
} catch (FileNotFoundException | DocumentException e) {
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
} catch (Exception e) {
e.printStackTrace();
}
finally{
if( is!=null ){
try {
is.close();
Expand Down Expand Up @@ -68,23 +69,33 @@ public static Map<String, String> getAttribute(Element element){
return map;
}

public static void getNode(Element element){
/**
* 根据传入的Action名返回结果JSP
* @param element 根节点
* @param actionName 标签name属性的value
* @return map 封装返回结果jsp的map对象
*/
public static Map<String, String> getJspMap(Element element,String actionName){
Map<String, String> map = new HashMap<String, String>();
List<Element> actions = element.elements();
for (Element action : actions) {
List<Element> results = action.elements();
for (Element result : results) {
String name = result.attributeValue("name");
String text = result.getText();
System.out.println(name+":"+text);
if(actionName.equals(action.attributeValue("name"))){
List<Element> results = action.elements();
for (Element result : results) {
String name = result.attributeValue("name");
String text = result.getText();
map.put(name, text);
}
}
}
return map;
}

public static void main(String[] args) {
String path = Dom4jParseXmlUtil.class.getResource("").getPath()+"struts.xml";
String path = Dom4jUtil.class.getResource("").getPath()+"struts.xml";
System.out.println(path);
Element element = parseXml(path);
Map<String, String> attribute = getAttribute(element);
getNode(element);
System.out.println(getJspMap(element,"login").get("success"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static View runAction(String actionName, Map<String,String> parameters) {
*/
String path = Struts.class.getResource("").getPath()+"struts.xml";
Element element = Dom4jParseXmlUtil.parseXml(path);
Map<String, String> attribute = Dom4jParseXmlUtil.getAttribute(element);
Element element = Dom4jUtil.parseXml(path);
Map<String, String> attribute = Dom4jUtil.getAttribute(element);
String className = attribute.get(actionName);
try {
Class clazz = Class.forName(className);
Expand All @@ -58,22 +58,22 @@ public static View runAction(String actionName, Map<String,String> parameters) {
}
View view = new View();
view.setParameters(map);

Map<String, String> result = Dom4jUtil.getJspMap(element, actionName);
view.setJsp(result.get(str));
return view;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

public static void main(String[] args) {
String actionName = "login";

Map<String,String> params = new HashMap<String,String>();
params.put("name","test");
params.put("password","1234");
runAction(actionName, params);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public void testLoginActionSuccess() {

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

// Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
// Assert.assertEquals("login successful", view.getParameters().get("message"));
Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
Assert.assertEquals("login successful", view.getParameters().get("message"));
}

// @Test
@Test
public void testLoginActionFailed() {
String actionName = "login";
Map<String,String> params = new HashMap<String,String>();
Expand Down

0 comments on commit 6bfc756

Please sign in to comment.