//使用dom4j.jar
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字段中。
一、涉及内容:
反射,XML
二、写后感:
顺便了解下struts,通过XML创建Action,更能理解反射作用,体现Java动态性。由此想到之前学过的SSH,也能猜想大概底层创建对象,加深理解。
写的时候还是觉得有点乱,最后封装和函数命名都乱了,(风中凌乱。。。/(ㄒoㄒ)/~~)写框架也是慢慢积累的。
三、上代码
//Struts.java
public class Struts {
public static View runAction(String actionName, Map<String,String> parameters) {
Element rootElement = null;
try {
/**
* 0.读取配置文件
*/
rootElement = readStrutsXml().getRootElement();
} catch (DocumentException e) {
e.printStackTrace();
}
/**
* 1.根据actionName找到class
* 并设置
*/
String classPath = findClass(actionName, rootElement);
return handle(classPath, parameters, rootElement);
}
private static Document readStrutsXml() throws DocumentException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(
new File("路径" +
"\\litestruts\\struts.xml"));
return document;
}
private static String findClass(String actionName, Element root) {
String classPath = null;
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element action = (Element) i.next();
if (actionName.equals(action.attribute("name").getText())) {
classPath = action.attribute("class").getText();
break;
}
}
return classPath;
}
private static View handle(String classPath, Map<String, String> parameters
, Element rootElement) {
View view = new View();
Class newClass = getClass(classPath);
Object action = getObject(newClass);
Element element = rootElement.element("action");
if (action instanceof LoginAction) {
LoginAction loginAction = (LoginAction) getAction(action, parameters);
String answer = loginAction.execute();
String page = getPage(element, answer);
view.setJsp(page);
view.setParameters(getMap(newClass, action));
}
return view;
}
private static Class getClass(String classPath) {
try {
return Class.forName(classPath);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
private static Object getObject(Class newClass) {
try {
return newClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
private static Object getAction(Object action, Map<String, String> parameters) {
LoginAction loginAction = (LoginAction) action;
loginAction.setName(parameters.get("name"));
loginAction.setPassword(parameters.get("password"));
return loginAction;
}
private static String getPage(Element element, String answer) {
for (Iterator i = element.elementIterator(); i.hasNext(); ) {
Element result = (Element) i.next();
if (answer.equals(result.attribute("name").getText())) {
return result.getText();
}
}
return "";
}
private static Map<String, String> getMap(Class newClass, Object action) {
Map<String, String> map = new HashMap<>();
Method[] methods = newClass.getDeclaredMethods();
String getterMethod;
for (Method method : methods) {
getterMethod = method.getName();
if (Pattern.matches("get(\\w+)", getterMethod)) {
try {
map.put(getterMethod.substring(3).toLowerCase(),
method.invoke(action).toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
return map;
}
}
//StrutsTest.java
package com.donaldy.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"));
}
}
//View.java
package com.donaldy.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;
}
}
//struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<action name="login" class="com.donaldy.litestruts.LoginAction">
<result name="success">/jsp/homepage.jsp</result>
<result name="fail">/jsp/showLogin.jsp</result>
</action>
<action name="logout" class="com.donaldy.litestruts.LogoutAction">
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</struts>
//LoginAction.java
package com.donaldy.litestruts;
public class LoginAction{
private String name ;
private String password;
private String message;
public LoginAction() {
}
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;
}
}