实例化多个对象实例并发送到jsp

发布于 2024-12-29 00:41:55 字数 578 浏览 0 评论 0原文

我该怎么做?

我有一个表单,可以发送同一参数的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只等公子 2025-01-05 00:41:55

首先,这会创建三个名为“color”的独立属性或
这会每次都会覆盖“颜色”属性吗?

setAttribute 将覆盖属性“color”,并且您将仅使用最后一个变量。但你可以这样做:

Child [] childs = new Child [color.length];
for(int i= 0; i<color.length; i++)    {
    Child child = new Child(color[i]);
    childs[i] = child;
}
request.setAttribute("colors",childs);

所以在 JSP 中你可以检索颜色,如下所示:

<% Child[] childs =(Child[]) request.getAttribute("colors"); %>

Firstly would this create three separate attributes named "color" or
would this override the "color" attribute each time?

The setAttribute will override the attribute "color" and you will have available just the last variable. But you can do:

Child [] childs = new Child [color.length];
for(int i= 0; i<color.length; i++)    {
    Child child = new Child(color[i]);
    childs[i] = child;
}
request.setAttribute("colors",childs);

So in the JSP you can retreive the colors as following:

<% Child[] childs =(Child[]) request.getAttribute("colors"); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文