Skip to content

Commit

Permalink
Merge pull request onlyliuxin#19 from zhaohuXing/master
Browse files Browse the repository at this point in the history
出乎意料
  • Loading branch information
gqipan authored Mar 6, 2017
2 parents adf9b3a + 8bbcd7f commit c7cfc95
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
5 changes: 4 additions & 1 deletion group11/1178243325/DataStructure/src/main/java/com/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com;

import java.util.*;
import com.coderising.litestruts.*;
import com.coderising.array.*;
public class Main {
Expand Down Expand Up @@ -48,6 +49,8 @@ public static void main(String[] args) {
System.out.print("\njoin测试:");
int[] jArray = new int[]{2, 3, 8};
System.out.print(ArrayUtil.join(jArray, "-"));
Struts.test();
Map<String, String> map = new HashMap<>();
Struts.runAction("login", map);

}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.coderising.litestruts;

import java.util.Map;

import java.util.HashMap;
import java.lang.reflect.Method;
public class Struts {

public static View runAction(String actionName, Map<String,String> parameters) {

/*
0. 读取配置文件struts.xml*/
String targetClassName = XmlUtil.parseXML("struts.xml", actionName);

/*
1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)
据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是
Expand All @@ -27,6 +26,35 @@ public static View runAction(String actionName, Map<String,String> parameters) {
放到View对象的jsp字段中。
*/
try {
String targetClassName = XmlUtil.parseXML("struts.xml", actionName);
Class<?> targetClass = Class.forName(targetClassName);

Method setName = targetClass.getMethod("setName", String.class);
Method setPassword = targetClass.getMethod("setPassword", String.class);
Object object = targetClass.newInstance();

setName.invoke(object, parameters.get("name"));
setPassword.invoke(object, parameters.get("password"));

Method execute = targetClass.getMethod("execute");
String result = (String)execute.invoke(object);

Method getMessage = targetClass.getMethod("getMessage");
String message = (String)getMessage.invoke(object);

Map<String, String> params = new HashMap<String, String>();
params.put("message", message);
String jspUrl = XmlUtil.getJspUrl("struts.xml", actionName, result);
View view = new View();
view.setJsp(jspUrl);
view.setParameters(params);
return view;

} catch (Exception e) {
e.printStackTrace();
}


return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,30 @@ public static String parseXML(String filePath, String actionName) {
return null;
}

public static String getJspUrl(String filePath, String actionName, String resultName) {
try {
File file = new File(filePath);
SAXReader reader = new SAXReader();
Document doc = reader.read(file);
Element root = doc.getRootElement();
for (Iterator iter = root.elementIterator("action"); iter.hasNext();) {
Element element = (Element)iter.next();
Attribute nameAttr = element.attribute("name");
if (nameAttr.getValue().equals(actionName)) {
for (Iterator ite = element.elementIterator("result"); ite.hasNext();) {
Element ele = (Element)ite.next();
Attribute resultAttr = ele.attribute("name");
if (resultAttr.getValue().equals(resultName)) {
return ele.getText();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

}
11 changes: 11 additions & 0 deletions group11/1178243325/DataStructure/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<action name="login" class="com.coderising.action.LoginAction">
<result name="success">/jsp/homepage.jsp</result>
<result name="fail">/jsp/showLogin.jsp</result>
</action>
<action name="logout" class="com.coderising.action.LogoutAction">
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</struts>
4 changes: 2 additions & 2 deletions group11/1178243325/DataStructure/struts.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<action name="login" class="com.coderising.action.LoginAction">
<action name="login" class="com.coderising.litestruts.LoginAction">
<result name="success">/jsp/homepage.jsp</result>
<result name="fail">/jsp/showLogin.jsp</result>
</action>
<action name="logout" class="com.coderising.action.LogoutAction">
<action name="logout" class="com.coderising.litestruts.LogoutAction">
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
Expand Down

0 comments on commit c7cfc95

Please sign in to comment.