Как выбрать «дочерний» объект из раскрывающегося списка в форме Spring MVC и вставить его в родительский объект с помощью установщика - PullRequest
0 голосов
/ 02 августа 2020

Я создаю простое приложение для управления системой clini c.

Я создал класс Patient, который связан с классом Doctor:

@Entity
@DiscriminatorValue("patient")
public class Patient extends Person{

    @ManyToOne(fetch = FetchType.LAZY,
            cascade = {
                    CascadeType.MERGE,
                    CascadeType.PERSIST,
                    CascadeType.REFRESH,
                    CascadeType.DETACH
            })
    private Doctor doctorInCharge;
    
    public Patient() {
        
    }

    public Doctor getDoctorInCharge() {
        return doctorInCharge;
    }

    public void setDoctorInCharge(Doctor doctorInCharge) {
        this.doctorInCharge = doctorInCharge;
    }

}

Я создал форму для добавления новых пациент:

дополнительный пациент. jsp

<h3>Add Patient</h3>
    
    <form:form action="savePatient" modelAttribute="patient" method="POST">
    
    <form:hidden path="id"/>
        <table>
            <tbody>
                *Inputs for String/Integer types*
                
                <tr>
                    <td><label>Select doctor in charge:</label></td>
                    <td>
                        <select name="List of doctors">
                            <c:forEach var="selectedDoctor" items="${doctorList}">
                                <option value="${selectedDoctor.id}">${selectedDoctor.surname}>
                                </option>
                            </c:forEach>
                        </select>
                    </td>   
                </tr>
                
                <tr>
                    <td><label></label></td>
                    <td><input type="submit" value="Add" class="save"/></td>
                </tr>
            </tbody>
        </table>
    </form:form>
*Other stuff*

Связанные методы контроллера:

@GetMapping("/addPatient")
public String showAddPatientForm(Model model) {
    Patient patient = new Patient();
    
    model.addAttribute("patient", patient);
    model.addAttribute("doctorList", doctorService.getAll());
    
    return "patient-add";
}

@PostMapping("/savePatient")
public String savePatient(@ModelAttribute("patient") Patient patient) {
    patientService.saveOrUpdate(patient);
    return "redirect:/patients/list";
}

Мне удалось создать раскрывающийся список, в котором отображается список всех врачей, которые находятся в моей базе данных

Теперь внутри savePatient() я хочу иметь возможность получить объект Doctor, который я выбрал из раскрывающегося списка, или я хочу, чтобы Spring автоматически вводил его в этот объект пациента с помощью установщика.

...