Skip to content

Commit

Permalink
part2 -- parsing the configuration file of struts
Browse files Browse the repository at this point in the history
  • Loading branch information
gongxun committed Mar 4, 2017
1 parent 374d7cd commit 7996ecb
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 0 deletions.
39 changes: 39 additions & 0 deletions group17/785396327/3.5/struts/LoginAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package struts;

/**
* Created by IBM on 2017/3/4.
*/
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;
}
}
36 changes: 36 additions & 0 deletions group17/785396327/3.5/struts/LoginActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package struts;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Assert;
import org.junit.Test;

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

/**
* Created by IBM on 2017/3/4.
*/
public class LoginActionTest {

@Test
public void fun1() throws DocumentException {
InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("struts/struts.xml");
SAXReader reader = new SAXReader();
Element rootElement = reader.read(resourceAsStream).getRootElement();
Element selectedElement = (Element) rootElement.selectSingleNode("//action[@name='login']");
System.out.println(selectedElement.attributeValue("class"));

}

@Test
public void fun2() {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("name", "test");
parameters.put("password", "1234");
View view = Struts.runAction("login", parameters);
Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
}
}
86 changes: 86 additions & 0 deletions group17/785396327/3.5/struts/Struts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package struts;

import org.dom4j.Element;

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

/**
* Created by william on 2017/3/4.
*/
public class Struts {


public static View runAction(String actionName, Map<String, String> parameters) {
Element root = StrutsUtils.getRoot("struts/struts.xml");
View view = null;
if (root != null) {
Element selectedEle = (Element) root.selectSingleNode("//action[@name='" + actionName + "']");
if (selectedEle != null) {
Class clazz = genClass(selectedEle.attributeValue("class"));
Object target = setValue(parameters, clazz);
String result;
try {
result = (String) clazz.getMethod("execute").invoke(target);
} catch (Exception e) {
throw new RuntimeException("invoke execute have some error", e);
}
Map<String, Object> response = getValue(clazz, target);
view = new View();
view.setParameters(response);
Element selectedResult = (Element) root.selectSingleNode("//action[@name='" + actionName + "']//result[@name='" + result + "']");
view.setJsp(selectedResult == null ? null : selectedResult.getText());
}
}
return view;
}


private static Class genClass(String className) {
Class clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return clazz;
}

private static Object setValue(Map<String, String> parameters, Class clazz) {
try {
Object target = clazz.newInstance();
if (!StrutsUtils.isEmpty(parameters)) {
for (Map.Entry<String, String> entry : parameters.entrySet()) {
String key = entry.getKey();
if (!StrutsUtils.isEmpty(key)) {
String setterName = new StringBuilder("set").append(key.substring(0, 1).toUpperCase()).append(key.substring(1, key.length())).toString();
clazz.getMethod(setterName, String.class).invoke(target, entry.getValue());
}
}
}
return target;
} catch (Exception e) {
throw new RuntimeException("create class instance have some error ", e);
}
}

private static Map<String, Object> getValue(Class clazz, Object target) {
Map<String, Object> resultsMap = new HashMap<String, Object>();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
String fieldName = method.getName();
if (fieldName.startsWith("get") && !fieldName.equals("getClass")) {
try {
Object value = method.invoke(target);
resultsMap.put(new StringBuilder(fieldName.substring(3, 4)).append(fieldName.substring(4, fieldName.length())).toString(), value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return resultsMap;
}


}
30 changes: 30 additions & 0 deletions group17/785396327/3.5/struts/StrutsUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package struts;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;

/**
* Created by IBM on 2017/3/4.
*/
public class StrutsUtils {

public static Element getRoot(String filePath) {
try {
InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
SAXReader saxReader = new SAXReader();
return saxReader.read(resourceAsStream).getRootElement();
} catch (DocumentException e) {
throw new RuntimeException("初始化文件异常", e);
}
}

public static boolean isEmpty(Object obj) {
if (obj instanceof String)
return obj == null || obj.equals("");
else
return obj == null;
}
}
26 changes: 26 additions & 0 deletions group17/785396327/3.5/struts/View.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package struts;

import java.util.Map;

/**
* Created by IBM on 2017/3/4.
*/
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;
}
}
11 changes: 11 additions & 0 deletions group17/785396327/3.5/struts/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="struts.LoginAction">
<result name="success">/jsp/homepage.jsp</result>
<result name="fail">/jsp/showLogin.jsp</result>
</action>
<action name="logout" class="struts.LoginAction">
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</struts>

0 comments on commit 7996ecb

Please sign in to comment.