个人经验:javaweb crud实现过程中需要展示数据界面代码实现总结

在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> &nbsp;
                            <a href="updateClass.jsp?cid=${aClass.cid}" class="btn btn-warning">修改</a> &nbsp;
                            <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> &nbsp;&nbsp;
                        <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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值