• 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

Struts 1.3 form pocessing question

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!


 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic