<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)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 国产精品色拉拉免费看| 97人妻精品全国免费视频| 无人影院手机版在线观看免费| 亚洲五月激情综合图片区| 人人揉揉香蕉大免费不卡| 久久久久亚洲精品无码系列| 久久国产乱子精品免费女| 亚洲天堂中文资源| 18禁美女裸体免费网站 | 99久久成人国产精品免费| 亚洲人成亚洲人成在线观看 | 精品亚洲成在人线AV无码| 成视频年人黄网站免费视频| 亚洲依依成人亚洲社区| 日韩一级免费视频| 一级特级女人18毛片免费视频| 亚洲精品字幕在线观看| 2021精品国产品免费观看| 精品久久亚洲中文无码| 国产国产人免费人成免费视频| 尤物视频在线免费观看| 国产亚洲综合久久系列| 2015日韩永久免费视频播放 | 波多野结衣久久高清免费| 男女作爱免费网站| 亚洲综合无码一区二区| 亚洲第一成年免费网站| 免费无码婬片aaa直播表情| 亚洲国产精品成人精品无码区| 日本免费网址大全在线观看| 美女被爆羞羞网站免费| 国产亚洲精品一品区99热| www视频在线观看免费| 国产亚洲精品美女久久久久| 精品国产_亚洲人成在线高清| 免费影院未满十八勿进网站| 日本黄页网址在线看免费不卡 | 亚洲AV无码一区东京热| 在线观看免费宅男视频| a级毛片视频免费观看| 亚洲国产欧洲综合997久久|