本文介绍了为什么此Thymeleaf表单与选择字段绑定不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,PatientStudy.在Study模型中,我想使用Patient的ID作为外键.我的Study模型(没有getter/setter)如下所示

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

编辑:还添加了耐心的课程(没有getter/setter)

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

我正在使用Thymeleaf,并且在scheduleStudy.html中选择患者的情况如下所示

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:

也是Study形式的控制器条目

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

    }

我刚开始使用Thymeleaf,我认为在patient字段中有一些错误.谁能帮忙吗?

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

编辑2 :我已经更新了POST请求的方法,但是patient在控制器中仍然为空.当然,先前的浏览器错误已消失.

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

推荐答案

我正在从事类似的任务.据我所知,当您发布研究表格时,Thymeleaf将所有字段作为参数发送到POST请求.它使用toString()将Patient对象转换为String.然后,在组装研究对象时,必须将患者转换回对象形式.我通过向Patient的标准conversionService注册2个转换器(toString和fromString)解决了此问题.

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.

这篇关于为什么此Thymeleaf表单与选择字段绑定不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 12:18