How struts2 store the form elements into Map ? (Prior Java5)
Lets assume that you are submitting following form data
<s:textfield name="myUsers['1'].age" label="age"/>
<s:textfield name="myUsers['2'].age" label="age"/>
<s:textfield name="myUsers['3'].age" label="age"/>
and below is the map available on your action class where the form data will be stored.
private Map myUsers;
public Map getMyUsers()
{
return myUsers;
}
public void setMyUsers ( Map myUsers ) {
this.myUsers= myUsers;
}
here myUsers map has Integer value as a Key and User object as a Value for integer key.
So to inform the OGNL about this key and value object's type we need to configure this key and value type into the
ActionClassName-conversion.properties file.
Key_myUsers = java.lang.Integer
/* Here we are telling the framework that the key into myUsers map must be of type Integer*/
Element_myOrderedUsers=com.data.User
/* Here we are telling the framework that the value into myUsers map must be User object*/
So when you will submit the form, OGNL will convert the '1' ,'2', '3' into integers and the value for the respective provided field will be stored into the
User object's age property.
<s:textfield name="myUsers['1'].age" label="age"/>
<s:textfield name="myUsers['2'].age" label="age"/>
<s:textfield name="myUsers['3'].age" label="age"/>
and below is the map available on your action class where the form data will be stored.
private Map myUsers;
public Map getMyUsers()
{
return myUsers;
}
public void setMyUsers ( Map myUsers ) {
this.myUsers= myUsers;
}
here myUsers map has Integer value as a Key and User object as a Value for integer key.
So to inform the OGNL about this key and value object's type we need to configure this key and value type into the
ActionClassName-conversion.properties file.
Key_myUsers = java.lang.Integer
/* Here we are telling the framework that the key into myUsers map must be of type Integer*/
Element_myOrderedUsers=com.data.User
/* Here we are telling the framework that the value into myUsers map must be User object*/
So when you will submit the form, OGNL will convert the '1' ,'2', '3' into integers and the value for the respective provided field will be stored into the
User object's age property.