During the development process, we often need to put the values queried by the database into the jsp page for display. In the controller of springmvc, we use request to pass the data over.
Ideas:
1. Call the service layer method in the comtroller to obtain the database data and place it in the domain through the addObject method of the modelview.
2. Read through the jsp tag in the jsp page
Develop controller.java file:
//Query all data to the page to display @RequestMapping("/dataAll")public ModelAndView dataAll()throws Exception{//Calling the Service layer for data search List<DataList> dataLists = dataService.finDataAll();ModelAndView modelAndView = new ModelAndView();//Put the data into the request modelAndView.addObject("datasList", dataLists);//Specify the view modelAndView.setViewName("/data/dataList");return modelAndView;}As shown above, the program passes the following code:
//Put the data into the request modelAndView.addObject("datasList", dataLists);Place the query data into the request.
Develop jsp page to receive display data:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Data query list</title></head><body> <form action="${pageContext.request.contextPath }/data/dataAll.action" method="post">Data query: <table border=1><tr><td><input type="text" name="num" placeholder="number"/><br/></td><td><input type="submit" value="query"/></td></tr></table>Data list: <table border=1><tr><td>Num</td><td>Humidity</td><td>Temperature</td><td>Carbon dioxide</td><td>Dust</td><td>Operation</td></tr><c:forEach items="${dataList }" var="data"><tr><td>${data.num }</td><td>${data.hum }</td><td>${data.tem }</td><td>${data.co }</td><td>${data.fc }</td><td><a href="${pageContext.request.contextPath }/data/editDatas.action?num=${data.num}" rel="external nofollow" rel="external nofollow" >Modify</a></td></tr></c:forEach></table></form></body></html>The page obtains information through the following code and then displays it cycle:
<c:forEach items="${dataList }" var="data"> <tr> <td>${data.num }</td> <td>${data.hum }</td> <td>${data.tem }</td> <td>${data.co }</td> <td>${data.fc }</td> <td><a href="${pageContext.request.contextPath }/data/editDatas.action?num=${data.num}" rel="external nofollow" rel="external nofollow" >Modify</a></td> </tr> </c:forEach>The dataList here is the name of the Object passed through the controller, which contains the dataList data.
The above article SpringMVC passes the value read by the database to the jsp page is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.