aspose file tools*
The moose likes Struts and the fly likes Struts 1.3 form pocessing question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 1.3 form pocessing question" Watch "Struts 1.3 form pocessing question" New topic
Author

Struts 1.3 form pocessing question

Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58
Hi everyone,

I don't understand the way struts treats the ActionForm bean in Struts 1.3. This is how it goes...

I have a <html:form action="/myaction.do"><html:text property="myinteger"/></html:form>. I understand that this creates a new ActionForm bean with a type and a scope specified in struts-config.xml if the bean doesn't already exist.

The ActionForm bean I'm using has an Integer attribute which I want to have value -1 if no value was introduced in the form.

The first time I present the jsp page that has the <html:form> nothing appears in that <html:text property="myinteger"/> as I expect because the ActionForm bean has just been created and that Integer property has a default value of null.
My problem is that in my MyAction class, the first time I get my AtionForm subclass something(this is what I'm trying to find out) has assigned a default value of 0 to my Integer property which I was expecting to still be null.

I tried to prevent this by chaning the myinteger setter in my ActionForm subclass to:


but when debugging the setter method already comes with a 0.

Thanks!


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd guess because if the string from the form is blank (it will never be null from the form) that the type conversion will make it a zero.
Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58
Thanks for you response David!

You are right, a blank field in the form is a request paramter with an empty string, not null. But my question is, where does this convertion from request parameter to Integer happens and how can I change that behavior so that I don't end up with 0.

I could achieve my goal with javascript by checking if that field is blank and assigning it a negative value onsubmit, but I wonder if there is a more elegant way to do it with struts...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I think somewhere along the line it just calls some of the BeanUtils converter stuff; you might have to register a converter and use that at the type if you want to circumvent the default behavior.

Personally, I essentially always used String form types and did the conversion/etc. myself to avoid these kinds of issues.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts 1.3 form pocessing question