views:

99

answers:

3

Hi can somebody explain that if i declare variables of other types (except string) in the form beans what problems I might face ?

A: 

Take a look at http://jtute.com/struts/0401.html

SUMMARY: What data types should you use for an action form’s properties? Here are some suggestions.

adatapost
A: 

The information from the browser to the server goes over HTTP, and there is no awareness of data types or objects, so your best bet is to use String types.

simon
+1  A: 

Your request parameters are Strings. If you have different type of parameters in your form, when binding the request, Struts will perform conversions from String to your parameter types.

This is where problems can occur.

Imagine you have a property that is of type int and you request some String that fails to convert to an int (e.g. contains letters – but this is already a data validation issue).

Now, an int must always have a value since it is a primitive, so Struts will put 0 in it and fail silently. When you will use that field with the value 0 you won’t know if you had an error or the user inserted 0 on his own. You can face this problems with other types of fields also.

You have to be in control of the binding, don’t always count on Struts.

dpb