checkboxstrutsactionform

How does struts 1.X ActionForm handle pure html checkbox list?


I am dealing with an old application which uses struts 1.2. And for some reason, we are using pure html form to submit to struts action. For example:

for the content output of testAction.do, I try to submit to itself.

item 1 item 2

Then I associate an form bean TestForm with testAction.

I am not sure how to specify the properties in the form so that it take the value from myitem1 after user click "submit"

I tried to add getMyitem1(), setMyitem1(String[] items), getSelectedMyitem1(), setSelectedMyitem1(String[] items)

Those set methods are only called when page are loaded.

I guess my understanding of ActionForm in struts 1.x must be wrong.

Please advise, thanks.


Solution

  • First, let's clarify a bit how Struts manages form submits:

    Now, binding is done based on name; name of the request parameter matching the name of the property in the form. The name of the request parameter is off course the name of an input field from the HTML form you submitted.

    For example, if you submit an input with name test, you should have the following accessors in your action form class (which respect JavaBeans convention for property named test):

    public void setTest(String val) { ... }
    public String getTest() { ... }
    

    If you have a list of values attached to the test parameter (which I assume is your case since you mention a check box list) the accessors change to array type:

    public void setTest(String[] val) { ... }
    public String[] getTest() { ... }
    

    But again the name is used for matching, always the name.

    Not sure what you are trying to do with item 1 item 2. Are these the values that are submitted for your input (equivalent of test)?