public class MyAction extends Action{//注意,这里并不需要继承DispatchAction
public ActionForward execute(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception { String func = {"insert","delete","modify","list"}; String methodName = null; for(String f : func){ if(request.getParameter(f) != null){ methodName = request.getParameter(f); break; } } try { //利用反射技术让程序自动找到要处理的方法并执行之,注意methodName参数
Method method = this.clazz.getDeclaredMethod(methodName, ActionMapping.class, ActionForm.class, HttpServletRequest.class, HttpServletResponse.class); return (ActionForward)method.invoke(this, mapping, from, request, response); } catch (Exception e) { request.setAttribute("message", e.getMessage()); return mapping.findForward("message"); } } public ActionForward insert(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception { //todo
} public ActionForward delete(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception { //todo
} public ActionForward modify(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception { //todo
} public ActionForward list(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception { //todo
}
|