我有一个转换器,但它没有被调用。我总是遇到 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;
}
最佳答案
更改 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>
.
关于java - 不调用 Spring Converter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23890136/