1 package com.ibatis.struts; 2 3 import org.apache.struts.action.Action; 4 import org.apache.struts.action.ActionForm; 5 import org.apache.struts.action.ActionForward; 6 import org.apache.struts.action.ActionMapping; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 import java.lang.reflect.Method ; 11 12 104 public class BeanAction extends Action { 105 106 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 107 throws Exception { 108 109 String forward = "success"; 110 111 try { 112 113 ActionContext.initialize(request, response); 114 115 if (form != null) { 116 117 Method method = null; 119 String methodName = mapping.getParameter(); 120 if (methodName != null && !"*".equals(methodName)) { 121 try { 122 method = form.getClass().getMethod(methodName, null); 123 forward = (String ) method.invoke(form, null); 124 } catch (Exception e) { 125 throw new BeanActionException("Error dispatching bean action via method parameter ('" + methodName + "'). Cause: " + e, e); 126 } 127 } 128 129 if (method == null && !"*".equals(methodName)) { 131 methodName = mapping.getPath(); 132 if (methodName.length() > 1) { 133 int slash = methodName.lastIndexOf("/") + 1; 134 methodName = methodName.substring(slash); 135 if (methodName.length() > 0) { 136 try { 137 method = form.getClass().getMethod(methodName, null); 138 forward = (String ) method.invoke(form, null); 139 } catch (Exception e) { 140 throw new BeanActionException("Error dispatching bean action via URL pattern ('" + methodName + "'). Cause: " + e, e); 141 } 142 } 143 } 144 } 145 } 146 147 } catch (Exception e) { 148 request.setAttribute("BeanActionException", e); 149 throw e; 150 } 151 152 return mapping.findForward(forward); 153 } 154 155 } 156 | Popular Tags |