Why this Thymeleaf form binding with selection field does not work?

隐身守侯 提交于 2020-01-14 12:52:30

问题


I have two models, Patient and Study. In the Study model, I want to use Patient's Id as a foreign key. My Study Model (without getter/setter) is as below

@Entity
@Table(name = "Study")
public class Study {

    @Id
    @Column(name = "study_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "patient_id")
    private Patient patient;

    @NotNull
    @Column(name = "description")
    private String description;

    @NotNull
    @Column(name = "status")
    private String status;
} 

EDIT : Adding Patient class (without getter/setter) as well

@Entity
@Table(name="Patient")
public class Patient {

    @Id
    @Column(name="patient_id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @NotNull
    @Column(name="name")
    private String name;

    @Column(name="sex")
    private String sex;

    @Column(name="date_of_birth")
    private Date dateOfBirth;
}

I am using Thymeleaf and selection of patient in my scheduleStudy.html is shown below

<form method="POST" th:action="@{/scheduleStudy}" th:object="${study}">

    <p>
        Select Patient<select th:field="*{patient}"
            required="required" class="form-control">

            <option th:each="patient: ${patientList}" th:value="${patient.id}"
                th:text="${patient.name}"></option>
        </select>
    </p>

The form is loading successfully with list of Patients in dropdown. However, when I am submitting the form after filling out all the fields, I am receiving:

Validation failed for object='study'. Error count: 1 error on browser.

Also the controller entries for Study form

@GetMapping("/scheduleStudy")
    public String addSchedule(Model model) {
        List<Patient> patientList = patientService.getPatients();
        model.addAttribute("patientList", patientList);
        model.addAttribute("study", new Study());
        return "scheduleStudy";
    }

    @PostMapping("/scheduleStudy")
    public void processAddSchedule(@ModelAttribute Study study) {
        studyService.addStudy(study);

    }

I have just started using Thymeleaf and I think I have dome some mistake in the patient field. Could anyone please help?

EDIT 2: I have updated the method for POST request, but patient is still null in controller. The previous browser error is gone of course.

@RequestMapping(value = "/scheduleStudy", method = RequestMethod.POST)
    public String processAddSchedule(Model model, @Valid @ModelAttribute("study") Study study,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {

        } else {
            studyService.addStudy(study);
        }
}

回答1:


I was working on a similar task. As far as I learned when you post the study form Thymeleaf sends all fields as parameters to a POST request. It converts the Patient object to a String with toString(). Then when assembling the Study object it must convert the Patient back to the Object form. I solved this by registering 2 converters (toString and fromString) with the standard conversionService for Patient.



来源:https://stackoverflow.com/questions/51571933/why-this-thymeleaf-form-binding-with-selection-field-does-not-work

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