Issue
i am beginner of spring boot and mysql. i need to load DropDownList. i don't know how to load them.what tried so far i attached below.i want load the student name on the dropdown.
index.html- DropDown load
<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>
Student 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;
}
}
Contorller i wrote like this. i stuck with this area how to get the Student names only
@ModelAttribute("allStudent")
public List<Student> allUsers() {
List<Student> userList= service.listAll();
return userList;
}
Solution
Here, I'm using <form:select /> , <form:option /> and <form:options />
tags to render HTML dropdown box. And <c:forEach />
for loop each student.
Don't forget to import below taglib to jsp.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
For example, POST request path is /students
. And modelAttribute="studentForm"
will be used to bind 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>
By the way, I used camelCase for studentName
.
Supposed service.listAll()
works fine. When GET request /students
is called -
Controller be like
@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";
}
Method level @ModelAttribute
will be invoked before any other method in controller. To use studentForm
model attribute in form
, we need to initialize first. You can do it without using method level @ModelAttribute
.
Answered By - Byaku