0

I'm trying to get an array of input into my action class but it always returns null;

Here's the HTML for the input

<input class="activityInput" type="text" name="sentdate[" + i + "]" value="1" />
<input class="activityInput" type="text" name="sentdate[" + i + "]" value="2" />

and here's the class for the action

public class ActivityAction extends ActionSupport{
    private List sentdate;
    public List getSentdate() {
    return sentdate;
    }

    public void setSentdate(List sentdate) {
        this.sentdate = sentdate;
    }
}

What am i doing wrong?

6 Answers 6

3

You shouldn't need the square brackets.

<s:textfield name"sentdate" value="1" />
<s:textfield name"sentdate" value="2" />

would render the following html

<input type="text" name="sentdate" value="1" />
<input type="text" name="sentdate" value="2" />

This should populate the 'sentdate' List of your Action.

Sign up to request clarification or add additional context in comments.

Comments

2

Struts should populate List but don`t :) The better solution of this problem is to use String instead List in Struts action class properties. If you submit form with

<input type="text" name="sentdate" value="1" />
<input type="text" name="sentdate" value="2" />

you will reseive in "private String sentdate" this: 1,2 and you can split this string with comma delimiter and give you List..

Comments

1

You should use the <s:select> tag.

Comments

0

Is it even making it to the Action? I suggest using an HTTP listener like Fiddler, or IBM Page Detailer, so that you can see what, if anything, is even being sent to the server.

Comments

0

Please consider a debug statement in your action to see what you get back. I'm thinking something like at the top of your Action:

Enumeration parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
  String currentParameterName = (String) parameterNames.nextElement();
  String[] values = request.getParameterValues(currentParameterName);
  for (String value : values) {
    logger.debug("Parameter " + currentParameterName + " has value " + value);
  } 
}

And I think it would be better if you use the real HTML in your sample. The " + i + " thing is not HTML and I can't see from here how that is rendered to HTML. It's probably allright but I prefer to see the HTML code as the browser sees it.

1 Comment

h lol. that was my problem. I forgot to change " + i + " to \"" + i + "\"
0

as Stanimir told even the "value" attribute is not required. Just sending the parameters with the same name is enough. In the serverside we will get the list of string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.