forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #16 from RishyPa/master
提交本次作业litestruts以及ArrayUtil类
- Loading branch information
Showing
8 changed files
with
565 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
39 changes: 39 additions & 0 deletions
39
group10/569420966/struct/src/main/java/com/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 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; | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
group10/569420966/struct/src/main/java/com/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,147 @@ | ||
package com.litestruts; | ||
|
||
import com.myutil.JavaBeanUtil; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.NamedNodeMap; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
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.rmi.server.ExportException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
public class Struts { | ||
|
||
private static Map<String, Map<String, String>> xmlInfo = new HashMap<>(); | ||
|
||
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字段中。 | ||
*/ | ||
|
||
// 【0】 | ||
getInfo(); | ||
|
||
|
||
Map<String, String> action = xmlInfo.get(actionName); | ||
Class<?> classForName; | ||
View view = new View(); | ||
try { | ||
// 【1】 | ||
Object aObj = Class.forName(action.get("class")).newInstance(); | ||
classForName = aObj.getClass(); | ||
Method[] methods = classForName.getMethods(); | ||
for (Method method : methods) { | ||
for (String key : parameters.keySet()) { | ||
if (JavaBeanUtil.getSetMethodName(key).equals(method.getName())) { | ||
Method declaredMethod = classForName.getDeclaredMethod(method.getName(), method.getParameterTypes()); | ||
declaredMethod.invoke(aObj, parameters.get(key)); | ||
} | ||
} | ||
} | ||
// 【2】 | ||
Map<String, String> resultMap = new HashMap<>(); | ||
Method execute = classForName.getDeclaredMethod("execute"); | ||
String invoke = (String) execute.invoke(aObj); | ||
// 【3】 | ||
Field[] fields = classForName.getDeclaredFields(); | ||
for (Field field : fields) { | ||
for (Method method : methods) { | ||
if (JavaBeanUtil.getGetMethodName(field.getName()).equals(method.getName())) { | ||
Method declaredMethod = classForName.getDeclaredMethod(method.getName()); | ||
String invoke1 = (String)declaredMethod.invoke(aObj); | ||
resultMap.put(field.getName(), invoke1); | ||
} | ||
} | ||
} | ||
view.setParameters(resultMap); | ||
// 【4】 | ||
view.setJsp(action.get(invoke)); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} catch (InstantiationException e) { | ||
e.printStackTrace(); | ||
} catch (NoSuchMethodException e) { | ||
e.printStackTrace(); | ||
} catch (InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return view; | ||
} | ||
|
||
private static void getInfo() { | ||
try { | ||
File file = new File("D:\\Own\\Code\\Java\\coding2017\\group10\\569420966\\struct\\src\\main\\resources\\struts.xml"); | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
Document doc = builder.parse(file); | ||
NodeList nl = doc.getElementsByTagName("struts"); | ||
|
||
if (nl.getLength() > 0) { | ||
NodeList actions = doc.getElementsByTagName("action"); | ||
if (actions.getLength() > 0) { | ||
Map<String, String> actionInfo; | ||
for (int i = 0; i < actions.getLength(); i++) { | ||
actionInfo = new HashMap<>(); | ||
NamedNodeMap attributes = actions.item(i).getAttributes(); | ||
for (int j = 0; j < attributes.getLength(); j++) { | ||
attributes.item(j).getNodeName(); | ||
actionInfo.put(attributes.item(j).getNodeName(), attributes.item(j).getNodeValue()); | ||
} | ||
NodeList childNodes = actions.item(i).getChildNodes(); | ||
for (int j = 0; j < childNodes.getLength(); j++) { | ||
if (childNodes.item(j).getNodeType() == 1) { // 元素节点 | ||
NodeList childNodes1 = childNodes.item(j).getChildNodes(); | ||
NamedNodeMap attributes1 = childNodes.item(j).getAttributes(); | ||
NodeList childNodes2 = childNodes.item(j).getChildNodes(); | ||
actionInfo.put(attributes1.item(0).getNodeValue(), childNodes2.item(0).getNodeValue()); | ||
} | ||
} | ||
if (actionInfo.get("name") != null) { | ||
xmlInfo.put(actionInfo.get("name"), actionInfo); | ||
} | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
getInfo(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
group10/569420966/struct/src/main/java/com/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 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; | ||
} | ||
} |
Oops, something went wrong.