博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个模仿Struct的Action类
阅读量:6821 次
发布时间:2019-06-26

本文共 6189 字,大约阅读时间需要 20 分钟。

hot3.png

我在项目中也写了一个模仿Struct的Action类,目前还不成熟,先拿出来讨论讨论。mfaction.xml 
testExcel.jsp
/login.jsp
/**************************************************************** * MFActionManager动作管理类 * *

* MFActionManager动作管理类 *

* * AndyZhang 2011 * @version 0.1 * ToDo:2011-12-29 ***************************************************************/package weaver.action;import java.io.File;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import weaver.core.MFPath;import weaver.core.MFString;import weaver.debug.MFDebug;import weaver.ormapping.MFDbTypeTransfer;import weaver.ormapping.MFReflect;public class MFActionManager { private List
includeFiles; private Map
configs; private MFActionItems items; private HttpServletRequest request; private HttpServletResponse response; private static String ext=".tw"; private static String execute="execute"; public MFActionManager() { includeFiles=new ArrayList
(); configs=new HashMap
(); //loadMain(); items=new MFActionItems(); } public MFActionManager(HttpServletRequest request,HttpServletResponse response) { this(); updateState(request,response); } public void updateState(HttpServletRequest request,HttpServletResponse response) { this.request=request; this.response=response; } public boolean prepareProperty(Class
classType,Object obj) throws Throwable { if (obj instanceof IMFRequestAware) { ((IMFRequestAware) obj).setRequest(request); ((IMFRequestAware) obj).setResponse(response); } Enumeration params=request.getParameterNames(); while(params.hasMoreElements()){ String name=(String)params.nextElement(); String value=request.getParameter(name); if (MFString.NotNullOrEmpty(name) && MFReflect.IsHaveProperty(obj,name)) { Class
type=MFReflect.GetPropertyType(obj, name); Object typedValue = MFDbTypeTransfer.toJavaType(type, value); MFReflect.SetProperty(obj, name, typedValue); } } return true; } public boolean runAction(MFActionItem item) throws Throwable { if (item!=null) { Class
c=Class.forName(item.getClassName()); Object obj=c.newInstance(); prepareProperty(c,obj); String methodName =MFString.NullToString(item.getMethod(),execute); Method method=c.getDeclaredMethod(methodName, null); Object value=method.invoke(obj, null); if (value!=null) { String url=item.getResults().get(value); if (MFString.NotNullOrEmpty(url)) { //String path=MFString.NullToString(item.getNamespace())+url; response.sendRedirect(url); } } } return false; } public boolean runAction(String url) throws Throwable { MFActionItem item=findAction(url); return runAction(item); } public String CheckPath(String url) { if (MFString.NotNullOrEmpty(url) && !url.startsWith(MFPath.useparator)) url=MFPath.useparator+url; return url; } public MFActionItem findAction(String url) { for(MFActionItem item:items) { String Str=MFString.NullToString(item.getNamespace())+item.getActionName()+ext; Str=CheckPath(Str); if (MFString.ComparerNoCase(url, Str)) { MFDebug.println("find"); return item; } } return null; } public String getAttrValue(Element item,String name) { Attribute attr=item.attribute(name); if (attr!=null) { String text=attr.getText(); return text; } return null; } public boolean loadConfig(String filePath) throws DocumentException { File file = new File(filePath); SAXReader reader = new SAXReader(); Document doc=reader.read(file); Element root = doc.getRootElement(); //load include file for (Iterator
i = root.elementIterator("include"); i.hasNext();) { Element item = (Element) i.next(); String value=getAttrValue(item,"file"); if (MFString.NotNullOrEmpty(value)) includeFiles.add(value); } //load constant for (Iterator
i = root.elementIterator("constant"); i.hasNext();) { Element item = (Element) i.next(); String conName=getAttrValue(item,"name"); String conValue=getAttrValue(item,"value"); if (MFString.NotNullOrEmpty(conName)&&MFString.NotNullOrEmpty(conValue) ) { configs.put(conName, conValue); } } //load action for (Iterator
i = root.elementIterator("package"); i.hasNext();) { Element packageItem = (Element) i.next(); String packageName=getAttrValue(packageItem,"name"); String namespace=getAttrValue(packageItem,"namespace"); for (Iterator
j = packageItem.elementIterator("action"); j.hasNext();) { MFActionItem action=new MFActionItem(); Element actionItem = (Element) j.next(); String actionName=getAttrValue(actionItem,"name"); String className=getAttrValue(actionItem,"class"); String method=getAttrValue(actionItem,"method"); action.setPackageName(packageName); action.setNamespace(namespace); action.setActionName(actionName); action.setActionName(actionName); action.setClassName(className); action.setMethod(method); for (Iterator
k = actionItem.elementIterator("result"); k.hasNext();) { Element resultItem = (Element) k.next(); String resultName=getAttrValue(resultItem,"name"); String url=(String)resultItem.getData(); action.getResults().put(resultName, url); } items.add(action); } } return true; } public void setIncludeFiles(List
includeFiles) { this.includeFiles = includeFiles; } public List
getIncludeFiles() { return includeFiles; }}

转载于:https://my.oschina.net/cnet2001/blog/55002

你可能感兴趣的文章
[转]Web Api系列教程第2季(OData篇)(二)——使用Web Api创建只读的OData服务
查看>>
linux内核段属性机制【转】
查看>>
eclipse设置系统字体
查看>>
复旦大学考研科目
查看>>
16、Java并发性和多线程-死锁
查看>>
Linux下用netstat查看网络状态、端口状态
查看>>
Java 实现有序链表
查看>>
zoj 1203 Swordfish
查看>>
手机怎么访问电脑服务器上的网页
查看>>
Python帮助函数调试函数 用于获取对象的属性及属性值
查看>>
制做rpm包工具fpm安装
查看>>
POJ 2253-Frogger (Prim)
查看>>
哪种锻炼方式最能让程序猿远离亚健康? - 强烈推荐
查看>>
基于Metronic的Bootstrap开发框架经验总结(15)-- 更新使用Metronic 4.75版本
查看>>
Kafka(二)-- 安装配置
查看>>
MapReduce 图解流程
查看>>
[LeetCode] Wildcard Matching
查看>>
推荐系统
查看>>
HoloLens | 世界的每一次变化,其实都提前打好了招呼
查看>>
seq命令
查看>>