views:

472

answers:

1

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.

+2  A: 

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

  • you submit some values using a HTML form to an action (in this case the action is a Struts action, usually *.do);
  • Struts receives the requests because of the ActionServlet (specified in web.xml);
  • based on the information you specified in your struts-config.xml file, a specific Action class is selected (the one matching your request);
  • based on the identified Action an action form is obtained (again based on the info you specify in struts-config.xml);
  • an instance of this ActionForm is created or recycled (based on the scope of the form: request vs session);
  • data from the request is binded to the form properties;
  • your action execute(...) method is called with this form object.

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)?

dpb
The step is my assumption too, but seems when submit, the setTest in the form next got called. It is only called when page is loading...
BlueDolphin
Are you sure that the values are sent under the correct name? Can you intercept the request (or debug at server) and see what is submitted? Could something interfere, a javascript maybe?
dpb
Just found the problem, the variable case was wrong. Thanks for helping.
BlueDolphin