user5863037
user5863037

Reputation: 79

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?

Upvotes: 3

Views: 2289

Answers (4)

shraddha bhardwaj
shraddha bhardwaj

Reputation: 145

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>

Upvotes: 1

nnunes10
nnunes10

Reputation: 550

The first step is to remove the @ResponseBody annotation from your getStudentList method.

You use @ResponseBody when you have a JS framework to develop your presentation layer. In this case, you seems to be using only Spring MVC framework.

Your code should look like this:

homecontroller.java

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

The handler's method above fill the model with all students. Then it returns the logical name of the view. In this way, the view (JSP page) can access the model data.

home.jsp

 <select class="form-control">
   <c:forEach items="${studentList}" var="student">
     <option value="${student.studentID}">${student.studentName}</option>
   </c:forEach>
 </select>

Upvotes: 0

Pradeep
Pradeep

Reputation: 1

The data you are returning is json . So could you please try with javascript or jquery to manipulate your data.

Upvotes: 0

DamianoPantani
DamianoPantani

Reputation: 1386

<jsp:useBean class="com.package.HomeController" id="controller" scope="session"/>

<select class="form-control">
    <c:forEach items="${controller.getStudentList()}" var="student">
        <option value="${student.id}">${student.id} - ${student.name}</option>
    </c:forEach>
</select>

Upvotes: 0

Related Questions