示例#1
0
  protected void updateModel(Object model, String prefix) throws Exception {
    Class<?> clasz = model.getClass();
    try {
      Method method = clasz.getMethod("isProxy");
      Boolean result = (Boolean) method.invoke(model);
      if (result == true) {
        clasz = clasz.getSuperclass();
      }

    } catch (Exception ex) {
    }

    for (Field f : clasz.getDeclaredFields()) {
      BindingContext ctx = new BindingContext();
      ctx.setParameterName(f.getName());
      ctx.setParameterType(f.getType());
      ctx.setRequest(request);
      ctx.setPrefix(prefix);

      DataBinder binder = DataBinders.getDataBinder(f.getType());
      if (binder != null) {
        String pname = ctx.getParameterName();
        if (!ctx.getPrefix().equals("")) {
          pname = ctx.getPrefix() + "." + pname;
        }
        if (request.getParameterMap().containsKey(pname)) {
          f.setAccessible(true);
          f.set(model, binder.bind(ctx));
        }
      } else {
        BindingContext bc = new BindingContext();
        bc.setParameterName(f.getName());
        bc.setParameterType(f.getType());
        bc.setRequest(ctx.getRequest());
        if (ctx.getPrefix().equals("")) {
          bc.setPrefix(f.getName());
        } else {
          bc.setPrefix(ctx.getPrefix() + "." + f.getName());
        }

        Object value = new ObjectBinder().bind(bc);
        if (value != null) {
          f.setAccessible(true);
          Object obj = f.get(model);
          if (obj != null) {
            updateModel(obj, bc.getPrefix());
            f.set(model, obj);
          } else {
            f.set(model, value);
          }
        }
      }
    }
  }
示例#2
0
  protected Object invoke() throws Exception {
    Class<?>[] types = getActionMethod().getParameterTypes();
    Annotation[][] annotations = getActionMethod().getParameterAnnotations();
    Object[] parameters = new Object[types.length];

    if (types.length > 0) {
      Paranamer paranamer = new AdaptiveParanamer();
      String[] names = paranamer.lookupParameterNames(getActionMethod());
      for (int i = 0; i < parameters.length; i++) {
        BindingContext ctx = new BindingContext();
        ctx.setParameterName(names[i]);
        ctx.setParameterType(types[i]);
        ctx.setRequest(getRequest());
        Annotation[] ann = annotations[i];
        if (ann.length > 0 && ann[0] instanceof Bind) {
          Bind bind = (Bind) ann[0];
          ctx.setPrefix(bind.prefix());
        } else {
          ctx.setPrefix("");
        }
        DataBinder binder = DataBinders.getDataBinder(types[i]);
        if (binder == null) {
          binder = new ObjectBinder();
        }
        parameters[i] = binder.bind(ctx);
      }
    }

    Object result = getActionMethod().invoke(this, parameters);

    if (getSession().getAttribute(Flash.FLASH_KEY) == null) {
      if (getFlash().getAttributes().size() > 0) {
        for (Entry<String, Object> attr : getFlash().getAttributes().entrySet()) {
          getRequest().setAttribute(attr.getKey(), attr.getValue());
        }
        getSession().setAttribute(Flash.FLASH_KEY, getFlash());
      }
    } else {
      if (getFlash().getAttributes().size() > 0) {
        for (Entry<String, Object> attr : getFlash().getAttributes().entrySet()) {
          getRequest().setAttribute(attr.getKey(), attr.getValue());
        }
      }
      getFlash().sweep();
      if (getFlash().getAttributes().size() == 0) {
        getSession().removeAttribute(Flash.FLASH_KEY);
      }
    }

    return result;
  }