it should work with just..

<form-property name="pmiRanges" type="java.util.ArrayList" />

theForm.set("pmiRanges", (ArrayList) currentRates.getPmiRanges());

You dont need session.setAttribute.. and this can lead to you thinking that's life's great when its not..

and then ...

ArrayList rateList = (ArrayList) theForm.get("pmiRanges");

rates.setPmiRanges((List) rateList);


I've been doing this okay using ArrayList so you could try that.I guess the class cast exception make have been thrown when you tried seting pmiRanges with an Object rather than list its stored as an object like with so many things in java.


Cheers Mark

On Monday, August 25, 2003, at 03:26 PM, Joel Wickard wrote:

Mark Lowe wrote:

That's more like it. Are all you fields of type string?

Can I see the before and after actions?

You sure can.


The fields inside the PMIRateDTO ( the object that makes up each element of the List ) are all of type String.

Here is the before action:

/*
* PreloadRateEditor.java
*/
package com.dbo.struts.actions;

import java.text.NumberFormat;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dbo.bd.RateStoreBD;
import com.dbo.dto.RateDTO;
import com.dbo.struts.forms.RateEditorForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

/**
* Struts Action to Load current rates from xml, then preload a Struts Form Bean before sending request
* to RateEditor.jsp to be displayed.
*
* === XDoclet Configuration Tags ===
*
* @struts.action
* name="persistRateInfo"
* path="/preloadRateEditor"
* scope="session"
* input="RateEditor.jsp"
* validate="false"
* parameter=""
*
* @struts.action-forward
* name="failure"
* path="/main.jsp"
*
* @struts.action-forward
* name="success"
* path="/RateEditor.jsp"
*
*
*/
public class PreloadRateEditorAction extends Action
{
// Obtain log instance for logging.
private Log log = LogFactory.getLog( this.getClass( ) );
/* Our implementation of Action.execute(); */
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
log.debug( "PreloadRateEditorAction.execute() called." );
/* Grab an instance of RateStoreBD() to retrieve the current rates */
RateStoreBD store = new RateStoreBD();
/* Load the rates from storage */
RateDTO currentRates = store.loadRates();
// debug info
log.debug( "RateDTO retrieved from storage: " + currentRates );
/* Here we cast the form we were passed into an instance of DynaActionForm, for easier
* manipulation. */
DynaActionForm loadForm = (DynaActionForm) form;


/* Obtain instance of Locale for use in NumberFormat. Used to format our doubles to strings */
Locale loc = Locale.US;
NumberFormat fmt = NumberFormat.getInstance(loc);
//get information from rates DTO and populate the form bean with them.
loadForm.set( "hazardInsRate", fmt.format( currentRates.getHazardInsRate() ) );
loadForm.set( "dealOneIR", fmt.format( currentRates.getDealOneIR( ) ) );
loadForm.set( "dealOneTerm", Integer.toString( currentRates.getDealOneTerm( ) ) );
loadForm.set( "dealOnePercentageDown", fmt.format( currentRates.getDealOnePercentageDown() ) );
loadForm.set( "dealTwoIR", fmt.format( currentRates.getDealTwoIR( ) ) );
loadForm.set( "dealTwoTerm", Integer.toString(currentRates.getDealTwoTerm( ) ) );
loadForm.set( "dealTwoPercentageDown", fmt.format( currentRates.getDealTwoPercentageDown() ) );
loadForm.set( "dealThreeIR", fmt.format( currentRates.getDealThreeIR( ) ) );
loadForm.set("dealThreeTerm", Integer.toString(currentRates.getDealThreeTerm( ) ) );
loadForm.set( "dealThreePercentageDown", fmt.format( currentRates.getDealThreePercentageDown() ) );
loadForm.set( "pmiRanges", currentRates.getPmiRanges() );
/* Store the form bean in request scope so that the <html> custom taglib can access it */
//request.setAttribute( "persistRateInfo", currentRates );
request.getSession().setAttribute( "persistRateInfo", currentRates );
/* set the forward to be successful */
String myForward = "success";
/* return forward */
return mapping.findForward( myForward );
}
}


Here is the action that the form is posted to:

/*
*  PersistRateInfo.java
*/
package com.dbo.struts.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dbo.bd.RateStoreBD;
import com.dbo.dto.RateDTO;
import com.dbo.struts.forms.RateEditorForm;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

