-
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 #34 from hilihan/master
0305作业提交
- Loading branch information
Showing
8 changed files
with
251 additions
and
4 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
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
39 changes: 39 additions & 0 deletions
39
group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.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,39 @@ | ||
package cn.mark.work0226.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; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
group16/502059278/src/cn/mark/work0226/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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cn.mark.work0226.litestruts; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.dom4j.Attribute; | ||
import org.dom4j.Document; | ||
import org.dom4j.Element; | ||
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字段中。 | ||
*/ | ||
String classPath = null; | ||
String className = null; | ||
|
||
Document dom = XMLUtils.getDocument("bin"+File.separator+"struts.xml"); | ||
Element ele = XMLUtils.getElement(dom, actionName); | ||
Attribute classAttr = ele.attribute("class"); | ||
classPath = classAttr.getValue(); | ||
className = classPath.substring(classPath.lastIndexOf(".")+1); | ||
System.out.println(className); | ||
|
||
|
||
|
||
try { | ||
Class clz = Class.forName(classPath); | ||
System.out.println(clz.getName()); | ||
|
||
|
||
|
||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (SecurityException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} catch (IllegalArgumentException e) { | ||
// TODO Auto-generated catch block | ||
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"); | ||
|
||
|
||
Struts.runAction(actionName,params); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.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,43 @@ | ||
package cn.mark.work0226.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>(); | ||
params.put("name","test"); | ||
params.put("password","123456"); //密码和预设的不一致 | ||
|
||
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")); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
group16/502059278/src/cn/mark/work0226/litestruts/View.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,23 @@ | ||
package cn.mark.work0226.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; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.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,49 @@ | ||
package cn.mark.work0226.litestruts; | ||
|
||
import java.util.List; | ||
|
||
import org.dom4j.Attribute; | ||
import org.dom4j.Document; | ||
import org.dom4j.DocumentException; | ||
import org.dom4j.Element; | ||
import org.dom4j.io.SAXReader; | ||
|
||
public class XMLUtils { | ||
/** | ||
* 获取Document | ||
* @param filePath 配置文件路径名 | ||
* @return Document对象 | ||
*/ | ||
public static Document getDocument(String filePath){ | ||
//1.创建解析器 | ||
SAXReader reader = new SAXReader(); | ||
//2.解析XML文档,返回document对象 | ||
Document dom = null; | ||
try { | ||
dom = reader.read(filePath); | ||
} catch (DocumentException e) { | ||
e.printStackTrace(); | ||
} | ||
return dom; | ||
} | ||
/** | ||
* 获取指定action元素 | ||
* @param doc Document | ||
* @param actionName 要获取的元素属性名 | ||
* @return 包含所要属性的元素 | ||
*/ | ||
public static Element getElement(Document doc , String actionName){ | ||
Element result = null; | ||
Element root = doc.getRootElement(); | ||
List<Element> elements = root.elements(); | ||
for(Element e : elements){ | ||
Attribute attr = e.attribute("name"); | ||
if(attr.getValue().equals(actionName)){ | ||
result = e; | ||
return result; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
} |
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="cn.mark.work0226.litestruts.LoginAction"> | ||
<result name="success">/jsp/homepage.jsp</result> | ||
<result name="fail">/jsp/showLogin.jsp</result> | ||
</action> | ||
<action name="logout" class="cn.mark.work0226.litestruts.LogoutAction"> | ||
<result name= "success">/jsp/welcome.jsp</result> | ||
<result name= "error">/jsp/error.jsp</result> | ||
</action> | ||
</struts> |