Say I have an Airline bean with a Collection Aircraft like this:
Now I'm designing an ActionForm for the Airline like this:
Should the AirlineActionForm.aircraft property be a Collection of Aircraft objects or a Collection of AircraftActionForm objects?
If I use Aircraft objects (which has properties that aren't Strings), won't I get an error when the user types in an invalid date for one of the fields corresponding to an AirlineActionForm.aircraft.purchaseDate?
Is it possible to have a Collection of ActionForms inside an ActionForm? Are there any pitfalls to this? [ March 01, 2007: Message edited by: Troy Harvey ]
Only the bean directly associated with the Action should extend ActionForm. All others should be plain old java objects (POJOs).
Regarding your issue with the dates: If they are only being displayed and not input, it's fine to leave them as they are. The <bean:write> tag has a format attribute that will allow you to format the date when it's displayed.
If you are inputting the dates, then you are right: Struts will have a problem with a field of type java.util.Date. There are a couple of different options to deal with this. One would be to create a new POJO that treats the date as a String, and then when the object is being passed back to the model layer, map the new POJO to the one used in your model. This is the best solution if you're using Sruts Validation.
Another solution is to create a second getter/setter pair that converts the date to and from a String format. This works in a situation where you've written your own validation, but not if you're using Struts validation.
I am inputting dates, so I will use that suggestion.
So say I am using an Aircraft POJO with all String properties (as suggested). When my user enters a date and clicks submit, how will Struts know to build Aircraft POJOs into that AirlineActionForm.aircraft Collection?
I assume this happens inside RequestUtils.populate(). I think I know how to prepare the html inputs in the jsp. I don't know how to write my ActionForm getters and setters properly for a Collection of POJOs.
Joined: Feb 15, 2005
Now the real fun begins!
In order to do this, you will need to use "indexed properties". This link is the best place to start learning how to use them. Look closely at the example and follow the same pattern.
Joined: Mar 01, 2007
That's great thanks! So now I want to use a Collection of Aircraft vanilla beans in the ActionForm:
The problem I have happens as I'm copying data from my AirlineDTO into my AirlineActionForm. It breaks inside my Action in BeanUtils.copyProperties(). Are there issues with using copyProperties to copy objects from a List to an array?
My problems seems to be going from AirlineDTO.aircraft (of type List) to AirlineActionForm.aircraft.
[ March 02, 2007: Message edited by: Troy Harvey ]
Joined: Feb 15, 2005
The BeanUtils.copyProperties method only works if the properties are of the same or at least compatible types. It does not convert from date to String or String to date. You'll have to write code to do that yourself. I'd suggest using the java.text.SimpleDateFormat class.
subject: Designing an Airline ActionForm with a Collection of Aircraft