How to register formatters in Spring MVC web application?

霸气de小男生 提交于 2019-12-24 01:56:30

问题


I am facing the problem while binding nested object in Model Attribute. I have one class Book, in which I have nested class Subject as below :

@Entity
@Table(name = "book")
public class Book {

    // Other properties...
    // With all getter setter..
    @ManyToOne
    @JoinColumn(name="subject_id",nullable=false)
    @NotBlank(message = "Please select subject")
    private Subject subject;
    // Getter setter of subject;
}

Also I have implemented Formatter class for Subject as below :

@Component
public class SubjectFormatter implements Formatter<Subject>{

    @Autowired
    SubjectService subjectService;

    @Override
    public String print(Subject object, Locale locale) {
        return object.getName();
    }

    @Override
    public Subject parse(String id, Locale locale) throws ParseException {
        return subjectService.getSubject(id);
    }
}

And added the formatter in spring configuration :

@EnableWebMvc
@Configuration
@PropertySource(value = { "classpath:application.properties" })
@ComponentScan(basePackages = "com.vbera.main")
@EnableJpaRepositories(basePackages = "com.vbera.main")
public class SpringConfiguration extends WebMvcConfigurerAdapter {

    //Other bean definitions...

    @Bean(name="conversionService")
    public FormattingConversionService conversionService() {
        FormattingConversionServiceFactoryBean bean = new FormattingConversionServiceFactoryBean();
        bean.setRegisterDefaultFormatters(false);
        bean.setFormatters(getFormatters());
        return bean.getObject();
    }

    private Set<Formatter> getFormatters() {
        Set<Formatter> converters = new HashSet<Formatter>();
        converters.add(new SubjectFormatter());
        return converters;
    }
}

Still I'm getting below exception on submit :

Failed to convert property value of type java.lang.String to required type com.vbera.main.pojo.Subject for property subject

JSP view for Subject list rendering :

<form:label path="subject" for="subject">Subject</form:label>
<form:select path="subject" class="form-control input-md">
    <form:option value="">--- Select ---</form:option>
    <form:options items="${subjectList}" itemLabel="name"
        itemValue="id" />
</form:select>

回答1:


There are two parts in your mvc.xml file.

  1. Add the bean converion service bean:

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <set> <ref bean="MyFormatter1" /> <ref bean="MyFormatter2" /> </set> </property> <property name="converters"> <set> <ref bean="MyConverter1" /> <ref bean="MyConverter2" /> </set> </property> </bean>

  1. Refer this bean in thee annotation-driven tag.

<mvc:annotation-driven conversion-service="conversionService" />

Using the IDs is OK most of the time. However, in forms, it would force Spring to create an empty object with the null Id instead of a null object.




回答2:


I find out the solution from Spring 3.1 Form and List Binding, Java - SpringMVC - Get parameter in the controller link.

In one answer, it is suggested to have below jsp select tag :

<form:select path="subject.id" class="form-control input-md">
<form:option value="">--- Select ---</form:option>
<form:options items="${subjectList}" itemLabel="name"
    itemValue="id" />
</form:select>


来源:https://stackoverflow.com/questions/25861430/how-to-register-formatters-in-spring-mvc-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!