如何在 Spring MVC Web 应用程序中注册格式化程序? [英] How to register formatters in Spring MVC web application?

查看:23
本文介绍了如何在 Spring MVC Web 应用程序中注册格式化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型属性中绑定嵌套对象时遇到问题.我有一个类 Book,其中嵌套了类 Subject,如下所示:

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);
    }
}

并在 spring 配置中添加格式化程序:

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 视图:

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>

推荐答案

我从 Spring 3.1 表单和列表绑定Java - SpringMVC - 在控制器中获取参数 链接.

在一个答案中,建议在 jsp select 标签下面有:

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>

这篇关于如何在 Spring MVC Web 应用程序中注册格式化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