tags:

views:

340

answers:

3

Hello,

I got a strange (and probably) wrong behaviour here. I was trying to put a checkbox on the page, and you know it: unchecked checkboxes are not written to the request (only checked boxes are)

What struts normally offers is to overrite:

@Override
public void reset(ActionMapping mapping, ServletRequest request)

to reset all the checkboxes to "false". But in my case, this reset() is never called!

Someone got an idea?

Thanks in advance,

mana

A: 

Reset is never called by default, you have to call it through your actions (if you want to reset your form),

alternatively, on your jsp, you'll have <html:reset /> tag and override the reset method of ActionForm. this helps.

The Elite Gentleman
I dont want to reset the whole form. I just want to set all the fields which hold values from the checkboxes to "false" - so that checked boxes are correctly written to these fiels with the value "true" ... and unchecked boxes (whose value is not transmitted in the request) stick at "false". This normally is an automatism within struts, but as I said, that reset() is not executed in my case.
mana
in that case, on your action, override the `reset()` method and then on your method e.g.<br /> `public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { form.reset(); return mapping.findForward(..);}`....What I mean is that you can (typecast) call the `reset()` as I did in the example.
The Elite Gentleman
the reset() normally doesn't need to be called manually. It has to be called by the struts framework? Or am I wrong?
mana
@mana: you are correct, in normal operating coditions Struts calls reset automatically.
dpb
+1  A: 

The reset method is automatically called by the Struts framework (your observation on The Elite Gentleman's answer is correct)... that is if you did everything by the book.

Check the following:

  1. are you extending an ActionForm class or something else?
  2. did you by any chance overwrite the reset method in a subclass of the one you are not getting the method called and you use that for your action?
  3. are you managing the form life cycle by hand, instead of letting Struts do it?
  4. in struts-config, did you specify that your action uses that particular form by specifying the name attribute on the action tag?

My money is on number 4.

dpb
Well, I checked:1. My Form is extending ValidationActionForm, which is extending Action Form. 2. In the debugger, it says the correct instance of my Form. In case any subclass would override reset(), too - this will not be a problem, since the reset() of the upper most class will be called. From there I call super.reset().3. The Form is only described in the struts-config.xml (I don't do new MyForm(); and session.add(myForm) or such stuff)4. Yes, the action has the correct name of the Form (with session scope)
mana
Strange... Maybe if you provide some more code with your question. Also,do you have an attribute named "attribute" on the action tag in struts-config? Do you use <html:form> tags in the JSP to retrieve the form or you use something else (like a plain vanilla form)?
dpb
A: 

The correct method signature to override is

public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request)

Azam Abdul Rahim