-
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.
- Loading branch information
Macx
authored and
Macx
committed
Apr 2, 2017
1 parent
4d4f2a5
commit bf736e4
Showing
7 changed files
with
310 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.litestruts; | ||
|
||
/** | ||
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class LoginAction{ | ||
private String name ; | ||
private String password; | ||
private String message; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public String execute(){ | ||
if("test".equals(name) && "1234".equals(password)){ | ||
this.message = "login successful"; | ||
return "success"; | ||
} | ||
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; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.litestruts; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NamedNodeMap; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
import com.litestruts.strutsBean.Action; | ||
|
||
|
||
|
||
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字段中。 | ||
*/ | ||
StrutsXmlReader strutsXml = new StrutsXmlReader(new File("src/com/litestruts/struts.xml")); | ||
View view = new View(); | ||
try { | ||
HashMap actMap = (HashMap) strutsXml.loadXml(); | ||
Action act = (Action) actMap.get(actionName); | ||
Class clazz = Class.forName(act.getClazz()); | ||
Object obj = clazz.newInstance(); | ||
HashMap paraMap = act.getParameters(); | ||
Iterator<Map.Entry<String, String>> iteraPara = parameters.entrySet().iterator(); | ||
|
||
while(iteraPara.hasNext()){ | ||
Entry<String, String> itera = iteraPara.next(); | ||
Field field = clazz.getDeclaredField(itera.getKey()); | ||
field.setAccessible(true); | ||
field.set(obj, itera.getValue()); | ||
} | ||
|
||
Method method = clazz.getMethod("execute", null); | ||
String results = (String)method.invoke(obj, null); | ||
Field[] getFields = clazz.getDeclaredFields(); | ||
HashMap getMapPara = new HashMap(); | ||
for (Field field : getFields) { | ||
field.setAccessible(true); | ||
String getFiledName = field.getName(); | ||
Object objValue = field.get(obj); | ||
getMapPara.put(getFiledName, objValue); | ||
} | ||
|
||
view.setParameters(getMapPara); | ||
view.setJsp((String)paraMap.get(results)); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return view; | ||
} | ||
|
||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.litestruts; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
|
||
|
||
|
||
|
||
public class StrutsTest { | ||
|
||
@Test | ||
public void testLoginActionSuccess() { | ||
|
||
String actionName = "login"; | ||
|
||
Map<String,String> params = new HashMap<String,String>(); | ||
params.put("name","test"); | ||
params.put("password","1234"); | ||
|
||
|
||
View view = Struts.runAction(actionName,params); | ||
|
||
Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); | ||
Assert.assertEquals("login successful", view.getParameters().get("message")); | ||
} | ||
|
||
@Test | ||
public void testLoginActionFailed() { | ||
String actionName = "login"; | ||
Map<String,String> params = new HashMap<String,String>(); | ||
|
||
View view = Struts.runAction(actionName,params); | ||
|
||
Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); | ||
Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.litestruts; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NamedNodeMap; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
import com.litestruts.strutsBean.Action; | ||
|
||
public class StrutsXmlReader { | ||
private File file; | ||
private HashMap actMap = new HashMap(); | ||
|
||
public StrutsXmlReader(File file) { | ||
super(); | ||
this.file = file; | ||
} | ||
|
||
|
||
public Map loadXml() throws ParserConfigurationException, SAXException, IOException { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
Document doc = builder.parse(file); | ||
NodeList nl = doc.getElementsByTagName("action"); | ||
for (int i = 0; i < nl.getLength(); i++) { | ||
Element book = (Element) nl.item(i); | ||
String name = book.getAttribute("name"); | ||
String claz = book.getAttribute("class"); | ||
Action act = new Action(name, claz); | ||
System.out.println(name); | ||
System.out.println(claz); | ||
NodeList bookNode = book.getChildNodes(); | ||
HashMap<String, String> paraMap = new HashMap(); | ||
for (int j = 0; j < bookNode.getLength(); j++) { | ||
Node bookCh = bookNode.item(j); | ||
if (bookCh.getNodeType() == Node.ELEMENT_NODE) { | ||
String valTag = bookCh.getTextContent(); | ||
NamedNodeMap attrs = bookCh.getAttributes(); | ||
String resultsValue = attrs.getNamedItem("name").getNodeValue(); | ||
paraMap.put(resultsValue, valTag); | ||
} | ||
act.setParameters(paraMap); | ||
} | ||
actMap.put(act.getName(), act); | ||
} | ||
return actMap; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.litestruts; | ||
|
||
import java.util.Map; | ||
|
||
public class View { | ||
private String jsp; | ||
private Map parameters; | ||
|
||
public String getJsp() { | ||
return jsp; | ||
} | ||
public View setJsp(String jsp) { | ||
this.jsp = jsp; | ||
return this; | ||
} | ||
public Map getParameters() { | ||
return parameters; | ||
} | ||
public View setParameters(Map parameters) { | ||
this.parameters = parameters; | ||
return this; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<struts> | ||
<action name="login" class="com.litestruts.LoginAction"> | ||
<result name="success">/jsp/homepage.jsp</result> | ||
<result name="fail">/jsp/showLogin.jsp</result> | ||
</action> | ||
<action name="logout" class="com.litestruts.LogoutAction"> | ||
<result name="success">/jsp/welcome.jsp</result> | ||
<result name="error">/jsp/error.jsp</result> | ||
</action> | ||
</struts> |
46 changes: 46 additions & 0 deletions
46
group26/723161901/src/com/litestruts/strutsBean/Action.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.litestruts.strutsBean; | ||
|
||
import java.util.HashMap; | ||
|
||
public class Action { | ||
private String name; | ||
private String clazz; | ||
private HashMap<String, String> parameters; | ||
|
||
public Action() { | ||
super(); | ||
} | ||
|
||
public Action(String name, String clazz) { | ||
super(); | ||
this.name = name; | ||
this.clazz = clazz; | ||
} | ||
|
||
public Action(String name, String clazz, HashMap<String, String> parameters) { | ||
super(); | ||
this.name = name; | ||
this.clazz = clazz; | ||
this.parameters = parameters; | ||
} | ||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
public String getClazz() { | ||
return clazz; | ||
} | ||
public void setClazz(String clazz) { | ||
this.clazz = clazz; | ||
} | ||
public HashMap<String, String> getParameters() { | ||
return parameters; | ||
} | ||
public void setParameters(HashMap<String, String> parameters) { | ||
this.parameters = parameters; | ||
} | ||
|
||
|
||
} |