5月14日 CMS 周一

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helloworld_1996/article/details/80316562
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'list.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">
    -->

  <script type="text/javascript" src="js/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        function gopage(cpage){
            var mohu = $("#mohu").val();
            location = "teacher?m=list&cpage="+cpage+"&mohu="+mohu;
        }
        function mohu(){
            var mohu = $("#mohu").val();
            location = "teacher?m=list&mohu="+mohu;
        }
    </script>
  </head>

  <body>
    <table>
    <tr>
        <th colspan="21">
            <input id="mohu" value="${mohu}">
            <button onclick="mohu()">搜索</button>
            <button onclick="location='add.jsp'">添加</button>
        </th>
    </tr>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>pwd</th>
            <th>age</th>
            <th>tel</th>
            <th>qq</th>
            <th>birthday</th>
            <th>cid</th>
            <th>dname</th>
        </tr>
        <c:forEach var="s" items="${tealist}">
            <tr>
                <td>${s.id}</td>
                <td>${s.name}</td>
                <td>${s.pwd}</td>
                <td>${s.age}</td>
                <td>${s.tel}</td>
                <td>${s.qq}</td>
                <td>${s.birthday}</td>
                <td>${s.cid}</td>
                <td>${s.dname}</td>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="21">
                <input type="button" value="首页" onclick="gopage(1)">
                <input type="button" value="上页" onclick="gopage(${pu.prevPage})">
                <input type="button" value="下页" onclick="gopage(${pu.nextPage})">
                <input type="button" value="末页" onclick="gopage(${pu.totalPage})">
                ${pu.cpage}/${pu.totalPage}页--共${pu.count}条数据
            </th>

        </tr>
    </table>
  </body>
</html>
package com.www.entity;

public class Teacher {
    private int id;
    private String name;
    private String pwd;
    private int age;
    private String tel;
    private String qq;
    private String birthday;
    private int cid;
    private String dname;
    public Teacher(int id, String name, String pwd, int age, String tel,
            String qq, String birthday, int cid, String dname) {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.age = age;
        this.tel = tel;
        this.qq = qq;
        this.birthday = birthday;
        this.cid = cid;
        this.dname = dname;
    }
    public Teacher() {
        super();
        // TODO Auto-generated constructor stub
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getQq() {
        return qq;
    }
    public void setQq(String qq) {
        this.qq = qq;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public int getCid() {
        return cid;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + ", pwd=" + pwd
                + ", age=" + age + ", tel=" + tel + ", qq=" + qq
                + ", birthday=" + birthday + ", cid=" + cid + ", dname="
                + dname + "]";
    }


}
package com.www.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.www.entity.Teacher;
import com.www.util.PageUtils;

public class TeacherDao {

    public List<Teacher> teaList(PageUtils pu, String mohu) {
        // TODO Auto-generated method stub
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<Teacher> tealist = new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql:///cms02","root","root");

            String sql = "select a.*,b.dname from t_teacher a left join t_dept b on a.cid = b.did " ;

            if(mohu!=null){
                sql += " where name like '%"+mohu+"%' "; 
            }

            sql += " limit "+pu.getStartIndex()+","+pu.getPageSize();

            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();

            while(rs.next()){
                tealist.add(new Teacher(rs.getInt("id"), rs.getString("name"), rs.getString("pwd"), rs.getInt("age"), rs.getString("tel"), rs.getString("qq"), rs.getString("birthday"), rs.getInt("cid"), rs.getString("dname")));
            }

        } catch (Exception e) {
            // TODO: handle exception
        }

        return tealist;
    }

    public Integer count(String mohu) {
        // TODO Auto-generated method stub
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        Integer count = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql:///cms02","root","root");
            String sql = "select count(*) from t_teacher ";
            if(mohu!=null){
                sql += " where name like '%"+mohu+"%' ";
            }
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();

            if(rs.next()){
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return count;
    }

}
package com.www.service;

import java.util.List;

import com.www.dao.TeacherDao;
import com.www.entity.Teacher;
import com.www.util.PageUtils;

public class TeacherService {
    TeacherDao dao = new TeacherDao();

    public List<Teacher> teaList(PageUtils pu, String mohu) {
        // TODO Auto-generated method stub
        return dao.teaList(pu,mohu);
    }

    public Integer count(String mohu) {
        // TODO Auto-generated method stub
        return dao.count(mohu);
    }
}
package com.www.servlet;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.www.entity.Teacher;
import com.www.service.TeacherService;
import com.www.util.PageUtils;

public class TeacherSerevlet extends HttpServlet {
    TeacherService service = new TeacherService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        String m = request.getParameter("m");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        if("list".equals(m)){
            teaList(request,response);
        }

    }
    private void teaList(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        String cpage = request.getParameter("cpage");
        String mohu = request.getParameter("mohu");
        if(cpage == null){
            cpage = "1";
        }

        Integer pageSize = 2; 
        Integer count = service.count(mohu); 

        PageUtils pu = new PageUtils(Integer.valueOf(cpage), pageSize, count);

        List<Teacher> tealist = service.teaList(pu,mohu);

        request.setAttribute("pu", pu);
        request.setAttribute("tealist", tealist);
        request.setAttribute("mohu", mohu);
        request.getRequestDispatcher("list.jsp").forward(request, response);

    }
}
package com.www.util;

public class PageUtils {

