Struts2:将输入数组读取到动作中
我试图将一组输入获取到我的操作类中,但它总是返回 null;
这是输入的 HTML
<input class="activityInput" type="text" name="sentdate[" + i + "]" value="1" />
<input class="activityInput" type="text" name="sentdate[" + i + "]" value="2" />
,这是操作的类
public class ActivityAction extends ActionSupport{
private List sentdate;
public List getSentdate() {
return sentdate;
}
public void setSentdate(List sentdate) {
this.sentdate = sentdate;
}
}
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不应该需要方括号。
将呈现以下 html
这应该填充您的操作的“sentdate”列表。
You shouldn't need the square brackets.
would render the following html
This should populate the 'sentdate' List of your Action.
Struts 应该填充 List,但没有:)
这个问题更好的解决方案是在Struts操作类属性中使用String而不是List。
如果您提交表单,
您将在“私人字符串发送日期”中重新接收:1,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
you will reseive in "private String sentdate" this: 1,2
and you can split this string with comma delimiter and give you List..
您应该使用
< /a> 标签。You should use the
<s:select>
tag.它甚至能参加行动吗? 我建议使用 HTTP 侦听器,例如 Fiddler 或 IBM Page Detailer,以便您可以查看正在发送到服务器的内容(如果有)。
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.
请在您的操作中考虑调试语句以查看返回的结果。 我在想类似在你的 Action 顶部的东西:
而且我认为如果你在示例中使用真正的 HTML 会更好。 “+i+”的事情
不是 HTML,我无法从这里看到它是如何呈现为 HTML 的。 这可能没问题,但我更喜欢看到浏览器看到的 HTML 代码。
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:
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.
正如 Stanimir 所说,甚至不需要“值”属性。 只需发送具有相同名称的参数就足够了。 在服务器端我们将获得字符串列表。
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.