File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 1.3 form pocessing question

 
Alex Serna
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic