Skip to content

Commit

Permalink
Merge pull request #30 from duxiaolong/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
gaodekui authored Mar 3, 2017
2 parents 78e0a66 + ab8ba87 commit 523b6a5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,21 @@ public String getName() {
return name;
}

public void setName(String name){
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password){
this.password = password;
}

public String getMessage(){
return this.message;
}

public String execute(){
if("test".equals(name) && "1234".equals(password)){
Expand All @@ -26,14 +38,4 @@ public String execute(){
this.message = "login failed,please check your user/pwd";
return "fail";
}

public void setName(String name){
this.name = name;
}
public void setPassword(String password){
this.password = password;
}
public String getMessage(){
return this.message;
}
}
77 changes: 50 additions & 27 deletions group16/1287642108/0305/src/com/coderising/litestruts/Struts.java
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;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.coderising.litestruts;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;





public class StrutsTest {

@Test
public void testLoginActionSuccess() {
public void testLoginActionSuccess() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

String actionName = "login";

Expand All @@ -29,7 +26,7 @@ public void testLoginActionSuccess() {
}

@Test
public void testLoginActionFailed() {
public void testLoginActionFailed() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
String actionName = "login";
Map<String,String> params = new HashMap<String,String>();
params.put("name","test");
Expand Down
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>

0 comments on commit 523b6a5

Please sign in to comment.