You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Richard Gundersen <R....@salford.ac.uk> on 2009/02/05 13:00:12 UTC

Collections and type conversion

Hi

Could anyone point out what I'm doing wrong here please? My model (FileType) has a collection of Field objects and a getter called getFileFields(). 

I want to automatically display and set these when my form is rendered/posted. 

Rendering is easy using an iterator, but to allow the collection to be reconstructed upon form submission, I think I need to enable type conversion. So, in the same package as my action, I have this file:

------------------------------------------
FileManagementAction-conversion.properties
------------------------------------------
KeyProperty_fields=id
Element_fields=uk.ac.salford.bulkuploader.entity.Field
CreateIfNull_fields=true

In my JSP, I have the following 
-------------------------------

<s:iterator id="field" value="model.fileFields" status="status">
	<s:textfield name="fieldName" size="20"/>     <%-- version 1 --%>
	<s:textfield name="fileFields(%{id}).fieldName" />	<%-- version 2 --%>
		...

Version 1 displays OK, but because it's not indexed, when I submit the form, the model's collection (fileFields) is not updated. 
Version 2 does not display the value (of 'fieldName') at all (although the '%{id}' value is correctly output). And, it doesn't set the collection in my model either, although I believe it should according to the docs. 

I've tried lots of variations on the OGNL syntax for the textfield's name attribute but can't hit the right one; would really appreciate some help.

Thanks
 
Richard


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Collections and type conversion

Posted by Richard Gundersen <R....@salford.ac.uk>.
Well, I got the collection populated using this syntax (I am using a List, and the docs say you should not use square brackets unless it's a map, but it seems to require square brackets (also needs the value attribute as below)

<s:textfield name="fileFields[%{id}].fieldName" value="%{fieldName}"/>

I also removed the properties file since it seems to work without it (probably generics kicking in I guess?)

So, my final problem is this: my model's List (an ArrayList) is being populated, but for each element that is correctly set, I also get an extra 'null' element, which I don't want. 

Anyone know where the null elements are coming from (it looks like this):

[0] null
[1] Field[blah]
[2] null
[3] Field[blahblah]

Nearly there - Thanks!

-----Original Message-----
From: Richard Gundersen [mailto:R.Gundersen@salford.ac.uk] 
Sent: 05 February 2009 12:00
To: user@struts.apache.org
Subject: Collections and type conversion

Hi

Could anyone point out what I'm doing wrong here please? My model (FileType) has a collection of Field objects and a getter called getFileFields(). 

I want to automatically display and set these when my form is rendered/posted. 

Rendering is easy using an iterator, but to allow the collection to be reconstructed upon form submission, I think I need to enable type conversion. So, in the same package as my action, I have this file:

------------------------------------------
FileManagementAction-conversion.properties
------------------------------------------
KeyProperty_fields=id
Element_fields=uk.ac.salford.bulkuploader.entity.Field
CreateIfNull_fields=true

In my JSP, I have the following 
-------------------------------

<s:iterator id="field" value="model.fileFields" status="status">
	<s:textfield name="fieldName" size="20"/>     <%-- version 1 --%>
	<s:textfield name="fileFields(%{id}).fieldName" />	<%-- version 2 --%>
		...

Version 1 displays OK, but because it's not indexed, when I submit the form, the model's collection (fileFields) is not updated. 
Version 2 does not display the value (of 'fieldName') at all (although the '%{id}' value is correctly output). And, it doesn't set the collection in my model either, although I believe it should according to the docs. 

I've tried lots of variations on the OGNL syntax for the textfield's name attribute but can't hit the right one; would really appreciate some help.

Thanks
 
Richard


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org