Stack Overflow на русском Asked by bylbyl9tor on January 7, 2021
Всем привет, не получается отправить записать данные из формы в базу данных.
Вот класс сущности которую я хочу записать в бд.
@Entity
@Table(name = "schedule_table")
public class Schedule {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Schedule() {
}
public Schedule(Teacher teacher,Group group, LessonDate date, Subject subject, Classroom classroom, LessonTime lessonTime, LessonType lessonType) {
this.teacher = teacher;
this.group = group;
this.date = date;
this.subject = subject;
this.classroom = classroom;
this.lessonTime = lessonTime;
this.lessonType = lessonType;
}
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "id_teacher", nullable = false)//specialty->faculty
private Teacher teacher;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "id_group", nullable = false)
private Group group;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "id_date", nullable = false)//specialty->faculty
private LessonDate date;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "id_subject", nullable = false)
private Subject subject;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "id_classroom")
private Classroom classroom;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "id_time")
private LessonTime lessonTime;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "id_lesson_type", nullable = false)//specialty->faculty
private LessonType lessonType;
//getters
//setters
}
вот форма с фронтэнда которую я пытаюсь отправить. Я хочу чтобы активный элемент из select отправлялся как поле объекта Schedule
<form name="search-theme-form" action="#" th:action="@{/admin/add}" id="search-theme-form"
th:object="${schedule}" method='POST'>
<select id="dropdown_group" th:field="*{group}">
<option th:text="${groupList.groupName}" th:value="${groupList.id}"></option>
</select>
<td>
<select id="dropdown_date" th:field="*{date}">
<option th:text="${dateList.dateName}" th:value="${dateList.id}"></option>
</select>
</td>
<td>
<select id="dropdown_lesson_time" th:field="*{lessonTime}">
<option th:each="time : ${timeAll}"
th:value="${time.id}"
th:text="${time.lessonTimeStart+' '+time.lessonTimeEnd}">
</option>
</select>
</td>
<td>
<select id="dropdown_subject" th:field="*{subject}">
<option th:each="subj : ${subjectAll}"
th:value="${subj.id}"
th:text="${subj.subjectName}">
</option>
</select>
</td>
<td>
<select id="dropdown_teacher" th:field="*{teacher}">
<option th:each="teach : ${teacherAll}"
th:value="${teach.id}"
th:text="${teach.teacherName}">
</option>
</select>
</td>
<td>
<select id="dropdown_classroom" th:field="*{classroom}">
<option th:each="class : ${classroomAll}"
th:value="${class.id}"
th:text="${class.classroomNumber}">
</option>
</select>
</td>
<td>
<select th:field="*{lessonType}">
<option th:each="lessonT : ${lessonTypeAll}"
th:value="${lessonT.id}"
th:text="${lessonT.lessonType}">
</option>
</select>
</td>
<td><input type="submit" value="Submit"/></td>
</form>
Вот код моего контроллера который отображает страницу с этой формой(отправляет туда данные, которые будут стоять в тегах select)
@GetMapping("/{faculty}/{specialty}/{group}/{date}")
public String getPutPage(@PathVariable String faculty,
@PathVariable String specialty,
@PathVariable String group,
@PathVariable String date,
Model model) {
model.addAttribute("rows", scheduleService.findAllByGroupAndDate(groupService.returnGroupObjectByParams(group,specialty,faculty), lessonDateRepository.findByDateName(date)));
model.addAttribute("groupList", this.groupService.returnGroupObjectByParams(group,specialty,faculty));
model.addAttribute("dateList", this.lessonDateRepository.findByDateName(date));
model.addAttribute("timeAll", this.timeService.findAll());
model.addAttribute("subjectAll", this.subjectService.findAll());
model.addAttribute("teacherAll", this.teacherService.findAll());
model.addAttribute("classroomAll", this.classroomServiceImpl.findAllBy());
model.addAttribute("lessonTypeAll", this.lessonTypeService.findAll());
model.addAttribute("schedule", new Schedule());
return "table-admin";
}
A код ниже принимает данные формы и записывает данные в базу данных. Но этого не происходит, выдаётся ошибка Validation failed for object=’schedule’. Error count: 5
@PostMapping(value = "/add")
public String putMyData( @ModelAttribute("schedule") Schedule schedule) throws UnsupportedEncodingException {
scheduleService.save(schedule);
System.out.println();
return returnUrl();
}
Как я понимаю, скорее всего ошибка в форме, именно в заполнении полей, буду рад любой помощи, или примеру реализации похожей на мою ситуацию.
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP