Announcement Announcement Module
Collapse
No announcement yet.
Configuring custom Object to String conversion Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuring custom Object to String conversion

    I've tried to post comment on spring-3-type-conversion-and-validation Spring blog entry, but its spam filter doesn't let me, so I'm posting question here.

    If I understood well, for converting object to String convention is to implement toString. But, mentioned blog entry (at least I don't get from it) does not describe how does one configure custom behavior. IMO convention should suite in most cases, but here I think convention will rarely be applied - toString that convention expects contradicts with what toString typically contains (e.g. for conversion of an @javax.persistence.Entity its toString should return just id but toString typically contains more stuff).

    Spring Webflow as of 2.1.0.M1 supposedly fully supports Spring 3 type conversion ( https://jira.springframework.org/browse/SWF-1234 ). Even though one registers custom org.springframework.core.convert.converter.Convert er bean with ConversionService to perform custom-POJO-to-String conversion, Spring 3.0.4 & Spring Webflow 2.1.1 ignore custom converter and POJO's toString method always gets called. Custom fromString converter gets used when binding form input, but displaying bound values doesn't work.

    E.g. if there is NamedEntity JavaBean with id and name attributes, and one implements "NamedEntityToStringConverter implements Converter<NamedEntity, String>" and "StringToNamedEntityConverter implements Converter<String, NamedEntity>", and registers them with ConversionService, if NamedEntity doesn't override toString or toString returns more than just id, following tag will work in from-string direction, but not to-string:

    Code:
    <form:checkboxes items="${allNamedEntityInstances}" path="selectedNamedEntities" itemValue="id" itemLabel="name" />
    After implementing toString by convention, to return String representation of NamedEntity's id attribute, to-string direction works as well.

    So question is, conventions works, but how does one override it?
    Last edited by sslavic; Sep 22nd, 2010, 01:08 PM.

  • #2
    Web Flow 2.1 does indeed support Spring 3 type conversion. Keep in mind that type conversion in Web Flow (which pre-dates the one Spring previously based on PropertyEditor's) supports the Spring 3 type conversion by wrapping an instance of a Spring 3 ConversionService. By default however this instance is not the same as the one automatically registered by the <mvc:annotation-driven> tag.

    Without any further details I can't tell what's going on. On a high level at least what you're doing seems about right. You could try this with Spring MVC first if you want to make sure it isn't anything to do with how you've configured Web Flow.

    Comment


    • #3
      There's definitely a bug in binding/conversion logic. Will create an example app and attach it to a ticket in JIRA.

      Comment

      Working...
      X