Struts2属性歧义

发布于 2025-01-05 11:32:28 字数 928 浏览 1 评论 0原文

我正在 struts2 中创建一个项目
我创建了一个 Registration.jsp 页面,如下所示。

<s:form name="registration" action="Registration" >
<s:textfield name="user.userName" label="UserName"></s:textfield>
<s:textfield name="user.userName" label="Password"></s:textfield>
<s:textfield name="user.userName" label="Re-Enter Password"></s:textfield>
<s:textfield name="user.userName" label="Name"></s:textfield>
<s:textfield name="user.userName" label="DOB"/>
<s:textfield name="user.userName" label="email"></s:textfield>
<s:textfield name="user.userName" label="Portfolio Name"></s:textfield>
<s:submit></s:submit>
</s:form>

如上所示,我尝试为所有字段指定相同的名称,并用于设置值。
当我尝试将结果重定向到 jsp 并尝试显示用户名时调用该操作后,

如果我们分别在给定字段中输入 a、b、c、d 和 e 作为参数,它将显示如下
。我得到的输出完全是这样的(包含在输出中) - Hello a,b,c,d,e..

我不明白为什么会发生这种情况..有人知道吗?

I am creating a project in struts2
I have created a Registration.jsp page like below.

<s:form name="registration" action="Registration" >
<s:textfield name="user.userName" label="UserName"></s:textfield>
<s:textfield name="user.userName" label="Password"></s:textfield>
<s:textfield name="user.userName" label="Re-Enter Password"></s:textfield>
<s:textfield name="user.userName" label="Name"></s:textfield>
<s:textfield name="user.userName" label="DOB"/>
<s:textfield name="user.userName" label="email"></s:textfield>
<s:textfield name="user.userName" label="Portfolio Name"></s:textfield>
<s:submit></s:submit>
</s:form>

As seen above I am trying to give the all fields same name and it is used in setting the values..
After calling the action when I am trying to redirect the result to a jsp and trying to display the userName it will display like below

if we input a, b, c, d and e as the parameter respectively in the given fields. I am getting the output exactly(, included in output) like that - Hello a,b,c,d,e..

I am not getting why is this happening.. does anyone has an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

铃予 2025-01-12 11:32:29

这就是客户端到服务器通信的工作方式。从 HTML 角度来看,所有内容都将使用字符串的键值对发送到服务器。

在 Action 类中设置并解释为 Collection/Array 的值是功能 S2 及其转换机制(OGNL 和 XWork 转换)。

因此,当您的值作为带有诸如 user.userName=a,b,c,d,e 之类的值的键发送时,并且您已在操作类中声明了一个集合/数组,因此 S2 类型转换机制是开始执行操作并将这些值转换为数组/集合并将它们设置在操作类中受尊重的属性中。

在相反的情况下,HTML 将只知道字符串,并且再次从服务器将它们作为键和值发送,并且由于您没有要求您的 S2 机制参与进来,因此这将按照您的描述打印在您的 HTML 中。

This is how client to server communication works.From HTML perspective everything will be sent over to server using key-value pairs of String.

The value being set in the Action class and interpreted as Collection/Array is a feature S2 and its conversion mechanism (OGNL and XWork conversion ).

So when your values being sent as a key with values like user.userName=a,b,c,d,e and you have declared a collection/Array in your action class so S2 type conversion mechanism is coming in to action and converting these values to Array/Collection and setting them in the respected property in your action class.

In the reverse case HTML will know only String and again from the server they are being sent as key and values and since you are not asking your S2 mechanism to come in to play so this is getting printed in your HTML as per your description.

深海里的那抹蓝 2025-01-12 11:32:29

当您提交此页面时,网址将类似于 ?user.userName=a&user.userName=b&user.userName=c&user.userName=d&user.userName=e。 Struts2 将其视为CollectionArray。当您将此值传递给 String 类型参数时,这些值将以逗号分隔并存储。稍后,这就是操作调用后在页面中显示的内容。

When you submit this page, the url will look like ?user.userName=a&user.userName=b&user.userName=c&user.userName=d&user.userName=e. Struts2 considers this as a Collection or Array. When you take this value to String typed parameter, the values are comma separated and stored. Later on, this is what getting displayed in your page after the action call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文