• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using BigDecimal in ActionForm

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to post Struts questions in the Struts forum.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can display a BigDecimal using either a <bean:write> or a <c:out> 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic