<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-57  評論-129  文章-0  trackbacks-0

    非常討厭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";
        }


    }





    評論也很精彩,請點擊查看精彩評論。歡迎您也添加評論。查看詳細 >>





    JavaEye推薦
    杭州:外企高薪聘請系統維護工程師(10-15K)
    杭州:國內大型網絡公司高薪招聘系統架構師,資深JAVA開發工程師
    北京:優秀公司NHNChina招聘:WEB開發,系統管理,JAVA開發, DBA
    廣州:急招 JAVA開發經理/系統架構師(10-15K/月)也招聘java程序員



    文章來源: http://jindw.javaeye.com/blog/33983
    posted on 2006-09-05 18:01 金大為 閱讀(81) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 日韩免费观看一级毛片看看 | 亚洲第一香蕉视频| 曰批全过程免费视频免费看| 日韩一区二区在线免费观看| 中文无码亚洲精品字幕| 成**人免费一级毛片| 亚洲情A成黄在线观看动漫软件 | 免费A级毛片无码视频| 久久亚洲国产精品五月天| 暖暖免费日本在线中文| 久久精品国产精品亚洲色婷婷| 成全高清在线观看免费| 亚洲精品天天影视综合网| 37pao成人国产永久免费视频| 亚洲欧洲自拍拍偷综合| 69堂人成无码免费视频果冻传媒| 亚洲国产精品张柏芝在线观看| 国产免费久久精品99re丫y| 亚洲综合成人婷婷五月网址| 国产美女无遮挡免费视频网站| 曰批免费视频播放在线看片二| 国产成人精品日本亚洲专区61| 成人免费乱码大片A毛片| 久久久亚洲欧洲日产国码aⅴ| 美女网站免费福利视频| 国产精品亚洲综合天堂夜夜| 中国亚洲女人69内射少妇| 色欲国产麻豆一精品一AV一免费 | 亚洲精品综合一二三区在线| 国产精品入口麻豆免费观看| 亚洲精品永久在线观看| 亚洲男人的天堂在线va拉文| 久久精品电影免费动漫| 一本天堂ⅴ无码亚洲道久久 | 亚洲αv在线精品糸列| A在线观看免费网站大全| 粉色视频免费入口| 亚洲欧洲在线观看| 国产精品色午夜免费视频| 三年片在线观看免费西瓜视频| 亚洲日韩国产精品乱-久|