DropDownlist如何使用Spring boot Mysql加载数据

DropDownlist how to load the data using Spring boot Mysql

我是 spring 引导和 mysql 的初学者。我需要加载 DropDownList。我不知道如何加载 them.what 到目前为止我已尝试 below.i 想在下拉列表中加载学生姓名。

index.html- 下拉加载

 <select class="form-control" name="example" id="example">
         <option value="0">ALL</option>
         <option th:each="Student : ${allStudents}"
                 th:value="${Student.id}"
                 th:selected="${Student.isSelected(lastselected)}"
                 th:text="${Student.studentname}">
         </option>
    </select>

学生Class

package com.example.StudentCrud.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String studentname;
    private String course;
    private int fee;
    public Student() {

    }
    public Student(Long id, String studentname, String course, int fee) {
    
        this.id = id;
        this.studentname = studentname;
        this.course = course;
        this.fee = fee;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getStudentname() {
        return studentname;
    }
    public void setStudentname(String studentname) {
        this.studentname = studentname;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
    public int getFee() {
        return fee;
    }
    public void setFee(int fee) {
        this.fee = fee;
    }

}

控制器我是这样写的。我坚持这个领域如何只获得学生姓名

 @ModelAttribute("allStudent")
        public List<Student> allUsers() {
            List<Student> userList= service.listAll();
            return userList;
        }

在这里,我使用 <form:select /> , <form:option /> and <form:options /> 标签来呈现 HTML 下拉框。并且 <c:forEach /> 循环每个学生。

不要忘记将下面的 taglib 导入到 jsp。

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

例如POST请求路径为/students。并且 modelAttribute="studentForm" 将用于绑定 Student POJO。

Index.jsp

<form:form modelAttribute="studentForm"
            action="${pageContext.request.contextPath}/students" method="POST">
            <form:select path="studentName">
                <form:option value="NONE" label="Select" />
                <c:forEach var="student" items="${allStudents}">
                    <form:option value="${student.id}" label="${student.studentName}"/>
                </c:forEach>
            </form:select>
</form:form>

顺便说一下,我使用了 studentName 的驼峰式命名法。

假设service.listAll()工作正常。当GET请求/students被调用时-

控制器像

@ModelAttribute(name = "studentForm")
public Student setUp() {
     return new Student();
}

@GetMapping
public String list(Model model) {
    List<Student> students = service.listAll();
    // add to model
   model.addAttribute("allStudent", students);
   // view
   return "index";
}

方法级别 @ModelAttribute 将在控制器中的任何其他方法之前被调用。要在form中使用studentForm模型属性,我们需要先进行初始化。您可以在不使用方法级别 @ModelAttribute.

的情况下执行此操作