Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring MVC web应用程序中注册格式化程序?_Java_Spring_Spring Mvc_Web Applications_Formatter - Fatal编程技术网

Java 如何在Spring MVC web应用程序中注册格式化程序?

Java 如何在Spring MVC web应用程序中注册格式化程序?,java,spring,spring-mvc,web-applications,formatter,Java,Spring,Spring Mvc,Web Applications,Formatter,我在绑定模型属性中的嵌套对象时遇到了这个问题。我有一本课程手册,其中我嵌套了以下课程主题: @Entity @Table(name = "book") public class Book { // Other properties... // With all getter setter.. @ManyToOne @JoinColumn(name="subject_id",nullable=false) @NotBlank(message = "Plea

我在绑定模型属性中的嵌套对象时遇到了这个问题。我有一本课程手册,其中我嵌套了以下课程主题:

@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;
}
@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);
    }
}
我还为主题实现了格式化程序类,如下所示:

@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;
}
@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);
    }
}
主题列表呈现的JSP视图:

<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>
主题
---挑选---

我从link中找到了解决方案

在一个答案中,建议使用以下jsp选择标记:

<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>

---挑选---

您的
mvc.xml
文件中有两个部分

  • 添加bean转换服务bean:
  • 
    
  • 在注释驱动标记中引用这个bean

    大多数情况下,使用IDs是可以的。然而,在表单中,它将强制Spring创建一个Id为null的空对象,而不是null对象

    <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>