wood burning stoves 2.0*
The moose likes Struts and the fly likes Using BigDecimal in ActionForm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using BigDecimal in ActionForm" Watch "Using BigDecimal in ActionForm" New topic
Author

Using BigDecimal in ActionForm

Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
Can we use BigDecimal attributes in actionForm and get the user inputs for that from a JSP.
I tried it and am getting IllegalArgumentException
<<
[10/8/06 14:26:42:000 CDT] 6fb22775 WebGroup E SRVE0026E: [Servlet Error]-[BeanUtils.populate]: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>

The JSP looks like this..

Test BigDecimal<html:text property="testBigDecimal"></html:text>


The actionForm looks like this

public class MyForm extends ActionForm {

BigDecimal testBigDecimal = null;
/**
* @return
*/
public BigDecimal getTestBigDecimal() {
return testBigDecimal;
}

/**
* @param decimal
*/
public void setTestBigDecimal(BigDecimal dec) {

testBigDecimal = dec;
}

}

Am I missing something. My lead wants all dollar values in our business objects to be a BigDecimal, that is why I am using BigDecimal instead of the primitives.

Please advice if it is a NO-NO to use objects attributes in JSP's. Is there any alternative for using them ??

Regards,
Ram


Cheers,
Ram
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Please be sure to post Struts questions in the Struts forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You can store the property value as a BigDecimal if you want, but you must have a getter and setter that accepts a String. Example:

private BigDecimal amount;

public BigDecimal getAmount() { return this.amount;}
public void setAmount(BigDecimal amount) {this.amount = amount; }

public String getFormattedAmount() { return amount.toString(); }
public void setFormattedAmount(String amtString) { amount = new BigDecimal(amtString); }

In your JSP you will refer to the property as formattedAmount.

This method does present some problems: If an invalid amount is passed in, the setter is going to throw a NumberFormatException.

I believe the best solution is to store the property as a String in the ActionForm bean and only convert it to a BigDecimal in some Model object once it's passed validation.


Merrill
Consultant, Sima Solutions
Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
Is there any easier alternative to this like using JSTL tags or custom tags ??

Thanks,
Ram
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You can display a BigDecimal using either a <bean:write> or a <cut> tag, but if you want to input the value, there is no easier way than the way I have already shown you.
[ October 09, 2006: Message edited by: Merrill Higginson ]
 
jQuery in Action, 2nd edition
 
subject: Using BigDecimal in ActionForm
 
Similar Threads
Collections in Struts jsp
displaying data from arrays with html:text
Mapbacked forms
pdk-struts-html:select
Cant find getter method on formbean