I have 2 class like below,
public class CheckInBean implements Serializable{
private String user;
private Date checkInDate;
private List<ItemBean> items;
..followed by getters and setters
public class ItemBean implements Serializable{
private static final long serialVersionUID = 1L;
private int itemId;
private String barcode;
private String description;
private int minimalCount;
private int initialCount;
private int actualCount;
private String location;
... followed by getters and setters
I have a jsp page using spring form tags.
I need to enter date and multiple ItemBean values (more than one on the same page) for List. I m not sure how to do it with i just read somewhere in stackoverflow to use Propertyeditor , but i m not sure if it suits my requirement. Please suggest.
I m using Spring 3.
Thanx
Well, what happends there is that if you submit your form with data for one or more ItemBeans, Spring MVC will try to map those values to EXISTING ItemBean entities. If your list of ItemBeans inside CheckInBean instance is null/empty Spring MVC will throw an error. There’s multiple things you can do to prevent this:
Make your own implementation of
java.util.List(you can overrideArrayListfor example) and change theget(int position)method such that when it’s called for a certain position and normally that position would return null, you first make a new instance, place it at that position in the (super) List and then return it. This is kind of a hack but it’s good if you can have a large interval of ItemBean instances (like you might need 3 or you might need 400), as it will only create the needed instances and not over-pollute the memory.You can prepopulate the list of ItemBeans inside CheckInBean with a predefined number of empty items which is the maximum number of ItemBean instances that the user can add to that bean using your form. This is less of a hack than the above option, but it tends to be wasteful memory-wise. It would be ok if you have a small maximum number of ItemBeans that can be added to CheckInBean .