letsbedecent
asked on
struts logic iterate.
Hello,
I have three classes
class car{
private String year;
}
class toyota{
private String mileage;
}
class Honda {
private String horsePower;
}
now, from an action class i get a list of objects some of type Honda and some of type Toyota.
using logic iterate(or anything else) i have to do the following :
<logic:iterate list >
(If Honda then) Year : 1998
horsePower : 220
(if Toyota then) Year : 2000
mileage : 30 MPG
<logic:iterate>
The problem is how to identify the "type" of the object i am iterating over... because i have both objects in the same list...
I have three classes
class car{
private String year;
}
class toyota{
private String mileage;
}
class Honda {
private String horsePower;
}
now, from an action class i get a list of objects some of type Honda and some of type Toyota.
using logic iterate(or anything else) i have to do the following :
<logic:iterate list >
(If Honda then) Year : 1998
horsePower : 220
(if Toyota then) Year : 2000
mileage : 30 MPG
<logic:iterate>
The problem is how to identify the "type" of the object i am iterating over... because i have both objects in the same list...
Two ways to do:
(1).
<logic:iterate id="element" >
<%if(element instanceof Honda){%>
Year : 1998
horsePower : 220
<%}%>
<%if(element instanceof Toyota ){%>
Year : 2000
mileage : 30 MPG
<%}%>
<logic:iterate>
<logic:iterate id="element" >
<%if(element instanceof Honda){%>
Year : 1998
horsePower : 220
<%}%>
<%if(element instanceof Toyota ){%>
Year : 2000
mileage : 30 MPG
<%}%>
<logic:iterate>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, the better one to do is that you can define a getInformation instead of getType. All information goes to this method. So your code will be like :
<logic:iterate id="element" ...>
<bean:write name="element" property="information" />
<logic:iterate>
:)
<logic:iterate id="element" ...>
<bean:write name="element" property="information" />
<logic:iterate>
:)