非常讨厌Struts1 Action的设计,一堆花俏的概念,没必要的复杂度. 但是工作关系,还非要使用这个垃圾,没办法,只好把苍蝇包起来咽下去.
做一个Action基类,SimpleAction ,把它封装的更像webwork.
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;
/** * @author jindw * @xwork.package name = "" */ public final class SimpleAction extends AbstractAction { /** * Commons Logging instance. */ protected static Log log = LogFactory.getLog(SimpleAction.class);
/** * common result name define */ public static final String SUCCESS = "success";
public static final String ERROR = "error";
public static final String INPUT = "input";
/** * method cache */ protected Map executeMap = new HashMap();
protected Map executeArgumentTypesMap = new HashMap();
public SimpleAction() { super(); Method[] methods = this.getClass().getMethods(); for (int i = 0; i < methods.length; i++) {
Method method = methods[i]; if (String.class != method.getReturnType()) { continue; } Class[] params = method.getParameterTypes(); int argCount = 0; for (int j = 0; j < params.length; j++) { if (ActionForm.class.isAssignableFrom(params[j])) { argCount++; } else if (HttpServletRequest.class.isAssignableFrom(params[j])) { argCount++; } else if (HttpServletResponse.class .isAssignableFrom(params[j])) { argCount++; } else if (ActionMapping.class.isAssignableFrom(params[j])) { argCount++; } else { argCount = -1; break; } } if (argCount >= 0) { executeMap.put(method.getName(), method); executeArgumentTypesMap.put(method.getName(), params); } } initialize(); }
protected void initialize() { Method[] methods = this.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Class[] paramTypes = methods[i].getParameterTypes(); if (paramTypes.length == 1) { String name = methods[i].getName(); if (name.startsWith("set")) { name = name.substring(3); if (name.length() > 0) { char fc = name.charAt(0); if (Character.isUpperCase(fc)) { name = Character.toLowerCase(fc) + name.substring(1); //implement it eg:get from Spring Context Object value = getBean(name); if (value != null) { try { methods[i].invoke(this, new Object[] { value }); } catch (Exception e) { log.info("set property error:", e); } } } } } } } }
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Get the method's name. This could be overridden in subclasses. String methodName = getMethodName(mapping, form); // Invoke the named method, and return the result String result = dispatchMethod(mapping, form, request, response, methodName); request.setAttribute("actionForm", form); if(result == null){ return null; }else{ return transformForward(mapping.findForward(result), request); } }
/** * @param forward * @param request * @return */ protected ActionForward transformForward(ActionForward forward, HttpServletRequest request) { if (forward == null) { return null; } else { StringBuffer buf = new StringBuffer(forward.getPath()); boolean containsVariable = false; for (int k = 0, i = buf.indexOf("${", k); i >= 0; i = buf.indexOf( "${", k), k = i) { int j = buf.indexOf("}", i); if (j > 0) { containsVariable = true; k = j; String key = buf.substring(i + 2, j); Object o = null; if(key.indexOf('.')<0){ o=request.getAttribute(key); }else{ String[] keys = key.split("[.]"); o=request.getAttribute(keys[0]); for(int l = 1;l<keys.length;l++){ try { o = BeanUtilsBean.getInstance().getPropertyUtils().getProperty(o, keys[l].trim()); } catch (Exception e) { log.debug("find property error:", e); o = null; break; } } } buf.replace(i, j + 1, String.valueOf(o)); } } if (containsVariable) { forward = new ActionForward(forward); forward.setPath(buf.toString()); return forward; } else { return forward; } } }
public static void main(String[] args) { StringBuffer buf = new StringBuffer("http://sdssfs${123}&{123}&${sd}"); for (int k = 0, i = buf.indexOf("${", k); i >= 0; i = buf.indexOf("${", k), k = i) { int j = buf.indexOf("}", i); if (j > 0) { k = j; String key = buf.substring(i + 2, j); buf.replace(i, j + 1, "%" + key + "%"); } } System.out.println(buf); }
protected String dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String methodName) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Method method = (Method) executeMap.get(methodName); Class[] argumentTypes = (Class[]) executeArgumentTypesMap .get(methodName); Object[] params = new Object[argumentTypes.length]; for (int i = 0; i < params.length; i++) { Class type = argumentTypes[i]; if (ActionForm.class.isAssignableFrom(type)) { if(type.isAssignableFrom(form.getClass())){ params[i] = form; }else{ throw new ClassCastException("action form type is:"+form.getClass()+";but required:"+type+";"); } } else if (HttpServletRequest.class.isAssignableFrom(type)) { params[i] = request; } else if (HttpServletResponse.class.isAssignableFrom(type)) { params[i] = response; } else if (ActionMapping.class.isAssignableFrom(type)) { params[i] = mapping; } } return (String) method.invoke(this, params); }
protected String getMethodName(ActionMapping mapping, ActionForm form) { String param = mapping.getParameter(); if (param != null) { int i = param.indexOf("method="); if (i > 0) { int j = param.indexOf(i, ';'); if (j >= 0) { return param.substring(i + ("method=".length()), j).trim(); } else { return param.substring(i + ("method=".length())).trim(); } } else { if (this.executeMap.containsKey(param)) { return param; } } } return "execute"; }
} |
|