十六、·实现显示所有雇员

1.实现跳转,在mainFrame.jsp中,修改显示所有雇员的链接

<a href="${pageContext.request.contextPath}/employee.do?flag=goAddEmp">添加雇员</a><br/>
    <a href="${pageContext.request.contextPath}/employee.do?flag=showAllEmp">显示雇员</a><br/>
    <a href="#">查询雇员</a><br/>
    <a href="#">退出系统</a><br/>

2.在EmployeeService中增加showAllEmp函数

public ActionForward showAllEmp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        List emps = employeeService.excuteQuery("from Employee", null);

        request.getSession().setAttribute("emplist", emps);
        return mapping.findForward("showAllEmp");
    }

3.在struts-config.xml文件中配置forward

<action path="/login" parameter="flag" name="employeeForm">
            <forward name="ok" path="/WEB-INF/mainFrame.jsp"></forward>
            <forward name="err" path="/WEB-INF/login.jsp"></forward>
        </action>
        
        <action path="/employee" parameter="flag" name="employeeForm">
        <!-- 跳往雇员添加界面 -->
            <forward name="goAddEmp" path="/WEB-INF/addEmpUI.jsp"></forward>
        <!-- 跳往显示所有雇员的界面 -->
            <forward name="showAllEmp" path="/WEB-INF/showAllEmp.jsp"></forward>
        </action>

4.在WEB-INF下新建showAllEmp.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'showAllEmp.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <h2>用户列表</h2>
    <table border="1px">
        <tr><td>id</td><td>姓名</td><td>grade</td><td>email</td><td>薪水</td><td>修改</td><td>删除</td></tr>
        <c:forEach items="${emplist}" var="emp">
            <tr><td>${emp.id}</td><td>${emp.name}</td><td>${emp.grade}</td><td>${emp.email}</td><td>${emp.salary }</td><td><a href="#">修改</a></td><td><a href="#">删除</a></td></tr>
        </c:forEach>
    </table>
  </body>
</html>

 

5.实现分页

  5.1在EmployeeServiceInterface中新增方法

public interface EmployeeServiceInterface extends BasicServiceInterface{
    public Employee loginCheck(Employee e);//登录验证
    
    public int getPageCount(int pageSize);//一共能够分几页
    
    public List showAllEmp(int pageNow,int pageSize);//分页显示所有雇员
    
}

  5.2EmployeeService中重写showAllEmp(int pageNow,int pageSize)方法

public List showAllEmp(int pageNow, int pageSize) {
        // TODO Auto-generated method stub
        String hql="from Employee order by id";
        List list = excuteQueryByPage(hql, null, pageNow, pageSize);
        return list;
    }

  5.3EmployeeService中重写getPageCount(int pageSize)方法

//获取一共能分多少页
    public int getPageCount(int pageSize){
        String hql="select count(*) from Employee";
        Object obj = uniqueQuery(hql, null);
        
        int empNum = Integer.parseInt(obj.toString());//获取雇员总个数
        
        System.out.println(empNum+"/"+pageSize+""+Math.ceil(empNum/pageSize));
        return (empNum-1)/pageSize+1;//一共能分几页
    }

  5.4EmployeeAction中重写showAllEmp方法

public ActionForward showAllEmp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub
        int pageCount=employeeService.getPageCount(2);
        System.out.println(pageCount);
        request.setAttribute("pageCount", pageCount);
        int pageNow=1;
        if(request.getParameter("pageNow")!=null){
            pageNow=Integer.parseInt(request.getParameter("pageNow").toString());
        }
        List list = employeeService.showAllEmp(pageNow,2);
        request.setAttribute("emplist", list);
        
        return mapping.findForward("showAllEmp");
    }

  5.5重写showAllEmp.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'showAllEmp.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <h2>用户列表</h2>
    <table border="1px">
        <tr><td>id</td><td>姓名</td><td>grade</td><td>email</td><td>薪水</td><td>修改</td><td>删除</td></tr>
        <c:forEach items="${emplist}" var="emp">
            <tr><td>${emp.id}</td><td>${emp.name}</td><td>${emp.grade}</td><td>${emp.email}</td><td>${emp.salary }</td><td><a href="#">修改</a></td><td><a href="#">删除</a></td></tr>
        </c:forEach>
    </table>
    <c:forEach var="i" begin="1" end="${pageCount}">
        <a href="${pageContext.request.contextPath}/employee.do?flag=showAllEmp&pageNow=${i}">${i }</a>
    </c:forEach>
  </body>
</html>

 

posted @ 2018-02-12 15:58  菜鸡蔡文姬  阅读(203)  评论(0编辑  收藏  举报