aspose file tools*
The moose likes Struts and the fly likes Setting dynamic list of different type of objects to a Java form bean in Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Setting dynamic list of different type of objects to a Java form bean in Struts2" Watch "Setting dynamic list of different type of objects to a Java form bean in Struts2" New topic
Author

Setting dynamic list of different type of objects to a Java form bean in Struts2

crazyk crazy
Greenhorn

Joined: Sep 23, 2013
Posts: 4
Scenario:

I am trying to map the JAXB Java object- 'Coverage' to a Java form bean-'CoverageForm'. I have a List -'coverageList' of Coverage objects.In the Coverage object, I have a list of content objects 'cov.getContent()' of type JAXBElement, Limit,Deductible. I need to set the cov.getContent() list values to a form bean-'CoverageForm'

Issue:

cov.getContent() list is dynamic.So for each Coverage object, cov.getContent() can be of size 5 or 3 or anything. How can I set the dynamic list of different objects-'cov.getContent()' to a form bean object-'CoverageForm' ? The below code of Action class do not show the expected result. I have tried to hardcode the index value but it throws ArrayIndexoutOfBoundExeception because in each Coverage object the cov.getContent() can be of size 5 or 3 or anything. If you see I need to set the limit.getFormatInteger() to LowLimit and HighLimit of CoverageForm while looping.How can I achieve this?Please suggest me.

To get an idea of the XML elements structure-'coverageList' structure is-

<Coverage>
<CoverageCd>BI</CoverageCd>
<CoverageDesc>Bodily Injury Liability</CoverageDesc>
<Limit>
<FormatInteger>100000</FormatInteger>
<LimitAppliesToCd>PerPerson</LimitAppliesToCd>
</Limit>
<Limit>
<FormatInteger>300000</FormatInteger>
<LimitAppliesToCd>PerAccident</LimitAppliesToCd>
</Limit>
</Coverage>
<Coverage>
<CoverageCd>PD</CoverageCd>
<CoverageDesc>Property Damage-Single Limit</CoverageDesc>
<Limit>
<FormatInteger>50000</FormatInteger>
<LimitAppliesToCd>PropDam</LimitAppliesToCd>
</Limit>
</Coverage>
<Coverage>
......"CoverageDesc","CoverageCd" are attributes of JAXBElement class.

public class Limit {

@XmlElement(name = "FormatInteger")
protected int formatInteger;
@XmlElement(name = "LimitAppliesToCd")
protected String limitAppliesToCd;
}

public class Deductible {

@XmlElement(name = "FormatInteger")
protected int formatInteger;
@XmlElement(name = "DeductibleTypeCd")
protected String deductibleTypeCd;
}

public class Coverageform {

private int vehId;
private String coverageCd;
private String coverageDesc;
private int lowLimit;
private int highLimit;
private int deducitble;

}

Action class:
for(int i=0; i<vehicleList.size();i++)
{
List coverageList=((Pers) vehicleList.get(i)).getCoverage();

for (int j=0; j<coverageList.size();j++)
{
Coverage cov= (Coverage)coverageList.get(j);

System.err.println("cov--"+cov);


//for( Object o: cov.getContent()){
if( cov.getContent().get(0) instanceof JAXBElement)
{
System.err.println("o is JAXBElement");
JAXBElement jaxbele= (JAXBElement)cov.getContent().get(0);
coverageForm.setCoverageCd(jaxbele.getValue().toString());
System.err.println("coverageForm.getCoverageCd()--"+coverageForm.getCoverageCd());
}
if( cov.getContent().size()>1 && cov.getContent().get(1) instanceof JAXBElement)
{
JAXBElement jaxbele1= (JAXBElement)cov.getContent().get(1);
coverageForm.setCoverageDesc(jaxbele1.getValue().toString());
System.err.println("coverageForm.getCoverageDesc()--"+coverageForm.getCoverageDesc());
}
if (cov.getContent().size()>2 && cov.getContent().get(2) instanceof Limit)
{
System.err.println("o is Limit");
Limit limit= (Limit)cov.getContent().get(2);
coverageForm.setLowLimit(limit.getFormatInteger());
System.err.println("coverageForm.getLowLimit()--"+coverageForm.getLowLimit());
}
if ( cov.getContent().size()==4 && cov.getContent().get(3) instanceof Limit)
{
Limit limit1= (Limit)cov.getContent().get(3);
coverageForm.setHighLimit(limit1.getFormatInteger());
System.err.println("coverageForm.getHighLimit()--"+coverageForm.getHighLimit());
}
if (cov.getContent().size()==5 && cov.getContent().get(4) instanceof Deductible)
{
System.err.println("o is Deductible");
Deductible ded= (Deductible)cov.getContent().get(4);
coverageForm.setDeducitble(ded.getFormatInteger());
System.err.println("coverageForm.getDeducitble()--"+coverageForm.getDeducitble());
}

coverageFormList.add(coverageForm);
System.err.println("coverageFormList.size--"+coverageFormList.size());

}
crazyk crazy
Greenhorn

Joined: Sep 23, 2013
Posts: 4
Sorry! I did not get you and I do not find this link to be working-purple mooseages
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting dynamic list of different type of objects to a Java form bean in Struts2