Hello Burton,

I could post back lists without any custom converter as below:

```jsp
<s:form action="strutsTypeConverterAction" >
     <s:iterator value="integerList" status="stat" var="i">
         <s:hidden value="%{#i}" name="integerList[%{#stat.count}]"/>
     </s:iterator>
     <s:iterator value="stringList" status="stat" var="s">
         <s:hidden value="%{#s}" name="stringList[%{#stat.count}]"/>
     </s:iterator>
     <s:submit/>
</s:form>
```

But about your question:
Java uses something called "type erasure", which means at runtime both 
List<String> and List<Integer> are equivalent. The compiler knows the 
lists contain integers or strings, and as such can maintain a type safe 
environment. This information is lost (on an object instance basis) at 
runtime, and the list only contain 'Objects'.

If the list has a non-zero number of elements, you could investigate the 
type of the first element ( using it's getClass method, for instance ). 
That won't tell you the generic type of the list, but it would be 
reasonable to assume that the generic type was some superclass of the 
types in the list.

I wouldn't advocate the approach, but in a bind it might be useful.

```java
List<Object> listCheck = (List<Object>)(Object) stringList;
     if (!listCheck.isEmpty()) {
        if (listCheck.get(0) instanceof String) {
            System.out.println("List type is String");
        }
        if (listCheck.get(0) instanceof Integer) {
            System.out.println("List type is Integer");
        }
     }
}
```

References:
[1] 
https://stackoverflow.com/questions/1942644/get-generic-type-of-java-util-list

On 9/25/2017 6:34 AM, Burton Rhodes wrote:
> When extending the StrutsTypeConverter, is there anyway to figure out if
> the "toClass" is a List<Integer> or a List<String>?  I have found that when
> converting data from a submitted webpage that has a value of a List in a
> single variable, the standard struts conversion fails.  For example:
> <s:hidden name="integerList" value="%{integerList}" /> display as <s:hidden
> name="integerList" value="[1,2,3,4,5]" />.  When the form submits, this
> value will not convert properly back into the List.  In addition, the
> conversion fails if you have a list of checkboxes and the user only checks
> one -  hence only a single variable being converted to a List.
> 
> I have solved this issue by creating a MyListConverter, but I have to
> declare the converter on each action in a [-conversion.properties] file
> which is now becoming a bit cumbersome since this is such a common
> occurrence. I have also declared some on my model/entity beans, but
> still.... I would love to have a more global "List" converter, such as:
> 
> [xwork-conversion.properties]
> java.util.List=com.afs.web.converter.MyListConverter
> 
> Thus any List that is encountered, I could attempt "my conversion" and send
> up the chain of command if the resulting List is not of List<Integer> or
> List<String>.
> 
> [Psuedo_Code]
> public class MyListConverter extends StrutsTypeConverter {
>      public Object convertFromString(Map context, String[] values, Class
> toClass) {
>          // Is toClass List<Integer>, then try "my" conversion
> 
>          // Else is toClass List<String?, then try "my" conversion
> 
>          // Else return performFallbackConversion(context, o, toClass);
>      }
> }
> 
> Any ideas?  Or am I going about this incorrectly?
> 
> Thanks in advance.
> 

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

Reply via email to