在javaweb crud小demo实现过程中,对于需要展示数据的界面,使用jsp文件+el表达式+jstl实现简洁又高效
并且目前个人小经验总结:可以将查询步骤放在jsp文件开头的程序代码区中实现,这样每次进来jsp数据显示界面就会使用查询方法查询出数据,就能实现每次都进入jsp界面都能查询一下最新的数据再显示了
注意:url中需要参数的话记得传入
举例
<%@ page import="com.lagou.service.ManagerService" %>
<%@ page import="com.lagou.bean.Class" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.lagou.bean.Manager" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>班级信息</title>
<link rel="stylesheet" href="WebContent/css/bootstrap.min.css">
</head>
<body>
<%
//每次进入该jsp界面都显示最新的信息:界面上面都根据班级名称查询班级信息
//1、获取前端传来的班级名称
String cName = request.getParameter("cName");
System.out.println("获取到的班级名称是:" + cName);
//2、根据班级名称在数据库中查询班级
ManagerService managerService = new ManagerService();
ArrayList<Class> classes = managerService.searchClassBycName(cName);
//3、之后使用jstl表达式将查询出来的班级显示出来
request.setAttribute("classes", classes);
%>
<h2 align="center">查询到的班级信息</h2>
<table class="table table-hover table-responsive">
<thead>
<th>班级名称</th>
<th>所属年级</th>
<th>班主任姓名</th>
<th>班级人数</th>
<th>班级口号</th>
<th>操作</th>
</thead>
<tbody>
<c:choose>
<c:when test="${requestScope.classes.size() > 0}">
<c:forEach var="aClass" items="${requestScope.classes}">
<tr>
<td>${aClass.cName}</td>
<td>${aClass.grade}</td>
<td>${aClass.tName}</td>
<td>${aClass.cNum}</td>
<td>${aClass.slogan}</td>
<td>
<a href="searchClassStu?cid=${aClass.cid}" class="btn btn-primary">查看班级学生</a>
<a href="updateClass.jsp?cid=${aClass.cid}" class="btn btn-warning">修改</a>
<a class="btn btn-danger">删除</a>
</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<span style="color:red; font-size: large">查无此班级,请返回重新查找</span>
</c:otherwise>
</c:choose>
</tbody>
</table>
</body>
</html>
<%@ page import="com.lagou.service.ManagerService" %>
<%@ page import="com.lagou.bean.Class" %>
<%@ page import="com.lagou.bean.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>班级中的所有学生</title>
<link rel="stylesheet" href="WebContent/css/bootstrap.min.css">
<script>
function deleteConfirm(sNum) {
if (window.confirm("确定要删除吗?")) {
location.href="deleteStu?sNum=" + sNum + "&jsp=showClassStu.jsp";
}
}
</script>
</head>
<body>
<%
//每次都要展示学生,在这里调用ManagerService中的查询方法查询该班级中的所有学生
ManagerService managerService = new ManagerService();
int cid = Integer.parseInt(request.getParameter("cid"));
ArrayList<Student> classStudents = managerService.searchAllStuByCid((Integer) session.getAttribute("mid"), cid);
request.setAttribute("searchStuByCidResult", classStudents);
%>
<table class="table table-hover table-responsive">
<caption>学生信息</caption>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>出生日期</th>
<th>邮箱</th>
<th>备注</th>
<th>所属班级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:choose>
<c:when test="${requestScope.searchStuByCidResult.size() == 0}">
<h2 style="color:red" align="center">此班级中没有查询到学生</h2>
</c:when>
<c:otherwise>
<c:forEach var="s" items="${requestScope.searchStuByCidResult}">
<tr>
<td>${s.sNum}</td>
<td>${s.stuName}</td>
<td>${s.gender}</td>
<td>${s.birthday}</td>
<td>${s.email}</td>
<td>${s.remark}</td>
<td>${s.cName}</td>
<td>
<a href="updateTransfer?sNum=${s.sNum}" class="btn btn-warning">修改</a>
<a class="btn btn-danger" onclick="deleteConfirm(${s.sNum})">删除</a>
</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</tbody>
</table>
<script type="text/javascript">
<c:if test="${requestScope.deleteResult != null}">
alert("${requestScope.deleteResult}");
</c:if>
</script>
</body>
</html>