Announcement Announcement Module
Collapse
No announcement yet.
Determination form backing object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Determination form backing object

    In our project, it's possible to add input fields with Javascript. These fields are bound to an object through the name and the trick with LazyList and LazyMap. Everything worked fine, but now there's a problem.

    If you only add 1 object to the ModelAndView and you create the name of the inputfield as HashMapName[X].attribute, it works fine. When you add fields to your page and you submit the form, you can extract the newly created values from the form backing object.

    But now there's a tricky part. We have 2 objects which are added to the ModelAndView (through mav.addObject("name", obj). When I add the input fields with Javascript and create the names, it doesn't work anymore. When I submit the form and try to extract them in the controller, I get an empty list. There are 2 objects, namely User (which represents the user who 's currently logged in) and a Document object.

    Does anyone know how to solve this? Or what the reason could be for this malfunctioning. I don't get any binding errors, the command Object is just empty.

    Controller:
    Code:
            ModelAndView view = new ModelAndView("changeaddress");
            view.addObject("user", user);
            view.addObject("document", doc);
    
            return view;
    Document:
    Code:
    public class ObjectivesDocument
    {
        private Map<Integer, Objective> individualObjectives = LazyMap.decorate(new HashMap<Integer, Objective>(), FactoryUtils.instantiateFactory(Objective.class));
    
        public Map<Integer, Objective> getIndividualObjectives() {
    		return individualObjectives;
    	}
    
    	public void setIndividualObjectives(Map<Integer, Objective> individualObjectives) {
    		this.individualObjectives = individualObjectives;
    	}
    }
    Objective contains a few attributes, description and weight.

    Now in my javascript, this is the name of the input fields:
    var descriptionName = "document.individualObjectives[" + c + "].description";
    var weightRow = "document.individualObjectives[" + c + "].weight";
    C is the new index. So if the list already contains objects, c will be the index of the last object + 1.

    I hope that there's anyone outthere who can help me .

  • #2
    No one? :-/. Seems like a common problem to me. If you add 2 objects to the ModelAndView, what happens to the form backing object. Is it even possible to have 2 form backing objects, or do you have to write a wrapper which contains a HashMap (a solution proposed in this thread).

    Comment


    • #3
      Originally posted by Bjorn121 View Post
      Is it even possible to have 2 form backing objects, or do you have to write a wrapper which contains a HashMap
      You can have as many model attributes as you like. The are just named attributes available for rendering.

      However only one command object should be subject to data binding during a specific form submit. This is done by making the command object an input argument to the form submit method and optionally annotating it with @ModelAttribute to give it a name.

      Comment

      Working...
      X