java - Spring form - mapping list of extended classes -


i have form contains multiple items:

<form>     <input name="items[0].title">     <input name="items[1].title">     <input name="items[2].title"> </form> 

and classes:

class form {     private list<item> items;      public list<item> getitems()     {         return items;     }     public void setitems(list<item> items)     {         this.items = items;     } }  class item {     private string title;      public string gettitle()     {         return title;     }     public void settitle(string title)     {         this.title= title;     }      public string print()     {         system.out.println("title: " + title);     } } 

and works fine. can use this:

@requestmapping(value="/add", method=requestmethod.post)     public modelandview addsubmit(form form) {     form.getitems().get(1).print();      ... } 

what add new type of item. example bigitem has additional field size:

class bigitem {     private string title;     private string size;      public string gettitle()     {         return title;     }     public void settitle(string title)     {         this.title= title;     }     public string getsize()     {         return size;     }     public void setsize(string size)     {         this.size= size;     }      public string print()     {         system.out.println("title: " + title);         system.out.println("size: " + size);     } } 

this prototype , simplified code. expect new quite complicated item types in future , need prepared that. idea create class item common fields , methods , extend anytime need new item type (bigitem example). don't know how map it. let's assume form looks this:

<form>     <input name="items[0].title">     <input name="items[1].title">     <input name="items[1].size">     <input name="items[2].title"> </form> 

and class bigitem extending item:

class bigitem extends item {     private string size;      public string getsize()     {         return size;     }     public void setsize(string size)     {         this.size= size;     }      public string print()     {         system.out.println("title: " + title);         system.out.println("size: " + size);     } } 

how can map inside controller method? call method print() bigitem class instead of item.

form.getitems().get(1).print(); 

when form returns spring, spring have no idea whether item[1] item or bigitem. won't work. best bet use class, formitem, super set of attributes of item, bigitem, biggeritem ... etc. use controller convert formitem appropriate item house input fields or process them. have careful in designing items definite item can identified presence of input attributes.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -