2

I have a problem with Struts. The flow is like this:

  • I submit a field with some value but the validation fails because of something. That's okay.
  • After that, I change some stuff on the page and that field becomes disabled.
  • Next time when I submit, I want the field value to be updated to null because it is disabled. The problem is that the field still holds the value from the first submit.

Any idea how should this be solved? I'm using Struts 1.

Bogdan
  • 23,890
  • 3
  • 69
  • 61
erik.c
  • 1,342
  • 2
  • 15
  • 27

1 Answers1

3

You probably have a session scoped form bean that retains the values between requests. When you submit new data, the form bean gets it's values updated from the fields inside the request (a data bind with the request parameters).

But there is a problem with disabled fields and checkboxes. Disabled fields, just like unchecked checkboxes, are not sent on the request when you submit the form. When the request arrives the field is not present in the request (because it's disabled) and Struts doesn't do a bind on it so it retains whatever value it previously had.

There are two ways to fix this:

  • use a request scoped form bean. This is not persisted between requests but is recreated fresh on each submit. Might not be possible if you have a wizard kind of flow and you do need to keep data in session in between requests.
  • use the ActionForm.reset() method to reset the value of your disabled field to null. If it's not present on the request when you submit it remains null, if it's present it's then updated when Struts does the data bind.
Bogdan
  • 23,890
  • 3
  • 69
  • 61