SpringMVC 向jsp頁面?zhèn)鬟f數(shù)據(jù)庫讀取到的值方法
在開發(fā)過程中,我們經(jīng)常需要將數(shù)據(jù)庫查詢到的值放入jsp頁面進行顯示,在springmvc的controller中,我們采用request將數(shù)據(jù)傳遞過去。
思路:
1、在comtroller中調(diào)用service層的方法獲取數(shù)據(jù)庫的數(shù)據(jù),并且將其通過modelandview的addObject方法放置到域中
2、在jsp頁面中通過jsp標簽進行讀取
開發(fā)controller.java文件:
//查詢所有數(shù)據(jù)到頁面顯示 @RequestMapping("/dataAll") public ModelAndView dataAll()throws Exception{ //調(diào)用Service層進行數(shù)據(jù)查找 List<DataList> dataLists = dataService.finDataAll(); ModelAndView modelAndView = new ModelAndView(); //將數(shù)據(jù)放到request中 modelAndView.addObject("datasList", dataLists); //指定視圖 modelAndView.setViewName("/data/dataList"); return modelAndView; }
如上所示,程序通過如下這條代碼:
//將數(shù)據(jù)放到request中 modelAndView.addObject("datasList", dataLists);
將查詢到的數(shù)據(jù)放置到request中。
開發(fā)jsp頁面接收顯示數(shù)據(jù):
<%@ 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>數(shù)據(jù)查詢列表</title> </head> <body> <form action="${pageContext.request.contextPath }/data/dataAll.action" method="post"> 數(shù)據(jù)查詢: <table width="10%" border=1> <tr> <td><input type="text" name="num" placeholder="編號"/><br/></td> <td><input type="submit" value="查詢"/></td> </tr> </table> 數(shù)據(jù)列表: <table width="100%" border=1> <tr> <td>編號</td> <td>濕度</td> <td>溫度</td> <td>二氧化碳</td> <td>粉塵</td> <td>操作</td> </tr> <c:forEach items="${datasList }" 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" >修改</a></td> </tr> </c:forEach> </table> </form> </body> </html>
頁面通過如下代碼獲取信息,進而進行循環(huán)顯示:
<c:forEach items="${datasList }" 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" >修改</a></td> </tr> </c:forEach>
這里的dataList即為通過controller傳遞過來的Object的名字,包含dataList數(shù)據(jù)。
以上這篇SpringMVC 向jsp頁面?zhèn)鬟f數(shù)據(jù)庫讀取到的值方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java EasyExcel實現(xiàn)動態(tài)列解析和存表
這篇文章主要為大家介紹了java EasyExcel實現(xiàn)動態(tài)列解析和存表示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06Springboot以Repository方式整合Redis的方法
這篇文章主要介紹了Springboot以Repository方式整合Redis的方法,本文通過圖文并茂實例詳解給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04SpringBoot項目執(zhí)行腳本 自動拉取最新代碼并重啟的實例內(nèi)容
在本篇文章里小編給大家整理的是一篇關于SpringBoot項目執(zhí)行腳本 自動拉取最新代碼并重啟的實例內(nèi)容,有需要的朋友們參考下。2019-12-12