如何解决"无法转换属性值[...]"

时间:2016-10-09 02:14:08

标签: spring hibernate spring-mvc spring-data-jpa

所以我有一个带有Spring MVC下拉列表的表单:

<form:form modelAttribute="user" action="registerVerify" method="post">
<!-- Other fields-->
<spring:message code="register.country" />
        <form:select path="country" items="${countryList}" />
        <br/>
</form:form>

由此控制器填充:

@Controller
public class RegisterController {

    @RequestMapping(value = "registerForm", method = RequestMethod.GET)
    public String register(@ModelAttribute("user") User user, Model model) {
        model.addAttribute("user", new User());
        model.addAttribute("countryList", cDao.getCountryMap());
        model.addAttribute("companyList", cpDao.getCompanyMap());
        return "login/registerForm";
    }
@RequestMapping(value = "registerVerify", method = RequestMethod.POST)
    public String makeRegistration(@ModelAttribute("user") @Valid User user, BindingResult result,
            RedirectAttributes redirectAttributes, Model model) {
        if (result.hasErrors()) {
            System.out.println(result.getFieldError().getDefaultMessage());
            model.addAttribute("org.springframework.validation.BindingResult.user", result);
            return "redirect:registerForm";
        }
        if (dao.add(user)) {
            redirectAttributes.addFlashAttribute("user", user);
            return "redirect:login";
        } else {
            return "redirect:registerForm";
        }

    }
// Service classes bellow

我制作了一些转换器

package br.com.sirious.energyquality.converters;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;

import br.com.sirious.energyquality.dao.CompanyDao;
import br.com.sirious.energyquality.models.Company;

public class IdToCompanyConverter implements Converter<String, Company>{

    @Autowired
    CompanyDao dao;

    @Override
    public Company convert(String id) {
        return dao.getCompanyByID(Integer.parseInt(id));
    }

}

我已经设置了My WebMVCConfig(以及WebApplicationInitializer和spring-context ......)

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry){
        registry.addConverter(new IdToCompanyConverter());
    }


}

但我仍然得到&#34;无法将[java.lang.String]类型的属性值转换为属性&#39; Company&#39所需类型[br.com.sirious.energyquality.models.Company] ;;嵌套异常是java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为属性&#39;公司的所需类型[br.com.sirious.energyquality.models.Company]:不匹配编辑或转换策略找到&#34;

我已经检查了许多类似的帖子,但没有解决问题。有人可以诊断发生了什么吗?

2 个答案:

答案 0 :(得分:1)

尝试使用以下 ConverterRegistry 方法:

<S,T> void addConverter(Class<S> sourceType,
                    Class<T> targetType,
                    Converter<? super S,? extends T> converter)

这将导致:

public void addFormatters(FormatterRegistry registry){
    registry.addConverter(String.class, Company.class, new IdToCompanyConverter());
}

答案 1 :(得分:0)

问题在于表单的路径:select tag:

<spring:message code="register.country" />
        <form:select path="country" items="${countryList}" />

为了映射到用作引用的对象,路径必须是对象的id,因此路径为: Country.id。

这个想法类似于这里所说的:Spring form binding how to do it ? Cannot convert value of type [java.lang.String] to required type

此外,xuesheng添加了一些有关在不使用web.xml的情况下注册转换器的有趣信息。