Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions group24/330657387/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/coding2017"/>
<classpathentry kind="output" path="bin"/>
</classpath>
21 changes: 21 additions & 0 deletions group24/330657387/src/main/week02/litestruts/CustomException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main.week02.litestruts;

import java.io.IOException;

import org.jdom2.JDOMException;

public class CustomException extends RuntimeException {

public CustomException(IOException e) {
super(e.getMessage());
}

public CustomException(JDOMException e) {
super(e.getMessage());
}

public CustomException(String msg) {
super(msg);
}

}
39 changes: 39 additions & 0 deletions group24/330657387/src/main/week02/litestruts/LoginAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main.week02.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;
}
}
79 changes: 79 additions & 0 deletions group24/330657387/src/main/week02/litestruts/Struts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main.week02.litestruts;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Struts {

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

/*
*
* 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字段中。
*/

XmlSaxUtil config = new XmlSaxUtil("struts.xml");
String classname = config.getClassName(actionName);
// 1 通过反射存入parameter
// 没有这个类名就报错
if (null == classname) {
throw new CustomException("没有这个action啊!");
}
// 通过反射获取实例
Class<?> controllerClass = Class.forName(classname);
// 创建实例
Object controller = controllerClass.newInstance();
// 获取类中声明的全部成员变量
Field[] fields = controllerClass.getDeclaredFields();
// 备用
Method m;
// 得到该action所有result的结果合集

// 为controller注入变量
for (String key : parameters.keySet()) {
for (Field f : fields) {
if (f.getName() == key) {
m = controllerClass.getMethod("set"
+ key.replace(key.substring(0, 1),key.substring(0, 1).toUpperCase()), String.class);
m.invoke(controller, parameters.get(key));
break;
}
}
}

// 2 通过反射调用excute 获取返回值
m = controllerClass.getMethod("execute");
String result = (String) m.invoke(controller);

// 3 把message放到View对象的parameters
View view = new View();
Map<String, String> viewParam = new HashMap<String, String>();

// 新建并传入View的viewParam属性值
m = controllerClass.getMethod("getMessage");
viewParam.put("message", (String) m.invoke(controller));
view.setParameters(viewParam);

// 传入jsp路径
view.setJsp(config.getResultView(actionName, result));

return view;
}

}
39 changes: 39 additions & 0 deletions group24/330657387/src/main/week02/litestruts/StrutsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main.week02.litestruts;

import java.util.HashMap;
import java.util.Map;

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

public class StrutsTest {

@Test
public void testLoginActionSuccess() throws Exception {

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() throws Exception {
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 group24/330657387/src/main/week02/litestruts/View.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main.week02.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;
}
}
24 changes: 24 additions & 0 deletions group24/330657387/src/main/week02/litestruts/XmlDomUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main.week02.litestruts;

public class XmlDomUtil {
/*// 0 读取xml文档的元素值
// 获取工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 产生解析器
DocumentBuilder builder = factory.newDocumentBuilder();
// 解析xml文档,得到代表文档的document对象
Document document = builder.parse(new File(
"./src/main/week02/litestruts/struts.xml"));
// 根据标签名获取节点列表
NodeList actionList = document.getElementsByTagName("action");*/

/* HashMap<String, String> actionMap = new HashMap<String, String>();*/

/*for (int i = 0; i < actionList.getLength(); i++) {
// 获取节点列表里的节点
Element action = (Element) actionList.item(i);
// 获取节点的属性值
actionMap.put(action.getAttribute("name"),
action.getAttribute("class"));
}*/
}
115 changes: 115 additions & 0 deletions group24/330657387/src/main/week02/litestruts/XmlSaxUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package main.week02.litestruts;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class XmlSaxUtil {

Map<String, ActionConfig> actions = new HashMap<>();

public XmlSaxUtil(String fileName) {
// 得到包名
String packageName = this.getClass().getPackage().getName();
// 把包名转化成路径的一部分
packageName = packageName.replace('.', '/');
// 基于流的sax方式解析,需要先给出流,从类的同地址下找文件。
InputStream is = this.getClass().getResourceAsStream(
"/" + packageName + "/" + fileName);

parseXML(is);

try {
is.close();
} catch (IOException e) {
throw new CustomException(e);
}
}

private void parseXML(InputStream is) {
// 解析器工厂
SAXBuilder builder = new SAXBuilder();

try {
// 获取xml文件对象
Document doc = builder.build(is);
// 根节点
Element root = doc.getRootElement();

for (Element actionElement : root.getChildren("action")) {

String actionName = actionElement.getAttributeValue("name");
String clzName = actionElement.getAttributeValue("class");

ActionConfig ac = new ActionConfig(actionName, clzName);

for (Element resultElement : actionElement
.getChildren("result")) {

String resultName = resultElement.getAttributeValue("name");
String viewName = resultElement.getText().trim();

ac.addViewResult(resultName, viewName);

}

this.actions.put(actionName, ac);
}

} catch (JDOMException e) {
throw new CustomException(e);

} catch (IOException e) {
throw new CustomException(e);

}

}

public String getClassName(String action) {
ActionConfig ac = this.actions.get(action);
if (ac == null) {
return null;
}
return ac.getClassName();
}

public String getResultView(String action, String resultName) {
ActionConfig ac = this.actions.get(action);
if (ac == null) {
return null;
}
return ac.getViewName(resultName);
}

private static class ActionConfig {

String name;
String clzName;
Map<String, String> viewResult = new HashMap<>();

public ActionConfig(String actionName, String clzName) {
this.name = actionName;
this.clzName = clzName;
}

public String getClassName() {
return clzName;
}

public void addViewResult(String name, String viewName) {
viewResult.put(name, viewName);
}

public String getViewName(String resultName) {
return viewResult.get(resultName);
}
}

}
11 changes: 11 additions & 0 deletions group24/330657387/src/main/week02/litestruts/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="main.week02.litestruts.LoginAction">
<result name="success">/jsp/homepage.jsp</result>
<result name="fail">/jsp/showLogin.jsp</result>
</action>
<action name="logout" class="main.week02.litestruts.LogoutAction">
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</struts>
Loading