aspose file tools*
The moose likes Struts and the fly likes Designing an Airline ActionForm with a Collection of Aircraft Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Designing an Airline ActionForm with a Collection of Aircraft" Watch "Designing an Airline ActionForm with a Collection of Aircraft" New topic
Author

Designing an Airline ActionForm with a Collection of Aircraft

Troy Harvey
Greenhorn

Joined: Mar 01, 2007
Posts: 6


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[1].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 ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
Troy Harvey
Greenhorn

Joined: Mar 01, 2007
Posts: 6
Thank you!

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.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.
Troy Harvey
Greenhorn

Joined: Mar 01, 2007
Posts: 6
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[].


Error

[ March 02, 2007: Message edited by: Troy Harvey ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.
 
wood burning stoves
 
subject: Designing an Airline ActionForm with a Collection of Aircraft