zoukankan      html  css  js  c++  java
  • Struts中Action里对Form的运用理解

    public ActionForward custDetail(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) {
            String forward 
    = "custDetail";
            CustInforForm custInforForm 
    = (CustInforForm) form;
            
    try {
                CustomerInfor customerInfor 
    = custInforForm.getCustomerInfor();
                
    if (!customerInfor.getCustomerId().equals("")) {
                    custInforForm.setCustomerInfor(customerInfor
                            .get(customerInfor.getCustomerId()));
                    custInforForm.setCheckboxSelect(customerInfor.getCustomerXxlx()
                            .split(
    ","));//这句中customerInfor.getCustomerXxlx为空
                }
            } 
    catch (ObjectPersistentException e) {
                logger.error(e.getMessage(), e);
                ActionErrors errors 
    = new ActionErrors();
                errors.add(ActionMessages.GLOBAL_MESSAGE, 
    new ActionError("error",
                        
    "查询失败:" + e.getMessage()));
                saveErrors(request, errors);
            }
            
    return mapping.findForward(forward);
        }


    原因:CustomerInfor customerInfor = custInforForm.getCustomerInfor()会生成一个对象,并将对象地址赋给CustomerInfor类中private接口类型变量(假设为A),同时将A作为函数返回值赋给customerInfor,此时A和customerInfor同时指向这个对象。
    custInforForm.setCustomerInfor(customerInfor.get(customerInfor.getCustomerId()));生成另一个对象,这个对象里各属性都有值,并将对象地址作为参数赋给Form里面的私有变量A,此时customerInfor仍然指向原来的对象,所以此时customerInfor里面没有CustomerXxlx的值

    根据这个原理上面代码customerInfor.getCustomerXxlx()应该改为custInforForm.getCustomerInfor().getCustomerXxlx();才能正常运行。或者把有属性值的对象重新赋给customerInfor.如下代码:

    代码
        public ActionForward custDetail(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) {
            String forward 
    = "custDetail";
            CustInforForm custInforForm 
    = (CustInforForm) form;
            
    try {
                CustomerInfor customerInfor 
    = custInforForm.getCustomerInfor();
                
    if (!customerInfor.getCustomerId().equals("")) {
                    customerInfor 
    = customerInfor.get(customerInfor.getCustomerId());
                    custInforForm.setCustomerInfor(customerInfor);
                    custInforForm.setCheckboxSelect(customerInfor.getCustomerXxlx().split(
    ","));
                }
                
    // 在这里进行一条记录的查询操作!
            } catch (ObjectPersistentException e) {
                logger.error(e.getMessage(), e);
                ActionErrors errors 
    = new ActionErrors();
                errors.add(ActionMessages.GLOBAL_MESSAGE, 
    new ActionError("error",
                        
    "查询失败:" + e.getMessage()));
                saveErrors(request, errors);
            }
            
    return mapping.findForward(forward);
        }


     

  • 相关阅读:
    转载:疯狂的XML扩展:GML、SVG、VML
    HDU 4274 Spy's Work [DFS]
    HDU 4279 Number [数学?]
    HDU 4276 The Ghost Blows Light [树形背包DP]
    HDU 3271 SNIBB [数位DP]
    HDU 4280 Island Transport [平面图网络流]
    HDU 4278 Faulty Odometer [进制转换]
    HDU 3058 Generator [AC自动机+期望DP]
    HDU 4277 USACO ORZ [状态压缩+枚举]
    HDU 4282 A very hard mathematic problem [枚举]
  • 原文地址:https://www.cnblogs.com/xryyforver/p/1803807.html
Copyright © 2011-2022 走看看