通过jsp与el对数据进行查询(引入jstl)

通过jsp与el对数据进行查询

servlet对数据进行连接

package com.mab.sevelet;

import com.mab.dao.EmpDao;
import com.mab.dao.impl.EmpDaoImpl;
import com.mab.pojo.Emp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/httpServlet.do")
public class servlet extends HttpServlet {
    //做一个对象empDao,去调用方法EmpDaoImpl()
    EmpDao empDao = new EmpDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //对象empDao再调用具体方法findall();返回查询的数据给到findall,以数据队列的形式存储
        List<Emp> list = empDao.findall();
        //把数据再发送到session中,页面缓存中
        req.setAttribute("emps",list);
        //当收到的数据时,我们进行跳转到页面index.jsp进行展示
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("index.jsp");
        //发送响应和请求
        requestDispatcher.forward(req,resp);


    }
}

优化JSP内容

<%--引入jstl的核心文件包和格式包--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Title</title>
    <style>
      table{
        width: 80%;
        border:4px solid  red;
        margin: 0px auto;
      }
        td,th{
          border: 3px solid green;
        }
    </style>
  </head>
  <body>
    <table cellspacing="0px" cellpadding="0px">
      <tr>
        <th>排名</th>
        <th>员工编号</th>
        <th>员工姓名</th>
        <th>员工职位</th>
        <th>上级编号</th>
        <th>入职日期</th>
        <th>薪资</th>
        <th>补贴</th>
        <th>部门编号</th>
        <th>薪资等级</th>
      </tr>
      <c:forEach items="${emps}" var="empt" varStatus="emptStatus">
        <tr>
            <%--el代码格式替换--%>
          <td>${emptStatus.count}</td>
          <td>${empt.empno}</td>
          <td>${empt.ename}</td>
          <td>${empt.mgr}</td>
          <td>${empt.job}</td>
          <td>
              <fmt:formatDate value="${empt.hiredate}" pattern="yyyy年MM月dd日"></fmt:formatDate>
          </td>
          <td>
           &yen;<fmt:formatNumber value="${empt.sal}" pattern="#,##0,000.00" ></fmt:formatNumber>
          </td>
          <td>${empt.comm}</td>
          <td>${empt.deptno}</td>
            <%--<td><%=emp.getEmpno()%></td>
            <td><%=emp.getEname()%></td>
            <td><%=emp.getMgr()%></td>
            <td><%=emp.getJob()%></td>
            <td><%=emp.getHiredate()%></td>
            <td><%=emp.getSal()%></td>
            <td><%=emp.getComm()%></td>
            <td><%=emp.getDeptno()%></td>
            <td>--%>
          <td>
            <c:choose>
            <c:when test="${empt.sal le 500}">A</c:when>
            <c:when test="${empt.sal le 1000}">B</c:when>
            <c:when test="${empt.sal le 1500}">C</c:when>
            <c:when test="${empt.sal le 2000}">D</c:when>
            <c:when test="${empt.sal le 3000}">E</c:when>
            <c:when test="${empt.sal le 4000}">F</c:when>
            <c:when test="${empt.sal gt 4000}">G</c:when>
            </c:choose>
              <%--${empt.sal le 500 ? "A":""}
              ${empt.sal le 1000 and empt.sal gt 500 ? "B":""}
              ${empt.sal le 1500 and empt.sal gt 1000 ? "C":""}
              ${empt.sal le 2000 and empt.sal gt 1500 ? "D":""}
              ${empt.sal le 3000 and empt.sal gt 2000 ? "E":""}
              ${empt.sal le 4000 and empt.sal gt 3000 ? "F":""}
              ${empt.sal gt 4000 ? "G":""}--%>
              <%--<%
                Double sal = emp.getSal();
                if(sal<=500){
                  out.print("A");
                }else if( sal <=1000){
                  out.print("B");
                }else if( sal <=1500){
                  out.print("C");
                }else if( sal <=2000){
                  out.print("D");
                }else if( sal <=3000){
                  out.print("E");
                }else if( sal <=4000){
                  out.print("F");
                }else {
                  out.print("G");
                }
              %>--%>
        </tr>
      </c:forEach>
        <%--<% List<Emp> emps =(List<Emp>) request.getAttribute("emps");
          for (Emp emp : emps) {
            pageContext.setAttribute("empt",emp);
        %>--%>

           <%-- <%
          }
        %>--%>
    </table>
  </body>
</html>

posted @ 2022-05-28 11:27  爱豆技术部  阅读(63)  评论(0)    收藏  举报