Reputation: 1272
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?
Upvotes: 0
Views: 269
Reputation: 23587
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.
Upvotes: 1
Reputation: 10555
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.
Upvotes: 0