【问题标题】:Spring form binding how to do it ? Cannot convert value of type [java.lang.String] to required typeSpring表单绑定怎么做呢?无法将类型 [java.lang.String] 的值转换为所需类型
【发布时间】:2012-02-23 16:24:16
【问题描述】:

这是我的表格:

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

这是支持pojo:

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

选中的选项默认为 fooDTO 中的国家/地区值,这很好。但是在提交表单时绑定失败 - 我收到上述错误,我是否必须在活页夹中注册自定义编辑器,还是有更简单的方法? Country 与您所期望的差不多,countrys 确实是控制器中填充的国家/地区列表...

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    将您的路径更改为&lt;form:select path="country.id"&gt;。这至少会在发布时为您提供在 Country 对象内填充的 id 字段。

    【讨论】:

      【解决方案2】:

      Spring 3 引入了 Converter SPI,这让这变得非常容易。看看documentation中的6.5

      从文档中获取源代码并放入您所在的国家/地区,您会执行类似

      的操作
      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
          }
      }
      

      然后在 xml 配置中配置转换器

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

      正如 GriffeyDog 指出的那样,您可能希望将国家/地区 ID 放入选择路径中,以便您可以通过 ID 或其他内容获取国家/地区,而不是您的 Country 对象的 toString() 返回的任何内容。

      【讨论】:

      • 并为 java.lang.String 键入 @javax.persistence.OneToMany java.util.Set ?
      【解决方案3】:

      问题是 Spring 看到了一个 String 的实例,但知道它需要一个 Country 的实例。默认情况下,它不“知道”如何从一个到另一个。

      我之前没有使用过 Spring 表单绑定,但这看起来与 Spring 框架本身可能遇到的问题相同。在后一种情况下,您可以通过registering a PropertyEditor implementation for your class 解决它,所以我希望类似的方法可以在这里使用。

      【讨论】:

      • 是的,我现在正在研究它。有时 spring 文档相当难以理解,它们提供了几乎太多的选择......
      【解决方案4】:

      在 Spring 中你也可以使用 ConversionService

      这是文档:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#format-configuring-FormattingConversionService

      您需要实现 Converter 接口。

      【讨论】:

        【解决方案5】:

        看看我的解决方案:

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

        这通过从 DB 中获取将公司 ID 从选择转换为公司。

        如果您有任何其他问题,请提出,因为现在我正在我的应用中做类似的事情:)

        另外你需要添加到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>
        

        【讨论】:

          【解决方案6】:

          如果您正在开发 MVC 应用程序,请查看此 Spring Source 论坛链接以了解如何配置您的应用程序上下文。详情在帖子底部。

          conversionService 类与 MVC 不同,您需要指定 mvc:annotation-driven(即使您没有使用注解)。

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

          您将在 Spring 文档中找到详细信息: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/validation.html#format-configuring-formatting-mvc

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-03-13
            • 2014-02-01
            • 2017-06-14
            • 1970-01-01
            • 1970-01-01
            • 2021-09-02
            • 2020-07-02
            • 2017-08-01
            相关资源
            最近更新 更多