Results 1 to 8 of 8

Thread: alter command before binding

  1. #1
    Join Date
    Dec 2006
    Posts
    6

    Default alter command before binding

    Is there a way to alter the command object after submission of the form but before binding? onBind() is to late in other words.

    I tried in referenceData() and that worked to show the form. After submission still the same problem.

    Maybe I'm looking the wrong way. The problem is that I have a collection . On creation of the instance this is empty. So the form to create one gives an error when trying to submit data because of size 0. If I could alter the command object (that is initialising the collection) it would be solved.

    === error ===

    org.springframework.beans.InvalidPropertyException : Invalid property 'attributeValues[0]' of bean class [company.project.model.Docpart]:
    Cannot get element with index 0 from Set of size 0, accessed using property path 'attributeValues[0]'
    Last edited by cappelleh; Dec 2nd, 2006 at 02:25 PM.

  2. #2

    Default

    You could use the Commons Collections library. There are two decorator classes, GrowthList and LazyList, that will automatically grow the list when you try to access it beyond its bounds.

    They're very useful for submitting a collection to bind to an object that doesn't know the length of the collection beforehand.

    Set the lists in your formBackingObject with these decorators like so:

    Code:
    List<PhoneNumber> phones = GrowthList.decorate(LazyList.decorate(
    				new ArrayList(),
    		FactoryUtils.instantiateFactory(PhoneNumber.class)));

  3. #3
    Join Date
    Dec 2006
    Posts
    6

    Default

    Quote Originally Posted by brianwilliams42 View Post
    You could use the Commons Collections library. There are two decorator classes, GrowthList and LazyList, that will automatically grow the list when you try to access it beyond its bounds.
    Thx for the reply is a good thing to know. But it won't solve my problem. This will only adjust size of list. I really need to put some objects in there. Because I work on the properties of these objects.

    Currently I'm binding programmatically. Youst look into request and using onBindAndValidate(). Still the automatic binding would be a neater solution.

  4. #4

    Default

    Ah, hmm, the way I understood those libraries was that if you had a list with no elements, and you did list.get[0].setProperty("blah") it would actually call the default no-arg constructor to create an object to return from the get[0] part of the call, then the object would be there for you to set the property on. The promise is you'll never get an IndexOutOfBoundsException on those lists.

    So, it does that creation of an object quietly, or else I'm in trouble too
    Last edited by brianwilliams42; Dec 5th, 2006 at 01:51 PM.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Why not just initialize the formObjects list on creation with some empty objects and after submit remove those. (That is how we do it).

    If you are binding on the collection with 'attributeValues[0]' you must have some loop or either hardcoded your jsp with those values.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Dec 2006
    Posts
    6

    Default

    Quote Originally Posted by mdeinum View Post
    Why not just initialize the formObjects list on creation with some empty objects and after submit remove those. (That is how we do it).

    If you are binding on the collection with 'attributeValues[0]' you must have some loop or either hardcoded your jsp with those values.
    And which method do you override to achieve this? Doe you have some sample code? That's what I'm trying to do but isn't working. On validation the list is empty again (it works on show the form however).

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    We normally do this in the formBackingObject method. Something like this. For the objects to still be there make sure you have turned the sessionForm property to true. Either by configuration or by using setSessionForm in the constructor of your controller.
    Code:
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        YourCommandObject command = new YourCommandObject();
        YourObjectInList objInList1 = new YourObjectInList();
        YourObjectInList objInList2 = new YourObjectInList();
        YourObjectInList objInList3 = new YourObjectInList();
        command.addObjectToList(objInList1);
        command.addObjectToList(objInList2);
        command.addObjectToList(objInList3);
        return command;
    }

    Then in your onSubmit or onBindAndValidate remove the rows you don't need.
    Code:
    protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) {
        YourCommandObject object = (YourCommandObject) command;
        Iterator it = object.getListWithObjects().iterator;
        while (it.hasNext()) {
            YourObjectInList listObject = (YourObjectInList) it.next();
            if (listObject.checkSomeValue() ) {
                it.remove();
            }
        }
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Dec 2006
    Posts
    6

    Thumbs up

    Quote Originally Posted by mdeinum View Post
    For the objects to still be there make sure you have turned the sessionForm property to true.
    Thx a lot, that's exactly what I needed to know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •