5月13日 CMS 周日

<%@ 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="student?m=list&cpage="+cpage+"&mohu="+mohu;
        }
        function jumppage(){
            var cpage = $("#d1").val();
            if(cpage>=1 && cpage<= "${pu.totalpage}"){
                gopage(cpage);
            }else{
                alert("输入有误");
            }
        }
        function mohu(){
            var mohu = $("#mohu").val();

            location="student?m=list&mohu="+mohu;
        }
    </script>
  </head>

  <body>
    <table>
        <tr>
            <th colspan="21">
                <input type="text" id="mohu" value="${mohu}">
                <input type="button" value="搜索" onclick="mohu()">
            </th>
        </tr>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>sex</th>
            <th>age</th>
        </tr>
        <c:forEach var="s" items="${stulist}">
            <tr>
                <th>${s.id}</th>
                <th>${s.name}</th>
                <th>${s.sex}</th>
                <th>${s.age}</th>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="21">
                <button onclick="gopage(1)">首页</button>
                <button onclick="gopage(${pu.prevpage})">上页</button>
                <button onclick="gopage(${pu.nextpage})">下页</button>
                <button onclick="gopage(${pu.totalpage})">尾页</button>
                <input type="text" id="d1" value="${pu.cpage}" style="width:30px;">
                /${pu.totalpage}页
                <input type="button" value="跳" onclick="jumppage()">
            </th>
        </tr>
    </table>
  </body>
</html>
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;
    }




}
package com.www.dao;

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

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

public class StudentDao {

    public List<Student> stulist(PageUtils  pu, String mohu) {
        // TODO Auto-generated method stub
        List<Student> stulist = new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql:///cms01","root","root");

            String sql = "select * from student ";
            if(mohu != null){
                sql += " where name like '%"+mohu+"%' ";
            }
            sql += " limit "+pu.getStartIndex()+","+pu.getPageSize();

            PreparedStatement pst = con.prepareStatement(sql);


            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                stulist.add(new Student(rs.getInt("id"),rs.getString("name"),rs.getString("sex"),rs.getInt("age")));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stulist;
    }

    public Integer count(String mohu) {
        // TODO Auto-generated method stub
        Integer count = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql:///cms01","root","root");
            String sql = "select count(*) from student ";
            if(mohu != null){
                sql += " where name like '%"+mohu+"%' ";
            }
            PreparedStatement 
            pst = con.prepareStatement(sql);
            ResultSet 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.entity;

public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(int id, String name, String sex, int age) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    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 getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex
                + ", age=" + age + "]";
    }


}
package com.www.service;

import java.util.List;

import com.www.dao.StudentDao;
import com.www.entity.Student;
import com.www.util.PageUtils;

public class StudentService {
    StudentDao dao = new StudentDao();

    public List<Student> stulist(PageUtils pu, String mohu) {
        // TODO Auto-generated method stub
        return dao.stulist(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.Student;
import com.www.service.StudentService;
import com.www.util.PageUtils;

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

        String m = request.getParameter("m");

        if("list".equals(m)){
            list(request,response);
        }
    }
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String mohu = request.getParameter("mohu");
        System.out.println("1"+mohu);
        String cpage = request.getParameter("cpage");

        if(cpage==null){
            cpage="1";
        }

        Integer pageSize = 2;

        Integer count = service.count(mohu);

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

        //Integer totalpage = count/pageSize + (count%pageSize==0?0:1);

        List<Student> stulist = service.stulist(pu,mohu);


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

    }
}

人的觉醒是在对旧传统旧信仰旧价值旧风习的破坏、对抗和怀疑中取得的。
——李泽厚《美的历程》

猜你喜欢

转载自blog.csdn.net/helloworld_1996/article/details/80304224
今日推荐