<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的設計,一堆花俏的概念,沒必要的復雜度.
    但是工作關(guān)系,還非要使用這個垃圾,沒辦法,只好把蒼蠅包起來咽下去.

    做一個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推薦
    杭州:外企高薪聘請系統(tǒng)維護工程師(10-15K)
    杭州:國內(nèi)大型網(wǎng)絡公司高薪招聘系統(tǒng)架構(gòu)師,資深JAVA開發(fā)工程師
    北京:優(yōu)秀公司NHNChina招聘:WEB開發(fā),系統(tǒng)管理,JAVA開發(fā), DBA
    廣州:急招 JAVA開發(fā)經(jīng)理/系統(tǒng)架構(gòu)師(10-15K/月)也招聘java程序員



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

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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 亚欧乱色国产精品免费视频| 免费精品国自产拍在线播放| 亚洲国产国产综合一区首页| 亚洲国产香蕉碰碰人人| 青青操免费在线视频| 亚欧色视频在线观看免费| 免费黄色app网站| 红杏亚洲影院一区二区三区| 亚洲成AV人片在线观看无| 亚洲1234区乱码| 成人精品综合免费视频| 美女被免费喷白浆视频| 伊人久久大香线蕉亚洲| 亚洲另类无码一区二区三区| 91国内免费在线视频| 亚洲成av人片天堂网| 2021国内精品久久久久精免费 | 精品国产一区二区三区免费| 97免费人妻无码视频| 亚洲中文字幕久久精品无码喷水 | 理论亚洲区美一区二区三区| a毛片免费全部播放完整成| 亚洲AV成人无码久久精品老人| 免费国产高清毛不卡片基地| 久久久久亚洲av成人无码电影 | 久久久亚洲AV波多野结衣| 老司机午夜精品视频在线观看免费| 国产免费一区二区三区| 亚洲日韩AV无码一区二区三区人| 国产在线ts人妖免费视频| 亚洲电影唐人社一区二区| 精品免费视在线观看| 亚洲制服丝袜一区二区三区| 午夜精品免费在线观看| 国产av无码专区亚洲国产精品| 亚洲一区二区三区高清在线观看| 亚洲视频免费播放| 久久精品国产亚洲AV电影网| 亚洲大尺度无码无码专区| 国产香蕉九九久久精品免费| 色婷婷六月亚洲婷婷丁香|