• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using BigDecimal in ActionForm

 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64964
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to post Struts questions in the Struts forum.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any easier alternative to this like using JSTL tags or custom tags ??

Thanks,
Ram
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic