Announcement Announcement Module
Collapse
No announcement yet.
Command in SimpleFormController not being replaced. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Command in SimpleFormController not being replaced.

    It appears that the command in the model is not being replaced and I'm not sure why...

    I'm doing a basic check to see if the request is to enter a new entity or edit an existing entity. Usually, I've just updated the command in place and have had not problems, but in a recent useage I replaced the command with a new command retreived from the database. I update the command in the model in the ModelAndView, but

    Code:
        @Override
        @SuppressWarnings("unchecked")
        protected final ModelAndView showForm(
                HttpServletRequest req, 
                HttpServletResponse res, 
                BindException errors, 
                Map controlModel) throws Exception {
            
            ModelAndView modelAndView = super.showForm(
                    req, 
                    res, 
                    errors, 
                    controlModel);
            
            U user = getUserEntity(req);
            C command = (C)modelAndView.getModel().get("command");
            
    // HERE the model shows the original command object
            Set< String > keys = modelAndView.getModel().keySet();
            for(String s : keys) {
                LOG.info("s == "+ modelAndView.getModel().get(s));
            }
            
            if(!isFormSubmission(req)) {
                
                Integer id = getEntityId(req);
                if(id != null && id > 0) {
                    
    // Here I replace the command with a new instance and override the old command in the model
                    C newCommand = prepareUpdateCommand(
                            req,
                            res,
                            id,
                            command,
                            user);
                    modelAndView.getModel().put("command", newCommand);
    
                } else {
                    
                    C newCommand = prepareNewCommand(
                            req,
                            res,
                            command,
                            user);
                    modelAndView.getModel().put("command", newCommand);
    
                }
            } 
            
    // HERE the model reflects that the command object has been replaced.
            keys = modelAndView.getModel().keySet();
            for(String s : keys) {
                LOG.info("s == "+ modelAndView.getModel().get(s));
            }
            
            return modelAndView;
        }
    All the above code is working the way I would expect for an "update" of an entity.

    When I view the entity in my form:form, however, the original command not newCommand is being used. I've been able to verify this by viewing the toString() using c:out in the form and the hashcode is of the "command" object not the "newCommand" object.

    Anyone have any ideas?

  • #2
    Originally posted by SnapShot View Post
    Anyone have any ideas?
    That's not how it is supposed to work. Have a look at formBackingObject() instead. You are to late in the form processing flow.

    Joerg

    Comment


    • #3
      Thank you for giving me everything I need to track this down. At least I know I'm not going crazy...

      Comment


      • #4
        same problem

        The use case I have is that a person is entering data one at a time, If they enter the same "person" twice I want to catch that and send them back to the data entry form with a message saying "you already entered that person".
        The catch is that I need the form pre populated with the original entry (the idea being that they really need to edit that person not enter a new one with the same id).
        Reading this forum it looks like I can detect that I have a duplicate entry but all I can do is redirerct back to the same url then when I am back in the controller I can use the
        protected Object formBackingObject(HttpServletRequest arg0) and pull up that earlier entered object and use that.
        It looks like I need to poke something in the request to signal that this isn't an empty object that I want but rather look for the duplicate in the session.

        Is this the only way to solve this problem? It doesn't seem very clean.

        Comment


        • #5
          I just tried out the solution I outlined above and it doesn't work. The issue seems to be that I can get the new object by submitting back to the page or I can get the error text saying that this is a previously entered record but not both.
          for the time being I am just putting up the error but leaving the second data entry in place.
          Is there anyway to replace the command object before calling showForm(...)?

          Comment

          Working...
          X