我有一个转换器,但它没有被调用。我总是得到一个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;
}
答案 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>
。