我是 mongodb 和 spring mvc 的初学者。我在 mongodb 中创建了一个集合。最后我在我的 Controller 中得到了那个集合。现在我想知道如何将该集合值传递到我的 jsp 页面。
模型: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>
如何将学生的 name
和 id
值传递到我的下拉列表中?
最佳答案
在 Controller 中,您可以使用映射或列表值添加模型属性,如下所示:
model.addAttribute("studentList ", studentList );
在 JSP 文件中,您可以执行以下操作:
<c:forEach items="${studentList}" var="record">
<td><c:out value="${record.studentID}" /></td>
<td><c:out value="${record.studentName}" /></td>
</c:forEach>
关于java - 在 Spring MVC 中将数据库值传递给 jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35175229/