Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass the database values to jsp in Spring MVC

I am beginner to mongodb and spring mvc.I have created a collection in mongodb.finally i got that collection in my controller.now I want to know how to pass that collection values to my jsp page.

Model: Student.java

 @Document(collection = "Studentlist")
 public class Student {
     private String studentID;
     private String studentName;
     public void setcustomerID(String studentID) {
         this.studentID = studentID;
     }
     public String getstudentID() {
         return studentID;
     }
     public void setcustomerName(String studentName) {
         this.studentName = studentName;
     }
     public String getstudentName() {
         return studentName;
     }
 }

homecontroller.java

 @RequestMapping(value = "/student", method = RequestMethod.GET)
 @ResponseBody
 public List<Student> getStudentList() throws Exception {
     List<Student> studentList = StudentBO.findAllstudentList();
     return studentList;
 }

home.jsp

<label for="CustomerName">Customer</label>
<select class="form-control">
<option>student id value from db</option>
<option>student name value from db</option>

How to pass the student name and id values to my dropdown?

like image 716
user5863037 Avatar asked Feb 01 '26 09:02

user5863037


1 Answers

In the controller, you can add the model attribute with the map or list values like this:

model.addAttribute("studentList ", studentList );  

In the JSP file, you can do the following:

<c:forEach items="${studentList}" var="record">
    <td><c:out value="${record.studentID}" /></td>
    <td><c:out value="${record.studentName}" /></td>
</c:forEach>
like image 87
shraddha bhardwaj Avatar answered Feb 03 '26 22:02

shraddha bhardwaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!