模拟struts中的DispatchAction

package org.lbr.action;

import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MyDispatch extends Action {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  response.setCharacterEncoding("GBK");
  PrintWriter out = response.getWriter();

  String parameterName = mapping.getParameter();
  String parameterValue = request.getParameter(parameterName);
  if (parameterValue.equals("")) {
   out.println(parameterName + "指定的属性值为空!");
  } else if (parameterValue.equals("execute")) {
   out.println("指定的属性值不能为 execute");
  } else {
   Class<?>[] types = new Class<?>[4];
   types[0] = Class.forName("org.apache.struts.action.ActionMapping");
   types[1] = Class.forName("org.apache.struts.action.ActionForm");
   types[2] = Class.forName("javax.servlet.http.HttpServletRequest");
   types[3] = Class.forName("javax.servlet.http.HttpServletResponse");

   try {
    Method method = this.getClass()
      .getMethod(parameterValue, types);
    if (method != null) {
     return (ActionForward) method.invoke(this, mapping, form,
       request, response);
    }
   } catch (NoSuchMethodException e) {
    throw new Exception("\n没有找到名为 \npublic ActionForward "
        + parameterValue
        + "(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception\n的方法");
   } catch (SecurityException e) {
    throw e;
   }
  }

  return null;
 }

}