package framework.core;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import framework.util.XmlUtil;
public class ActionManager {
/**
* 默认路径
*/
public static final String STRUTS_XML_FILE = "/mystruts.xml";
/**
* mystruts.xml类路径
*/
private String filePath;
//拦截器
Map<String,MyInterceptor> intercepts = new HashMap<String, MyInterceptor>();
public ActionManager() {
this.filePath = STRUTS_XML_FILE;
}
public ActionManager(String filePath) {
this.filePath = filePath;
}
/**
* 根据MyFilter传过来的actionName,得到相应的Action类
*/
public ActionClass getActionClass(String actionName) {
// 得到mystruts.xml类路径
InputStream is = this.getClass().getResourceAsStream(filePath);
try {
Document doc = XmlUtil.getDocument(is);
Element root = XmlUtil.getRoot(doc);
// 得到所有的package结点
List<Element> packages = XmlUtil.getElementsByName(root, "package");
if (packages != null && packages.size() > 0) {
for (Element _package : packages) {
// 得到某个package下的所有结点
List<Element> actions = XmlUtil.getElementsByName(_package, "action");
for (Element elem : actions) {
// 判断某个<result>结点元素的name属性是否与传递过来的actionName相等,如果相等那么将其method属性取出
Attribute att = XmlUtil
.getAttributeByName(elem, "name");
if (actionName.equals(att.getText())) {
Attribute cls = XmlUtil.getAttributeByName(elem,
"class");
Attribute method = XmlUtil.getAttributeByName(elem,
"method");
List<Element> results = XmlUtil.getElementsByName(
elem, "result");
List<Element> interceptor_ref = XmlUtil.getElementsByName(
elem, "interceptor-ref");
ActionClass actionClass = new ActionClass();
actionClass.setClassName(cls.getText());
if(method!=null){
actionClass.setMethod(method.getText());
}else{
actionClass.setMethod("execute");
}
actionClass.setElements(results);
actionClass.setInterceptors(interceptor_ref);
return actionClass;
}
}
}
}
} catch (DocumentException e) {
e.printStackTrace();
throw new RuntimeException("mystruts.xml 不存在或有误");
}
return null;
}
/**
* 设置Action参数
*/
public Object setValues(ActionClass actionClass,
Map<String, String[]> params) {
try {
// 得到Action的Class,实例化action
Class clas = Class.forName(actionClass.getClassName());
Object obj = clas.newInstance();
if (params != null && params.size() > 0) {
Iterator<String> it = params.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String[] value = params.get(key);
String upperFirstLetter = key.substring(0, 1).toUpperCase();
// 获得和属性对应的setXXX()方法的名字
String setMethodName = "set" + upperFirstLetter
+ key.substring(1);
Method method = null;
// 看看该页面提交的参数名中,是否在Action有set方法
try {
method = clas.getMethod(setMethodName,new Class[] { String.class });
} catch (NoSuchMethodException e) {
System.out.println(actionClass.getClassName()
+ "." + setMethodName + "("
+ String.class.getName() + ") 不存在");
}
if (method != null) {
// 如果有set方法,就调用set方法,进行赋值操作
String result = this.StringArrayToString(value);
method.invoke(obj, new String[] { result });
}
}
}
return obj;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
throw new RuntimeException("");
}
/**
* 调用Action
*/
public Object invokeAction(ActionClass actionClass,String resultCode) {
try {
Object obj = actionClass.getAction();
if(resultCode==null){
Class clas = obj.getClass();
Method method = clas.getMethod(actionClass.getMethod(), null);
resultCode = (String) method.invoke(obj, null);
}
this.setInvokeResult(resultCode, actionClass);
actionClass.setAction(obj);
return obj;
} catch (InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException("出现InvocationTargetException异常:"
+ e.getMessage());
} catch (SecurityException e) {
e.printStackTrace();
throw new RuntimeException("出现SecurityException异常:"
+ e.getMessage());
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException("出现NoSuchMethodException异常:"
+ e.getMessage());
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException("出现IllegalAccessException异常:"
+ e.getMessage());
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw new RuntimeException("出现IllegalArgumentException异常:"
+ e.getMessage());
}
}
/**
* 匹配<result name="success">/index.jsp</result> Xml中的result
* name属性,如果匹配成功,设置返回结果"/index.jsp"
*/
private void setInvokeResult(String result, ActionClass actionClass) {
List<Element> elements = actionClass.getElements();
for (Element elem : elements) {
Attribute name = XmlUtil.getAttributeByName(elem, "name");
if (result.equals(name.getText())) {
actionClass.setResult(elem.getText());
return;
}
}
}
public String getFilePath() {
return filePath;
}
//获得拦截器
public Map<String,MyInterceptor> getInterceptor() {
// 得到mystruts.xml类路径
InputStream is = this.getClass().getResourceAsStream(filePath);
try {
Document doc = XmlUtil.getDocument(is);
Element root = XmlUtil.getRoot(doc);
// 得到所有的package结点
List<Element> packages = XmlUtil.getElementsByName(root, "package");
if (packages != null && packages.size() > 0) {
for (Element _package : packages) {
// 得到某个interceptors下的所有结点
List<Element> inters = XmlUtil.getElementsByName(_package, "interceptors");
for (Element element : inters) {
List<Element> inter = XmlUtil.getElementsByName(element,"interceptor");
for(Element elem : inter){
Attribute name = XmlUtil.getAttributeByName(elem, "name");
Attribute cls = XmlUtil.getAttributeByName(elem,"class");
try {
//实例化拦截器
Class clas = Class.forName(cls.getText());
Object obj = clas.newInstance();
intercepts.put(name.getText(),(MyInterceptor)obj);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
} catch (DocumentException e) {
e.printStackTrace();
throw new RuntimeException("mystruts.xml 不存在或有误");
}
return intercepts;
模拟struts2框架代码.rar_模拟struts框架
版权申诉
14 浏览量
2022-09-21
20:57:57
上传
评论
收藏 311KB RAR 举报
Kinonoyomeo
- 粉丝: 75
- 资源: 1万+
最新资源
- onenet_chongwukanhu_v06.apk
- 2022NOC软件创意编程赛项真题C++初中-决赛(有解析)
- 2022NOC软件创意编程赛项真题python初中-决赛(有解析)
- openLayer-本地数据加载 (day5)
- A题-正弦信号发生器.xdf
- 2022NOC软件创意编程赛项真题python小学高年级-决赛(有解析)
- mathml转换latex需要的xsl文件
- 2022NOC软件创意编程赛项真题图形化小学高年级-决赛(有解析)
- gbase驱动下载gbase-connector-java-8.3.81.53驱动下载
- 2022NOC软件创意编程赛项真题图形化小学低年级-决赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