File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How to Avoid Default Values for Double or Any Wrapper Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to Avoid Default Values for Double or Any Wrapper Class" Watch "How to Avoid Default Values for Double or Any Wrapper Class" New topic
Author

How to Avoid Default Values for Double or Any Wrapper Class

Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi Friends,

In my Struts Application, In the Transfer Object Im using the Wrapper Class say Integer,Double .. for the attributes.Im getting the default values 0 for Integer and 0.0 for Double in my textfield.How can i avoid these default values and i need to show just the empty text field.

Thanks in Advance

Cheers,
Prasath


Thanks,
Prasath

SCJP1.4, SCWCD
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you're displaying static text on the page, just use the formatKey attribute of the <bean:write> tag. This specifies the name of an entry in your ApplicationResources.properties file that contains a mask for how you want to display the value. Here's an example:

ApplicationResources.properties
zerosuppress=###.##

JSP
<bean:write name="myForm" property="myDouble" formatKey="zerosupress" />


If this is being displayed in an <html:text> box, It is strongly recommend that you not use data types other than string for text input. This is mainly because it makes validation messy.

What you would need to do in this case is create a separate getter (e.g. getFormattedAmount) on your ActionForm that displays the double value as a String formatted the way you want. You also need to create a setter (e.g. setFormattedAmount) that accepts a String as input and converts it to a Double.


Merrill
Consultant, Sima Solutions
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Dear Merrill Higginson,

Im using <html:text/> tag only.

Here the piece of code how Im using in my application:

In my ActionForm
*****************************************************
Class MyForm {

private TransferObject objTransferObject = new TransferObject();

}
*****************************************************

Class TransferObject {

private Double amout;

}
*****************************************************

In my Jsp

<html:text name="MyForm" property="objTransferObject.amount"/>

*****************************************************

While Im running the application Im getting 0.0 in the text box as default value.I want to avoid this.

Since the TransferObject carries the values and using hibernate the values are inserted in the database.

Since the TransferObject's amount property has Double as data type which is not String type we are referring this property in the Jsp.Whether it will create problem while validation as u told.

Is it a wrong approach that placing the TransferObject inside the formbean ?

Thanks in Advance

Cheers,
Prasath
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I would suggest creating ActionForm classes that contain String properties. That is what my project does. It is a little bit of a pain having to write the code required to translate between your data object and your form fields. Another issue with using wrapper classes is that if the user enters "12345A" in the text box and submits the page, it will redisplay with a validation message saying "the value you entered in the salary field is invalid" but the field will display as empty.

This translation code is a good place to do formatting. For example, you can format decimal values to a limited number of decimals, format date string or build up a user name from first, middle and last fields.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Avoid Default Values for Double or Any Wrapper Class