I also attempted to extend the DefaultTypeConverter (instead of the StrutsTypeConverter) and override one of the convertValue() methods to try to reuse existing Struts code. Although the code below gives no errors, ultimately, the target object (viewTasksFilter.taskStatuses) is not set (it remains null). Again, any suggestions are welcome. Thanks!
/** * Converter class to convert comma separated string to a collection */ public class MyStringToCollectionConverter extends DefaultTypeConverter { @Override public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) { // Convert value to "cleaned" String[] String s = ((String[]) value)[0]; String temp = s.replaceAll("[\\[\\]\"']", ""); String[] cleanedValues = Arrays.stream(temp.split(",")) .map(String::trim) .filter(StringUtils::isNotBlank) .toArray(String[]::new); // Use the member variable to discover the type of object in the target collection Class<?> valueClass; try { Type[] types = ((Method) member).getGenericParameterTypes(); ParameterizedType pType = (ParameterizedType) types[0]; valueClass = (Class<?>) pType.getActualTypeArguments()[0]; } catch (Exception e) { valueClass = String.class; } // Convert cleanedValues into a collection of "valueClass" objects List<Object> convertedValues = new ArrayList<>(); for (String val : cleanedValues) { convertedValues.add(convertValue(val, valueClass)); } return super.convertValue(context, target, member, propertyName, convertedValues.toArray(), toType); } } On Wed, Aug 11, 2021 at 7:49 AM Burton Rhodes <burtonrho...@gmail.com> wrote: > I have a html form input that submits a string value that needs to be > converted to a java Collection (a Set object in this case). Specifically > the input key/value is: viewTasksFilter.taskStatuses="[OPEN,COMPLETE]". > In the "viewTaskFilter" object, "taskStatuses" is defined as a Set with > enum values (Set<TaskStatus> taskStatuses). The custom converter below > does convert the input form value to a Set, however it is a Set<String> > (not Set<TaskStatus>). This is an issue because the Struts Action member > variable "viewTasksFilter.taskStatuses" now contains a Set of the wrong > type. Since I will have other variables that need this logic, I would > prefer not to hardcode the Set<TaskStatus> in this converter. Is there a > way to use existing Struts "converter code" to convert the Set<String> to > the appropriate Set<Object>? I know the built-in Struts converters already > do this, but I am unable to figure out how to do this in a custom > converter. Is this possible? Or is this even the right way to solve this > issue? Any help is appreciated! > > > /** > * Converter class to convert comma separated string to a collection > */ > public class MyStringToCollectionConverter extends StrutsTypeConverter { > > @Override > public Object convertFromString(Map context, String[] values, Class > toClass) { > > String s = values[0]; > String temp = s.replaceAll("[\\[\\]\"']", ""); > List<String> cleanedValues = Arrays.stream(temp.split(",")) > .map(String::trim) > .collect(Collectors.toList()); > > if (toClass == Set.class) { > try { > if (cleanedValues.size() == 0) { > return null; > } else { > return new HashSet<>(cleanedValues); > } > } catch (Exception e) { > throw new TypeConversionException("Could not parse to Set > class:" + Arrays.toString(values)); > } > > } else if (toClass == List.class) { > try { > if (cleanedValues.size() == 0) { > return null; > } else { > return cleanedValues; > } > } catch (Exception e) { > throw new TypeConversionException("Could not parse to List > class:" + Arrays.toString(values)); > } > > } > > return null; > } > > @Override > public String convertToString(Map context, Object o) { > if (o == null) { > return null; > } else { > return o.toString(); > } > } > } >