在jsp页面开头写上所要用的标签库
<%@ taglib prefix="s" uri="/struts-tags"%> <%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>在body内写上链接所需数据,包括数据库连接所需的JDBC(MYSQL8.0和5.0有不一样的jar包和不一样的写法,此处是MYSQL8.0)还有账号和密码
<sql:setDataSource var="snapshot" driver="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/equipmentmgr?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true" user="root" password="123456" />采用sql标签取出对应数据:
<sql:query dataSource="${snapshot}" var="result"> SELECT distinct BUILD FROM `d_area` </sql:query>因为区域信息中有重复的楼,所以说需要获取distinct buid 然后foreach循环:
<c:forEach var="row" items="${result.rows}"> <li class="layui"><c:out value="${row.build}"/>号楼 </li> </c:forEach>就可以展示楼号 同理对应的全部信息是依靠来显示
<div class="layui-tab-content"> <div class="layui-tab-item layui-show"> <div class="table-responsive"> <table class="layui-table" lay-skin="line" lay-size="lg" id="table"> <thead> <tr> <th>楼号</th> <th>层数</th> <th>操作</th> </tr> </thead> <tbody> <sql:query dataSource="${snapshot}" var="result2"> SELECT * FROM `d_area` </sql:query> <c:forEach var="row" items="${result2.rows}"> <tr> <td>第<c:out value="${row.build}"/>楼</td> <td>第<c:out value="${row.floor}"/>层</td> <td> <div class="layui-table-cell laytable-cell-1-0-10"> <a class="layui-btn layui-btn-normal layui-btn-xs">修改</a> <a class="layui-btn layui-btn-danger layui-btn-xs delete">删除</a> </div> </td> </tr> </c:forEach> </tbody> </table> </div> </div>最后是根据不同楼号来显示不同层数信息
<c:forEach var="row1" items="${result.rows}"> <!-- 首先获得楼号数据 --> <sql:query dataSource="${snapshot}" var="result3"> SELECT FLOOR FROM `d_area` where BUILD=${row1.build} </sql:query> <!-- 在获取对应楼号的楼层信息 --> <div class="layui-tab-item"> <div class="table-responsive"> <table class="layui-table" lay-skin="line" lay-size="lg" id="table"> <thead> <tr> <th>楼号</th> <th>层数</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach var="row2" items="${result3.rows}"> <tr> <td>第<c:out value="${row1.build}"/>号楼</td> <td>第<c:out value="${row2.floor}"/>层</td> <td> <div class="layui-table-cell laytable-cell-1-0-10"> <a class="layui-btn layui-btn-normal layui-btn-xs">修改</a> <a class="layui-btn layui-btn-danger layui-btn-xs delete">删除</a> </div> </td> </tr> </c:forEach> </tbody> </table> </div> </div> </c:forEach> <!-- foreach内循环foreach -->
有两个表role和user,user的user_role属性和role id相连接 期待结果是获取到所有role type=2的user name
<sql:query dataSource="${snapshot}" var="role"> SELECT * FROM `d_role` where TYPE='2' </sql:query> <div class="layui-tab layui-tab-card"> <ul class="layui-tab-title"> <li class="layui-this">全部</li> <c:forEach var="fix" items="${role.rows}"> <sql:query dataSource="${snapshot}" var="user"> SELECT * FROM `d_user` where USER_ROLE=${fix.ID} </sql:query> <c:forEach var="fixman" items="${user.rows}"> <li class="layui"><c:out value="${fixman.nickname}"/> </li> </c:forEach>> </c:forEach>效果