-
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 #27 from xiaomingbai/master
1.convert to maven; 2.add struts
- Loading branch information
Showing
12 changed files
with
934 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="src" path="test"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="output" path="bin"/> | ||
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="test"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="test_resource"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>498654356Learning_one</groupId> | ||
<artifactId>498654356Learning_one</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<build> | ||
<sourceDirectory>src</sourceDirectory> | ||
<testSourceDirectory>test</testSourceDirectory> | ||
<resources> | ||
<resource> | ||
<directory>src</directory> | ||
<excludes> | ||
<exclude>**/*.java</exclude> | ||
</excludes> | ||
</resource> | ||
</resources> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.5.1</version> | ||
<configuration> | ||
<source/> | ||
<target/> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/dom4j/dom4j --> | ||
<dependency> | ||
<groupId>dom4j</groupId> | ||
<artifactId>dom4j</artifactId> | ||
<version>1.6.1</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
66 changes: 66 additions & 0 deletions
66
group04/498654356/one/src/org/coding/two/ActionBeanDefinition.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,66 @@ | ||
package org.coding.two; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* 封装 Struts.xml 中 action 标签 | ||
*/ | ||
public class ActionBeanDefinition { | ||
public static final String TAG_ACTION = "action"; | ||
public static final String TAG_RESULT = "result"; | ||
public static final String TAG_ACTION_ATTR_NAME = "name"; | ||
public static final String TAG_ACTION_ATTR_CLASS = "class"; | ||
public static final String TAG_RESULT_ATTR_NAME = "name"; | ||
public static final String DEFAULT_RESOURCE = "org/coding/two/struts.xml"; | ||
|
||
private String name; | ||
|
||
private String className; | ||
|
||
private Map<String, String> resultMap; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getClassName() { | ||
return className; | ||
} | ||
|
||
public void setClassName(String className) { | ||
this.className = className; | ||
} | ||
|
||
public Map<String, String> getResultMap() { | ||
return resultMap; | ||
} | ||
|
||
public ActionBeanDefinition(String name, String className) { | ||
super(); | ||
this.name = name; | ||
this.className = className; | ||
this.resultMap = new HashMap<String, String>(); | ||
} | ||
|
||
/** | ||
* | ||
* @param name result 标签的 name 属性值 | ||
* @param viewPath result 标签的视图路径 | ||
*/ | ||
public void putResult(String name, String viewPath) { | ||
this.resultMap.put(name, viewPath); | ||
// this.resultMap.put("name", name); | ||
// this.resultMap.put("view", viewPath); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ActionBeanDefinition [name=" + name + ", className=" + className + ", resultMap=" + resultMap + "]"; | ||
} | ||
|
||
} |
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,45 @@ | ||
package org.coding.two; | ||
|
||
/** | ||
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 | ||
* @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; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LoginAction [name=" + name + ", password=" + password + ", message=" + message + "]"; | ||
} | ||
|
||
} |
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,150 @@ | ||
package org.coding.two; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.dom4j.Document; | ||
import org.dom4j.DocumentException; | ||
import org.dom4j.Element; | ||
import org.dom4j.io.SAXReader; | ||
|
||
|
||
|
||
public class Struts { | ||
|
||
private static final String METHOD_SET = "set"; | ||
private static final String METHOD_GET = "get"; | ||
private static Map<String, ActionBeanDefinition> actionDefinitionMap = new HashMap<String, ActionBeanDefinition>(); | ||
|
||
static { | ||
//0 | ||
parseXml(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static void parseXml(){ | ||
SAXReader reader = new SAXReader(); | ||
Document document = null; | ||
String path = Struts.class.getClassLoader().getResource("").getPath(); | ||
try { | ||
document = reader.read(path + ActionBeanDefinition.DEFAULT_RESOURCE); | ||
} catch (DocumentException e) { | ||
throw new RuntimeException(e); | ||
} | ||
Element struts = document.getRootElement(); | ||
List<Element> actions = struts.elements(ActionBeanDefinition.TAG_ACTION); | ||
for (Element element : actions) { | ||
String name = element.attributeValue(ActionBeanDefinition.TAG_ACTION_ATTR_NAME); | ||
String className = element.attributeValue(ActionBeanDefinition.TAG_ACTION_ATTR_CLASS); | ||
List<Element> results = element.elements(ActionBeanDefinition.TAG_RESULT); | ||
ActionBeanDefinition beanDefinition = new ActionBeanDefinition(name, className); | ||
for (Element result : results) { | ||
beanDefinition.putResult(result.attributeValue(ActionBeanDefinition.TAG_RESULT_ATTR_NAME), result.getTextTrim()); | ||
} | ||
actionDefinitionMap.put(name, beanDefinition); | ||
} | ||
// System.out.println(actionDefinitionMap); | ||
} | ||
|
||
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字段中。 | ||
*/ | ||
|
||
if(!actionDefinitionMap.containsKey(actionName)) { | ||
throw new RuntimeException("does not exist action : " + actionName); | ||
} | ||
ActionBeanDefinition beanDefinition = actionDefinitionMap.get(actionName); | ||
try { | ||
//1 | ||
Class<?> bean = Class.forName(beanDefinition.getClassName()); | ||
Object instance = bean.newInstance(); | ||
Set<String> keySet = parameters.keySet(); | ||
for (String key : keySet) { | ||
Method method = bean.getMethod(getSetMethodName(key), new Class[]{String.class}); | ||
method.invoke(instance, parameters.get(key)); | ||
} | ||
// System.out.println(instance); | ||
//2 | ||
Method method = bean.getMethod("execute"); | ||
Object result = method.invoke(instance); | ||
|
||
//3 | ||
Method[] methods = bean.getMethods(); | ||
Map<String, Object> parameterss = new HashMap<String, Object>(); | ||
for (Method m : methods) { | ||
String methodName = m.getName(); | ||
if(methodName.contains(METHOD_GET)) { | ||
String para = getPropNameByMethoedName(methodName); | ||
parameterss.put(para, m.invoke(instance)); | ||
} | ||
} | ||
View view = new View(); | ||
view.setParameters(parameterss); | ||
// System.out.println(parameterss); | ||
//4 | ||
String jsp = actionDefinitionMap.get(actionName).getResultMap().get(result); | ||
view.setJsp(jsp); | ||
return view; | ||
|
||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (InstantiationException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} catch (IllegalAccessException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} catch (NoSuchMethodException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} catch (SecurityException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} catch (IllegalArgumentException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} catch (InvocationTargetException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
|
||
|
||
return null; | ||
} | ||
|
||
|
||
private static String getPropNameByMethoedName(String methodName) { | ||
String prop = null; | ||
if(methodName.contains(METHOD_GET)){ | ||
String s = methodName.substring(3); | ||
prop = s.substring(0, 1).toLowerCase() + s.substring(1); | ||
} | ||
return prop; | ||
} | ||
|
||
private static String getSetMethodName(String para) { | ||
return METHOD_SET + para.substring(0, 1).toUpperCase() + para.substring(1); | ||
} | ||
} |
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 org.coding.two; | ||
|
||
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.