Struts2属性含糊不清

时间:2012-02-15 10:00:57

标签: java parameters struts2 action

我正在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并尝试显示userName时调用操作后,它将显示如下

如果我们分别在给定字段中输入a,b,c,d和e作为参数。我正确地得到输出(包括在输出中) - 你好a,b,c,d,e ..

我不明白为什么会发生这种情况..有没有人有想法?

2 个答案:

答案 0 :(得分:1)

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

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

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

在相反的情况下,HTML将只知道字符串,并且再次从服务器知道它们作为键和值被发送,因为你没有要求你的S2机制进来播放所以这将按照你的HTML打印出来描述

答案 1 :(得分:0)

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