/**
* Action which obtains an instance of RateStoreBD() and saves an instance of the current mortgage rates
* for use in the application.
*
* === XDoclet Struts Action Config Tags ===
*
* @struts.action
* name="persistRateInfo"
* path="/persistRateInfo"
* scope="session"
* input="RateEditor.jsp"
* validate="false"
* parameter=""
* * we forward control back to RateEditor.jsp upon success or failure.
* @struts.action-forward
* name="done"
* path="/RateEditor.jsp"
*/
public class PersistRateInfo extends Action
{
/* Load instance of log */
private Log log = LogFactory.getLog( this.getClass( ) );
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
log.debug( "PersistRateInfo.execute() called." );
/* Instantiate instance of RateDTO to store information submitted in form. */
RateDTO rates = new RateDTO();
/* Instantiate instance of RateStoreBD to use to store our values. */
RateStoreBD store = new RateStoreBD();
/* Cast our form to DynaActionForm for easier operation. (keeps from having to use beanutils)*/
DynaActionForm rateForm = (DynaActionForm) form;
/* populate RateDTO() with form values */
rates.setHazardInsRate( Double.parseDouble( (String) rateForm.get( "hazardInsRate" ) ) );
rates.setDealOneIR( Double.parseDouble( (String) rateForm.get( "dealOneIR" ) ) );
rates.setDealOneTerm( Integer.parseInt( (String) rateForm.get( "dealOneTerm" ) ) );
rates.setDealOnePercentageDown( Double.parseDouble( (String) rateForm.get( "dealOnePercentageDown" ) ) );
rates.setDealTwoIR( Double.parseDouble( (String) rateForm.get( "dealTwoIR" ) ) );
rates.setDealTwoTerm( Integer.parseInt( (String) rateForm.get( "dealTwoTerm" ) ) );
rates.setDealTwoPercentageDown( Double.parseDouble( (String) rateForm.get( "dealTwoPercentageDown" ) ) );
rates.setDealThreeIR( Double.parseDouble( (String) rateForm.get( "dealThreeIR" ) ) );
rates.setDealThreeTerm( Integer.parseInt( (String) rateForm.get( "dealThreeTerm" ) ) );
rates.setDealThreePercentageDown( Double.parseDouble( (String) rateForm.get( "dealThreePercentageDown" ) ) );
rates.setPmiRanges( rateForm.get( "pmiRanges" ) );
// debug info
log.debug( "RatesDTO bieng passed to form: " + rates );
/* store rate values */
store.storeRates( rates );
/* Set forward */
String myForward = "done";
/* return forward */
return mapping.findForward( myForward );
}
}


In the interest of completeness here again is the DynaActionForm bean declaration:

<form-bean
   name="persistRateInfo"
   type="org.apache.struts.action.DynaActionForm">

Specify the dynamic properties of the form
<form-property name="hazardInsRate" type="java.lang.String"/>
<form-property name="dealOneTerm" type="java.lang.String"/>
<form-property name="dealOneIR" type="java.lang.String"/>
<form-property name="dealOnePercentageDown" type="java.lang.String"/>
<form-property name="dealTwoTerm" type="java.lang.String"/>
<form-property name="dealTwoIR" type="java.lang.String"/>
<form-property name="dealTwoPercentageDown" type="java.lang.String"/>
<form-property name="dealThreeTerm" type="java.lang.String"/>
<form-property name="dealThreeIR" type="java.lang.String"/>
<form-property name="dealThreePercentageDown" type="java.lang.String"/>
<form-property name="pmiRanges" type="java.util.List" />
</form-bean>


And the snippet from the jsp that uses the bean:

<logic:iterate id="pmiRanges" name="persistRateInfo" property="pmiRanges">
<tr>
<td style="font-family: sans; font-size: 12;" width="100">Low: </td>
<td style="font-family: sans; font-size: 12;">
<html:text name="pmiRanges" property="low" indexed="true" size="5"/>
</td>


<td style="font-family: sans; font-size: 12;" width="100">High: </td>
<td style="font-family: sans; font-size: 12;">
<html:text name="pmiRanges" property="high" indexed="true" size="5"/>
</td>


<td style="font-family: sans; font-size: 12;" width="100">Rate: </td>
<td style="font-family: sans; font-size: 12;">
<html:text name="pmiRanges" property="rate" indexed="true" size="5"/>
</td>
</tr>
</logic:iterate>




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to