Search code examples
javajspstrutsindexed-properties

adding new item in indexed property of struts


here is what i want to do: I have an actionForm with an indexed property(a list of districts), I have managed to show and bind all items to form the list districts.

I want to have a jsp page that allow user to add and delete item in that list. The problem is that struts use for example districts[0], districts[1] to bind each item in the list, when i want new item to be added or deleted, does this mean i need to append new 'input' tag with name='districts[last_index]' at the end of the list using javascript for addition and remove input tags for the corresponding index for deletion? is there alternative for this?


Solution

  • Yes. This is the way it works. I have tried managing parameters like this and has worked fine. And, for the deletion, you need not re-index the items. You just need to manage the last_index. For your information:

    If the list contains objects with nested property, you need to implement this. Otherwise, you can just get the items in the list with the listname specified.

    E.g.:

    For List<Integer> integerInputs;, &integerInputs=1&integerInputs=230&integerInputs=332 will work.

    For

    class MyObj {
          String name;
          int id;
          .
          .
          Getters and Setters
          .
          .
    }
    
    List<MyObj> myObjList;
    

    The url to populate the list would be something like &myObjList[0].id=12&myObjList[0].name=testName1&myObjList[1].id=122&myObjList[1].name=testNameOnly