不调用Spring Converter

时间:2014-05-27 12:58:39

标签: java spring spring-mvc

我有一个转换器,但它没有被调用。我总是得到一个BindException。

public class IntegerToQueConverter implements Converter<Integer, QuestionType> {

@Autowired
private QuestionTypeService questionTypeService;

@Override
public QuestionType convert(Integer questionType) {

    return questionTypeService.findOne(questionType);
}

我在应用程序上下文中定义了以下bean。

<annotation-driven conversion-service="conversionService">

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

    </list>
</property>
</bean>

请帮忙。提前谢谢!

更新

我希望在此代码中调用转换器:

    @RequestMapping(value="/insertQuestion", method = RequestMethod.POST)
public ModelAndView insertQuestion(@ModelAttribute("questions") Question question)
{
    ModelAndView view=new ModelAndView("addQuestion");

    System.out.println(question.getQuestion());


            .............................

    return view;
}

1 个答案:

答案 0 :(得分:2)

Converter的类型参数更改为<String,QuestionType>

public class IntegerToQueConverter implements Converter<String, QuestionType> {

    @Autowired
    private QuestionTypeService questionTypeService;

    @Override
    public QuestionType convert(String questionType) {
        return questionTypeService.findOne(questionType);
    }
}

请求传入的值将为String类型,这要求转换器的类型为Converter<String,QuestionType>