实例化多个对象实例并发送到jsp
我该怎么做?
我有一个表单,可以发送同一参数的 1-5 个值:因此,举个例子,我可以让用户填写 3 个文本框,每个文本框包含他们最喜欢的颜色。我会从 using: 中获取这些值,
String [] color = request.getParameterValues("color");
这会给我一个包含这些值的数组。然后我的计划是做这样的事情:
for(int i= 0; i<color.length; i++)
{
Child child = new Child(color[i]);
request.setAttribute("color",color);
}
首先,这会创建三个名为“颜色”的单独属性,还是每次都会覆盖“颜色”属性?
其次,我如何在最后一页收集这些信息?
<% Color color =(Color) request.getAttribute("color"); %>
这会只返回一个值还是我创建的对象数组?
How should I do this?
I have a form that could send 1-5 values of the same parameter: so for an example I could have the user fill out 3 text boxes each containing their favorite color. I would grab those values from using:
String [] color = request.getParameterValues("color");
which would give me an array of those values. Then my plan was to do something like this:
for(int i= 0; i<color.length; i++)
{
Child child = new Child(color[i]);
request.setAttribute("color",color);
}
Firstly would this create three separate attributes named "color" or would this override the "color" attribute each time?
Secondly how would I collect that information out on the final page?
<% Color color =(Color) request.getAttribute("color"); %>
Would this return only one value or an array of the objects I created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setAttribute 将覆盖属性“color”,并且您将仅使用最后一个变量。但你可以这样做:
所以在 JSP 中你可以检索颜色,如下所示:
The setAttribute will override the attribute "color" and you will have available just the last variable. But you can do:
So in the JSP you can retreive the colors as following: