0

I'm trying to pass the value from JSP page to my Action class when pressing the Update button.

In this case, I create in action class a list of Strings named value with getter and setter.

My problem is that, when trying to modify the input field in JSP and then pressing Update, value remains the same (with its initial value) in the action class.

For example:

v[0] = "zero"
v[1] = "something"
v[2] = "true"

In my form I've changed the v[2] input text in "false" and then pressed Update; when printed in execute(), v[2] remains "true".

UPDATE: I rewrite the JSP code, using Struts 2 tag instead of scriptlet

<form name="propertiesForm" method="post" action="<s:url value='/update.action'/>" >
        <table>
              <tr>
            <th>Property</th>
            <th>Value</th>
              </tr>
          <s:iterator value="%{propertiesForm.properties}" status="rowStatus">
         <tr>
        <td><s:property value="%{properties[#rowStatus.index].name}"/></td>
        <td><s:textfield name="value[%{#rowStatus.index}]" value="%{propertiesForm.getValue(#rowStatus.index)}"/></td>
        </tr>

         </s:iterator>
        <tr>
           <td><input class="buttons" type="submit" value="Update" /></td>
        </tr>
   </table> 
</form>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Ariana
  • 283
  • 1
  • 6
  • 17
  • Is there a `setValue(String[] v)` in the Action? – fustaki Jun 06 '17 at 11:13
  • Did you see the log show that the `update` action is actually be called when you press the `update` button, if so, could you show your `update` action. – Fangxing Jun 06 '17 at 11:13
  • @fustaki Yes, I have setter and getter for value in action class – Ariana Jun 06 '17 at 12:07
  • 2
    Post the List definition and its getter and setter. However, the way you're mixing scriptlets with struts2 tags with html is creepy :| have a look at the `` tag – Andrea Ligios Jun 06 '17 at 13:45

1 Answers1

0

Form fields are bound by their name attribute. It should be a proper OGNL expression, which is evaluated by params interceptor against the valueStack. The action bean should be on top of the valueStack when parameters are populated, if your action is not model driven, then the action bean will be populated if it has corresponding property accessors.

The problem is in your code as you use scriptlets (that are highly discouraged, see How to avoid Java code in JSP files?) and has a bug in the code name="<%="value[" + i + "]"%>.


Looking on the code you have update recently in your question

<s:textfield name="value[%{#rowStatus.index}]" value="%{propertiesForm.getValue(#rowStatus.index)}"/>

I see that the value comes from propertiesForm property, but the form field is bound to action property. It might not have corresponding setter to fill the property value when the form is submitted. I recommend to bind the value to the propertiesForm, so you even don't need the value pre-populated, since the value evaluated by the name attribute.

<s:textfield name="propertiesForm.value[%{#rowStatus.index}]" />
Roman C
  • 49,761
  • 33
  • 66
  • 176