-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from duxiaolong/master
Master
- Loading branch information
Showing
4 changed files
with
69 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 50 additions & 27 deletions
77
group16/1287642108/0305/src/com/coderising/litestruts/Struts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,57 @@ | ||
package com.coderising.litestruts; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
import org.dom4j.Document; | ||
import org.dom4j.DocumentException; | ||
import org.dom4j.Element; | ||
import org.dom4j.Node; | ||
import org.dom4j.io.SAXReader; | ||
|
||
public class Struts { | ||
|
||
public static View runAction(String actionName, Map<String,String> parameters) { | ||
|
||
/* | ||
0. 读取配置文件struts.xml | ||
1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) | ||
据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 | ||
("name"="test" , "password"="1234") , | ||
那就应该调用 setName和setPassword方法 | ||
2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" | ||
3. 通过反射找到对象的所有getter方法(例如 getMessage), | ||
通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , | ||
放到View对象的parameters | ||
4. 根据struts.xml中的 <result> 配置,以及execute的返回值, 确定哪一个jsp, | ||
放到View对象的jsp字段中。 | ||
*/ | ||
|
||
public static View runAction(String actionName, Map<String,String> parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { | ||
//创建SAXReader读取器,专门用于读取xml | ||
SAXReader saxReader = new SAXReader(); | ||
Document document = null; | ||
try { | ||
document = saxReader.read(new File("D:/DemoSpace/coding2017/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml")); | ||
Element root = document.getRootElement(); | ||
//根据节点名称找节点 | ||
Node node = root.selectSingleNode("action[@name='"+actionName+"']"); | ||
String classPath = ((Element) node).attributeValue("class"); | ||
//根据类名反射实例化 | ||
Class<?> onwClass = Class.forName(classPath); | ||
Object o = onwClass.newInstance(); | ||
Method setName = onwClass.getMethod("setName",String.class); | ||
Method setPassword = onwClass.getMethod("setPassword",String.class); | ||
Method execute = onwClass.getMethod("execute"); | ||
Method getName = onwClass.getMethod("getName"); | ||
Method getPassword = onwClass.getMethod("getPassword"); | ||
Method getMessage = onwClass.getMethod("getMessage"); | ||
setName.invoke(o,parameters.get("name")); | ||
setPassword.invoke(o,parameters.get("password")); | ||
String result = (String) execute.invoke(o); | ||
//组装params参数 | ||
HashMap<String,String> map = new HashMap<>(); | ||
String name = (String) getName.invoke(o); | ||
String password = (String) getPassword.invoke(o); | ||
String message = (String) getMessage.invoke(o); | ||
map.put("name", name); | ||
map.put("password", password); | ||
map.put("message", message); | ||
//组装view数据 | ||
View view = new View(); | ||
view.setParameters(map); | ||
//根据execute的返回值,找对应的jsp页面路径 | ||
String jspPath = node.valueOf("//result[@name='"+result+"']"); | ||
view.setJsp(jspPath); | ||
return view; | ||
} catch (DocumentException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
group16/1287642108/0305/src/com/coderising/litestruts/struts.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<?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"> | ||
<result = "success">/jsp/welcome.jsp</result> | ||
<result = "error">/jsp/error.jsp</result> | ||
<result name="success">/jsp/welcome.jsp</result> | ||
<result name="error">/jsp/error.jsp</result> | ||
</action> | ||
</struts> | ||
</struts> |