Spring form binding how to do? Cannot convert value of type [java.lang.String] to the required type

here is my form:

<form:form modelAttribute="fooDTO"> fooCountry: <form:select path="country"> <form:options items="${countries}" itemLabel="shortName" itemValue="id"/> </form:select> 

here is POJO support:

 public class FooDTO { private Country country; //getters and setters present } 

The selected parameter defaults to the country value in fooDTO, which is good. But the binding does not work when submitting the form - I get the above error, should I register my own editor in the binder, or is there a simpler method? The country is largely, as you would expect, and the countries are indeed a list of countries inhabited by the controller ...

+4
source share
6 answers

Spring 3 introduced the SPI converter, which makes it pretty easy. Take a look at 6.5 in the documentation

Taking the initial data from the documents and the placement in your country, you would do something like

 package my.converter; final class StringToCountry implements Converter<String, Country> { public Country convert(String source) { return // whatever you do to get a country from your string } } 

Then in the xml configuration you should configure the converter

 <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="my.converter.StringToCountry"/> </list> </property> </bean> 

As GriffeyDog pointed out, you can specify a country identifier to choose a path so that you can get the country by ID or something instead of what was returned toString () of the Country object.

+3
source

Change your path to <form:select path="country.id"> . This will at least give you the id field populated inside the Country object when publishing.

+5
source

The problem is that Spring sees the String instance, but knows that it needs the Country instance. He does not "know" by default how to get from one to another.

I haven't used Spring form bindings before, but this seems like the same problem you might encounter in the Spring framework itself. And in the latter case, you decide to register the PropertyEditor implementation for your class , so I expect a similar approach will work here.

+1
source

In Spring, you can also use ConversionService

Here's the documentation: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#format-configuring-FormattingConversionService

You need to implement the converter interface.

+1
source

Take a look at my solution:

 public class CompanyIdToInstanceConverter implements Converter<String, Company> { @Autowired CompanyService _companyService; @Override public Company convert(final String companyIdStr) { return _companyService.find(Long.valueOf(companyIdStr)); } } 

This converts the company identifier from the selected one to the company, receiving it from the database.

If you have additional questions, ask, because now I am doing similar things in my application :)

In addition, you need to add in the app-context:

 <mvc:annotation-driven conversion-service="conversionService" /> <!-- conversion service --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set>toryLabelConverter"></bean> <bean class="pl.greenpath.converter.CompanyIdToInstanceConverter"></bean> </set> </property> </bean> 
+1
source

If you are working on an MVC application, see the Spring Source forum link to customize your application context. Details are at the bottom of the stream.

The conversionService class is different from MVC, and you need to specify mvc: annotation-driven (even if you are not using annotations).

http://forum.springsource.org/showthread.php?84003-Converters-no-matching-editors-or-conversion-strategy-found

You will find features of the Spring documentation: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/validation.html#format-configuring-formatting-mvc

0
source

Source: https://habr.com/ru/post/1398074/


All Articles