    private Integer cpage;//褰撳墠椤�
    private Integer pageSize;//姣忛〉灞曠ず鏈�ぇ鏉℃暟
    private Integer count;//鏁版嵁鎬绘潯鏁�
    private Integer totalPage;//鎬婚〉鏁�
    private Integer startIndex;//璧峰涓嬫爣
    private Integer prevPage;//涓婁竴椤�
    private Integer nextPage;//涓嬩竴椤�
    //浣跨敤姝ゅ伐鍏风被锛岃皟鐢ㄨ繖涓瀯閫犲櫒
    public PageUtils(Integer cpage, Integer pageSize, Integer count) {
        this.cpage = cpage;
        this.pageSize = pageSize;
        this.count = count;
        calPrevPage();
        calStartIndex();
        calTotalPage();
        calNextPage();
    }
    private void calTotalPage(){//璁$畻鎬婚〉鏁�
        this.totalPage = count/pageSize + (count%pageSize==0?0:1);
    }
    private void calStartIndex(){//璁$畻璧峰涓嬫爣
        this.startIndex = (cpage-1)*pageSize;
    }
    private void calPrevPage(){//璁$畻涓婁竴椤�
        this.prevPage = cpage==1?1:(cpage-1);
    }
    private void calNextPage(){//璁$畻涓嬩竴椤�
        this.nextPage = cpage.equals(totalPage)?totalPage:(cpage+1);
    }
    public Integer getCpage() {
        return cpage;
    }
    public void setCpage(Integer cpage) {
        this.cpage = cpage;
    }
    public Integer getPageSize() {
        return pageSize;
    }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
    public Integer getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }
    public Integer getStartIndex() {
        return startIndex;
    }
    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }
    public Integer getPrevPage() {
        return prevPage;
    }
    public void setPrevPage(Integer prevPage) {
        this.prevPage = prevPage;
    }
    public Integer getNextPage() {
        return nextPage;
    }
    public void setNextPage(Integer nextPage) {
        this.nextPage = nextPage;
    }
}

不疯魔不成活
“不疯魔不成活”指的是一种职业精神,敬业.更深一步,“不疯魔不成活”是一种境界,一种极痴迷的境界,无论对戏还是对事物具有一种深深的迷恋,这种迷恋让人深陷其中,如痴如醉,忘我地全身心付出。只有达到痴迷的境界 才能将某件事做到极致
人生总要有一股疯狂的劲头。为了自己的目标、理想、信仰,竭尽全力,才不枉活了一生。

阅读更多

没有更多推荐了,返回首页