没什么活干,让我想起了毕业设计,记得当初改过几个毕业设计。有相当一部分Java开发的毕业设计都用到了类似于Struts的一些框架,可是大部分人只是用到了Struts的相当一小部分核心功能。。。
所以在学习过程中我就又萌生了动手的想法,其实仔细想想MVC下的表现层框架工作流程无非是:
1. 拦截并分析请求信息;
2. 利用请求的action名将请求中的参数设入相应的Java对象(既Struts中的Form)中;
3. 利用请求的action名实例化相应的Action类,并调用相应的方法;
4. 利用返回值取得相应的路径,完成跳转。
所以核心功能应该不难实现,主要还是基于Java的反射机制。后来还见过一篇文章,不错,不过我倒是想做一下这位大哥不屑于做的事:再造一个破轮子(木头轮子) 呵呵~
http://superleo.iteye.com/blog/227857
/*
* EasyAction超迷你框架主Servlet
* author KOKONOL
* version 0.00000001
* date 2008-08-29
*/
package EasyAction;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import EasyAction.config.EasyActionConfig;
import EasyAction.servlet.EasyActionServletContext;
import EasyAction.util.StringUtility;
/**
* @author KOKONOL
* @version 0.00000001
* @date 2008-08-29
* */
public class EasyActionServlet extends HttpServlet {
/** 配置Class */
private EasyActionConfig easyActionConfig;
/**
* Constructor of the object.
*/
public EasyActionServlet() {
super();
}
/**
* The doGet method of the servlet.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
EasyActionServletContext.setRequest(request);
EasyActionServletContext.setResponse(response);
// 从requestURL中取得 Action类名 和 Form类名
String requestUrl = request.getRequestURL().toString();
String actionName = requestUrl.substring(requestUrl.lastIndexOf("/") + 1, requestUrl.length()).replace(".do", "");
//若form请求action属性值为"NNN" 则 Action的类名为"NNNAction" Form的类名为"NNNForm"
String actionClassName = easyActionConfig.getActionClassName(actionName);
String formClassName = easyActionConfig.getFormClassName(actionName);
// Action.execute返回值
String resultStr = "";
// 根据请求参数创建Form对象 并 设值
Object formO = null;
try {
formO = this.setForm(formClassName, request.getParameterMap());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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 (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 创建相应Action的对象,并调用 execute方法,取得返回值
try {
// 定义Action的Class 并实例化
Class actionClass = Class.forName(actionClassName);
Object actionO = actionClass.newInstance();
// 取得 execute的 Method实例
Method actionExecuteMethod = actionClass.getMethod("execute", formO.getClass());
// 执行execute方法
resultStr = actionExecuteMethod.invoke(actionO, formO).toString();
} 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 (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException 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();
}
String tgtURL = "";
if(resultStr!=null && !"".equals(resultStr)){
tgtURL = easyActionConfig.getForwardURL(actionName, resultStr);
}
request.getRequestDispatcher(tgtURL).forward(request, response);
}
/**
* Initialization of the servlet.
*/
public void init() throws ServletException {
// 将Servlet内置对象静态封装入EasyActionServletContext中
EasyActionServletContext.setServletContext(this.getServletContext());
// 取得配置文件所在的路径
String configPath = this.getServletContext().getRealPath("WEB-INF");
try {
// 取得配置类的实例
easyActionConfig = new EasyActionConfig(configPath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Set request parameter values of request to FormObject.
*/
private Object setForm(String formClassName, Map<String,String[]> paramMap) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
// 声明并实例化FormClass
Class formClass = Class.forName(formClassName);
Object formObject = formClass.newInstance();
// 如果没有请求参数 则 退出该方法
if(paramMap == null || paramMap.size() == 0){
return null;
}
// 取得FormClass的方法列表
Method[] allMethod = formClass.getMethods();
// 遍历FormClass的方法列表
Iterator<String> it = paramMap.keySet().iterator();
w: while(it.hasNext()){
// 取得请求参数名
String key = it.next();
// 取得该请求参数对应的set方法名
String methodName = "set"+StringUtility.toUpperCaseFirst(key);
for(Method m:allMethod){
if(m.getName().equals(methodName)){
// 选择请求参数在Form中对应的set方法
String[] paramValue = paramMap.get(key);
// 取得该Set方法第一个参数的类型
Class paramType = m.getParameterTypes()[0];
if(String[].class.equals(paramType)){
// 请求参数为String[]的情况时
Method method = formClass.getMethod(methodName, new Class[]{String[].class});
method.invoke(formObject, new Object[]{paramValue});
}else{
// 请求参数为String的情况时
Method method = formClass.getMethod(methodName, new Class[]{String.class});
method.invoke(formObject, new Object[]{paramValue[0]});
}
continue w;
}
}
}
return formObject;
}
}
代码还没有完成,还有不少任务。。。比如Exception都没有处理。
稍后我会上传完整的代码。。。
这只是实现了基础中的基础的功能,通过写这点东西 我发现有很多东西需要学一下,接下来学一学dom4j,然后学一下log4j。让我这个破轮子能够真正的跑起来。