Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 1301|回复: 0

struts1.3执行流程分析下

[复制链接]

该用户从未签到

发表于 2011-8-1 10:14:34 | 显示全部楼层 |阅读模式




17、为ActionForm填充数据。
    processPopulate(request, response, form, mapping);   
    1、首先判定form是否为null,假如是则直接return。
        if (form == null) {
            return;
        }   
    2、往log里写入一句话提示从这里开始填充formBean
        if (log.isDebugEnabled()) {
            log.debug(" Populating bean properties from this request");
        }   
    3、设置Servletservletservletservletservletservletservletservletstrutsservletservletstrutsstrutsservletstrutsservletservletservletservletservletservletservletservletservlet。
        form.set(this.);
        (不知道详细作用)        
    4、执行reset方法重置表单。默认reset方法不做任何事情。这个方法是为了利便扩展。可以继续ActionForm类重写reset方法,这个方法可以用来做设置一些默认值等工作。
        form.reset(mapping, request);        
    5、假如是上传表单,则获取上传类。(不甚了解)
        if (mapping.getMultipartClass() != null) {
            request.setAttribute(Globals.MULTIPART_KEY,
                mapping.getMultipartClass());
        }   
    6、填充form
       RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(), request);      
        1、建立一个HashMap 用于存放属性
            HashMap properties = new HashMap();            
        2、建立一个Enumeration用于存放参数名
            Enumeration names = null;        
        3、建立一个Map来存放multipart参数
            Map multipartParameters = null;        
        4、获取哀求的ContentType和Method。并设置multipart表示为false。
            String contentType = request.getContentType();
            String method = request.getMethod();
            boolean isMultipart = false;            
        5、假如是multipart表单则做上传处理(不甚了解)
            if (bean instanceof ActionForm) {
                ((ActionForm) bean).setMultipartRequestHandler(null);
            }
            MultipartRequestHandler multipartHandler = null;
            if ((contentType != null)&& (contentType.startsWith("multipart/form-data"))&& (method.equalsIgnoreCase(&quotOST"))) {
                ActionWrapper ;
                if (bean instanceof ActionForm) {
                     = ((ActionForm) bean).getWrapper();
                } else {
                    throw new Exception("bean that's supposed to be "
                        + "populated from a multipart request is not of type "
                        + "\"org.apache..action.ActionForm\", but type "
                        + "\"" + bean.getClass().getName() + "\"");
                }
                multipartHandler = getMultipartHandler(request);
                if (multipartHandler != null) {
                    isMultipart = true;
                    .setFor(multipartHandler);
                    multipartHandler.setMapping((ActionMapping) request .getAttribute(Globals.MAPPING_KEY));
                    multipartHandler.handleRequest(request);
                    Boolean maxLengthExceeded =
(Boolean) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
                    if ((maxLengthExceeded != null)
                        && (maxLengthExceeded.booleanValue())) {
                        ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
                        return;
                    }
                    multipartParameters =
                        getAllParametersForMultipartRequest(request,
                            multipartHandler);
                    names = Collections.enumeration(multipartParameters.keySet());
                }
            }            
        6、假如不是上传,则把参数名存到names枚举里面。
            if (!isMultipart) {
                names = request.getParameterNames();
            }        
        7、遍历这个枚举
            while (names.hasMoreElements())            
            1、把名字拿出来存到name和stripped变量里
                String name = (String) names.nextElement();
                String stripped = name;               
            2、去掉name的前缀和后缀(假如有的话(配置文件里可以配置))
                if (prefix != null) {
                    if (!stripped.startsWith(prefix)) {
                        continue;
                    }
                    stripped = stripped.substring(prefix.length());
                }
                if (suffix != null) {
                    if (!stripped.endsWith(suffix)) {
                        continue;
                    }
                    stripped = stripped.substring(0, stripped.length() - suffix.length());
                }               
            3、获取参数值,分上传和非上传两种方式
                Object parameterValue = null;
                if (isMultipart) {
                    parameterValue = multipartParameters.get(name);
                    parameterValue = rationalizeMultipleFileProperty(bean, name, parameterValue);
                } else {
                    parameterValue = request.getParameterValues(name);
                }               
            4、假如参数名去掉了前缀后缀不是以org.Apache.开头则把参数存到定义好的HashMap里
                if (!(stripped.startsWith("org.apache.."))) {
                    properties.put(stripped, parameterValue);
                }               
        8、调用BeanUtils的方法把formBean的属性填充进去
            try {
                BeanUtils.populate(bean, properties);
            } catch (Exception e) {
                throw new Exception("BeanUtils.populate", e);
            } finally {
                if (multipartHandler != null) {
                    ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
                }
            }            
    7、加入合适的话就把退出属性设置到request里;
        if ((request.getParameter(Globals.CANCEL_PROPERTY) != null)
            || (request.getParameter(Globals.CANCEL_PROPERTY_X) != null)) {
            request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
        }        
18、验证表单输入的正当性。假如有不正当的则return。(一般不用的表单级验证)
    try {
        if (!processValidate(request, response, form, mapping)) {
            return;
        }
    } catch (InvalidCancelException e) {
        ActionForward forward = processException(request, response, e, form, mapping);
        processForwardConfig(request, response, forward);
        return;
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw e;
    }   
19、处理mapping指定的forward 和 include
    if (!processForward(request, response, mapping)) {
        return;
    }
    if (!processInclude(request, response, mapping)) {
        return;
    }   
20、创建或者获取一个Action的实例来处理哀求。
    Action action = processActionCreate(request, response, mapping);   
    1、从mapping里掏出配置的Action类名
        String className = mapping.getType();   
    2、把查找Action实例的动作记入到日志里
        if (log.isDebugEnabled()) {
            log.debug(" Looking for Action instance for class " + className);
        }
    3、在拿到Action实例之前先线程同步synchronized (actions) ,保证只有一个Action实例   
    4、从map里掏出Action返回,(假如有的话),并把结果写入日志
        nstance = (Action) actions.get(className);
        if (instance != null) {
            if (log.isTraceEnabled()) {
                log.trace("  Returning existing Action instance");
            }
            return (instance);
        }   
    5、假如上面的操纵没进行,那说明要新建一个Action实例,把新建实例的动作记实到日志里
        if (log.isTraceEnabled()) {
            log.trace("  Creating new Action instance");
        }        
    6、创建出Action实例,把实例放到map里并返回实例
        try {
                instance = (Action) RequestUtils.applicationInstance(className);
            } catch (Exception e) {
                log.error(getInternal().getMessage("actionCreate",
                        mapping.getPath()), e);
                response.sendError(HttpResponse.SC_INTERNAL_SERVER_ERROR,
                    getInternal().getMessage("actionCreate", mapping.getPath()));
                return (null);
            }
            actions.put(className, instance);
            if (instance.get() == null) {
                instance.set(this.);
            }
        }
        return (instance)        
21、再次判定Action是否创建成功,假如没有则方法直接return
        if (action == null) {
            return;
        }     
22、执行Action的excute方法,获得ActionForward
        ActionForward forward = processActionPerform(request, response, action, form, mapping);
        //其中processActionPerform方法调用了action的excute方法:
        protected ActionForward processActionPerform(HttpRequest request,
            HttpResponse response, Action action, ActionForm form,
            ActionMapping mapping)
            throws IOException, Exception {
            try {
                return (action.execute(mapping, form, request, response));
            } catch (Exception e) {
                return (processException(request, response, e, form, mapping));
            }
        }
    这里也做了一个处理,假如要在执行excute方法之前做一些操纵,就可以笼盖processActionPerform方法。   
23、更具Actionforward进行转发
       processForwardConfig(request, response, forward)   
        1、假如ActionForward为空,则方法直接返回
            if (forward == null) {
                return;
            }         
        2、把接下来处理forward的操纵记实到日志里
            if (log.isDebugEnabled()) {
                log.debug("processForwardConfig(" + forward + ")");
            }        
        3、从mapping里获取forward对应的url,默认用forward的方式转发,假如配了redirect,则用redirect重定向
            String forwardPath = forward.getPath();
            String uri;
            String actionIdPath = RequestUtils.actionIdURL(forward, request, );
            if (actionIdPath != null) {
                forwardPath = actionIdPath;
                ForwardConfig actionIdForward = new ForwardConfig(forward);
                actionIdForward.setPath(actionIdPath);
                forward = actionIdForward;
            }
            if (forwardPath.startsWith("/")) {   
                uri = RequestUtils.forwardURL(request, forward, null);
            } else {
                uri = forwardPath;
            }
            if (forward.getRedirect()) {
                if (uri.startsWith("/")) {
                    uri = request.getContextPath() + uri;
                }
                response.sendRedirect(response.encodeRedirectURL(uri));
            } else {
                doForward(uri, request, response);


回复

使用道具 举报

  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2011-8-1 10:28:03 | 显示全部楼层
    谢谢楼主分享。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2012-6-18 16:23:11 | 显示全部楼层
    看后感悟了点,收下了








    哈尔滨清洗公司http://www.hrbctqx.com
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-20 03:37 , Processed in 0.347014 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表