Я новичок в Spring boot и планирую создать приложение School managem anet с использованием Spring boot и Mysql. Однако я застрял при отправке данных ученика в его категорию. Есть отдельная таблица под названием "Faculty", и я Отправим данные из таблицы «Студент». Проблема в том, что есть «Выбрать опцию» для отправки данных для указанного c факультета. Тем не менее, отображается неизвестная ошибка, которую я не понимаю почему? Буду признателен, если вы отправите мне полную информацию об отправке данных в категорию, используя опцию выбора в форме. Ниже того, что я пробовал:
StudentController.class
package io.adu.Controllers;
import java.lang.reflect.Method; import java.util.Arrays; import
java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import
org.springframework.stereotype.Controller; import
org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import
org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import
org.springframework.web.bind.annotation.PathVariable; import
org.springframework.web.bind.annotation.PostMapping; import
org.springframework.web.bind.annotation.RequestBody; import
org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import io.adu.DAO.Faculty;
import io.adu.DAO.Student;
import io.adu.DAO.User;
import io.adu.Repository.FacultyRepository;
import io.adu.Repository.StudentRepository;
import io.adu.Service.StudentService;
@Controller
public class StudentController {
@Autowired
StudentService service;
@Autowired
FacultyRepository repository;
@Autowired
StudentRepository studentRepository;
@GetMapping("/addStudent")
public String viewStudentsList(Model model) {
List<Student>students = studentRepository.findAll();
List<Faculty>faculties = repository.findAll();
model.addAttribute("addstudent",true);
model.addAttribute("students",students);
model.addAttribute("faculties",faculties);
return "addStudent";
}
@PostMapping("/addStudent")
public String showStudents(@Valid @ModelAttribute("addStudent")Student student,BindingResult result,ModelMap model){
Student student = new Student();
List<Faculty>faculties = repository.findAll();
model.addAttribute("Student",student);
model.addAttribute("faculty",new faculty());
model.addAttribute("addstudent",true);
service.addStudent(student);
return "addStudent";
}
StudentEntity.class
@Entity
@Table(name="student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="student_id")
private int id;
@Column(name="student_name")
private String name;
@Column(name="student_email")
private String email;
//@Column(name="student_name")
//private String faculty;
@Column(name="paid_amount")
private String paidamount;
@GeneratedValue(strategy=GenerationType.IDENTITY)
@ManyToOne
@JoinTable(name = "faculty_students",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "faculty_id"))
private Faculty faculty;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFaculty() {
return faculty;
}
public void setFaculty(String faculty) {
this.faculty = faculty;
}
public String getPaidamount() {
return paidamount;
}
public void setPaidamount(String paidamount) {
this.paidamount = paidamount;
}
}
StudentForm. html
<form th:action="@{/addStudent}" th:Object="${addStudent}" method="post">
<div class="card-body card-block">
<div class="form-group">
<label for="company" class=" form-control-label">Ismi</label>
<input type="text" id="company" placeholder="Talaba ismini kiriting" class="form-control">
</div>
<div class="form-group">
<label for="vat" class=" form-control-label">To'langan summa</label>
<input type="text" id="vat" class="form-control">
</div>
<div class="form-group">
<label for="street" class=" form-control-label">fakulteti</label>
<select class="form-control" th:field="${addStudent.faculties}" th:disabled="${disableFields}">
<option th:each="faculty :${faculties}" th:value="${faculty.facultyId}" th:text="${faculty.FacultyName}">
</option>
</select>
</div>
<div class="row form-group">
<div class="col-8">
<div class="form-group">
<label for="city" class=" form-control-label">Elektron pochtasi</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-8">
<div class="form-group">
<label for="postal-code" class=" form-control-label">Status</label>
<input type="text" placeholder="Budjed-Kontrakt" class="form-control">
</div>
</div>
</div>
<div class="form-group">
<label for="country" class=" form-control-label">Tugilgan Sana</label>
<input type="text" id="country" class="form-control">
</div>
<button type="submit" class="btn btn-primary btn-sm">
<i class="fa fa-dot-circle-o"></i> Submit
</button>
</div> </form>
Ошибка журнала
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringSelectFieldTagProcessor' (template: "addStudent" - line 299, col 71)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
... 85 common frames omitted
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'addStudent' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:258) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
... 98 common frames omitted
2020-02-03 21:02:59.704 ERROR 13336 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/addStudent.html]")] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'addStudent' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:258) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]