• 用户关系管理最终版


    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>客户列表</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>
    <h3 align="center">客户列表</h3>
    <table border="1" width="70%" align="center">
    <tr>
    <th>客户姓名</th>
    <th>性别</th>
    <th>生日</th>
    <th>手机</th>
    <th>邮箱</th>
    <th>描述</th>
    <th>操作</th>
    </tr>
    <!--遍历的是pageBean的beanList这个对象所对应的数据-->
    <c:forEach  items="${pb.beanList}"  var="cstm">
    <tr>
    <td>${cstm.cname} </td>
    <td>${cstm.gender}</td>
    <td>${cstm.birthday}</td>
    <td>${cstm.cellphone}</td>
    <td>${cstm.email}</td>
    <td>${cstm.description}</td>
    <td>
    <a href="<c:url value='/customerServlet?method=preEdit&cid=${cstm.cid}'/>" >编辑</a>
    <a href="<c:url value='/customerServlet?method=delete&cid=${cstm.cid}'/>">删除</a>
    </td>
    </tr>
    </c:forEach>
    </table>
    <br/>
    <!--给出分页相关的链接-->
    <center>
    第${pb.pc}/共${pb.tp}页


    <a href="${pb.url }&pc=1" >首页</a>


    <c:if test="${pb.pc > 1}">
    <a href="${pb.url }&pc=${pb.pc-1}" >上一页</a>
    </c:if>
    <%-- 只是在计算begin和end的值  ! --%>
    <c:choose>
    <%-- 如果不足十页的话,就把所有的页数显示出来! --%>
    <c:when test="${pb.tp<=10 }">
    <c:set var="begin" value="1"/>
    <c:set var="end" value="${pb.tp }"/>
    </c:when>
    <c:otherwise>
    <%-- 当页面大于十的时候 --%>
    <c:set var="begin" value="${pb.pc-5}"/>
    <c:set var="end" value="${pb.pc+4}"/>
    <%-- 头溢出 --%>
    <c:if test="${begin<1 }">
    <c:set var="begin" value="1"/>
    <c:set var="end" value="10"/>
    </c:if>
    <%-- 尾溢出 --%>
    <c:if test="${end>pb.tp }">
    <c:set var="begin" value="${pb.tp-9 }"/>
    <c:set var="end" value="${pb.tp }"/>
    </c:if>
    </c:otherwise>
    </c:choose>


    <c:forEach var="i" begin="${begin }" end="${ end}">
    <%--当就是当前页面的时候就不进行超链接了 --%>


    <c:choose>
    <c:when test="${i eq pb.pc}">
    [${i }]
    </c:when>




    <c:otherwise>
    <a href="${pb.url }&pc=${i }">[${i}]</a>
    </c:otherwise>
    </c:choose>
    </c:forEach>


    <c:if test="${pb.pc <pb.tp}">
    <a href="${pb.url }&pc=${pb.pc+1}" >下一页</a>
    </c:if>
    <a href="${pb.url }&pc=${pb.tp }" >尾页</a>
    </center>

      </body>




    package cn.edu.nsu.cstm.servlet;


    import java.io.IOException;


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


    import cn.edu.nsu.cstm.domain.customer;
    import cn.edu.nsu.cstm.domain.pageBean;
    import cn.edu.nsu.cstm.service.customerService;
    import cn.itcast.commons.CommonUtils;
    import cn.itcast.servlet.BaseServlet;


    public class customerServlet extends BaseServlet {
    customerService customerService=new customerService();


    public String add(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);//得到封装好的用户
    c.setCid(CommonUtils.uuid());//将用户的id用uuid随机生成的数表似
    customerService.add(c);//通过service添加用户
    request.setAttribute("msg", c.getCname()+"用户注册成功");//保持信息到msg中
    return "f:/msg.jsp";//f代表转发,若是r代表的就是重定向了
    }
    // public String findall(HttpServletRequest request, HttpServletResponse response)
    // throws ServletException, IOException {
    // //调用service得到用户的对象
    // //保存到request域中
    // //转发到list.jsp中
    // request.setAttribute("cstmList", customerService.findall());
    // return "f:/list.jsp";
    // }

    public String findall(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //获取页面传递的pc
    //给定ps的值
    //使用pc和ps调用service方法,得到pagebean,保存到request域中
    //转发到list。jsp
    int pc=getPc(request);
    int ps=10;//给定ps的值,每页十行
    pageBean<customer> pb=customerService.findall(pc,ps);
    pb.setUrl(getUrl(request));

    request.setAttribute("pb", pb);//将对象保存在request域中
    return "f:/list.jsp";//转发到list.jsp中
    }

    private int getPc(HttpServletRequest request){

    String value=request.getParameter("pc");
    // System.out.println(value);
    if(value==null||value.trim().isEmpty()){
    return 1;
    }
    return Integer.parseInt(value);
    }

    public String preEdit(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //获取cid
    //使用cid得到service的customer对象
    //转发显示到表单中
    String cid =request.getParameter("cid");
    customer cstm=customerService.load(cid);
    request.setAttribute("cstm", cstm);

    return "f:/edit.jsp";
    }
    public String edit(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //封装表单对象到customer中
    //调用service方法完成修改
    //保存信息到request域中
    //转发到msg.jsp中
    customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);
    customerService.edit(c);
    request.setAttribute("msg", c.getCname()+"用户修改成功");

    return "f:/msg.jsp";
    }
    public String delete(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //封装表单对象到customer中
    //调用service方法完成修改
    //保存信息到request域中
    //转发到msg.jsp中
    customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);
    String idString=c.getCname();
    customerService.delete(c);
    request.setAttribute("msg", idString+"用户删除成功");

    return "f:/msg.jsp";
    }
    // public String query(HttpServletRequest request, HttpServletResponse response)
    // throws ServletException, IOException {
    // //封装表单数据到customer对象之中,它有四个属性(cname, gender,cellphone,email)
    // //调用service方法
    // //保存在request域中
    // //转发到list.jsp中
    // customer criteria=CommonUtils.toBean(request.getParameterMap(), customer.class);
    // List<customer> cstmList=customerService.query(criteria);
    // request.setAttribute("cstmList", cstmList);
    // return "f:/list.jsp";

    public String query(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //把条件服装到customer对象之中
    //得到pc
    //给定ps
    //使用pc和ps,以及条件对象,调用service方法得到pageBean
    //把pagebean保存到request域中
    //转发到list.jsp中
    customer criteria=CommonUtils.toBean(request.getParameterMap(), customer.class);
    int pc=getPc(request);
    int ps=10;//给定ps的值,每页十行
    pageBean<customer> pb=customerService.query(criteria,pc,ps);

    //得到url保存到pb中
    pb.setUrl(getUrl(request));
    request.setAttribute("pb", pb);
    return "f:/list.jsp";

    }
    private String getUrl(HttpServletRequest request){
    String contextPath=request.getContextPath();
    String servletPath=request.getServletPath();
    String querysString=request.getQueryString();
    if(querysString.contains("&pc=")){
    int index=querysString.lastIndexOf("&pc=");
    querysString=querysString.substring(0,index);
    }

    //我的tomcat下面配置了utf-8这个属性所有没用进行解码和编码这个过程

    return contextPath+servletPath+"?"+querysString;
    }







    }


    package cn.edu.nsu.cstm.dao;


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


    import org.apache.commons.dbutils.QueryRunner;


    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.ScalarHandler;


    import com.sun.org.apache.bcel.internal.generic.NEW;


    import cn.edu.nsu.cstm.domain.customer;
    import cn.edu.nsu.cstm.domain.pageBean;
    import cn.itcast.jdbc.TxQueryRunner;


    public class customerDao {
    private QueryRunner qr = new TxQueryRunner();// 依赖此对象对数据库的使用进行操作


    // 添加客户


    public void add(customer c) {
    try {
    String sql = "INSERT INTO t_customer VALUES(?,?,?,?,?,?,?)";// 插入用户
    Object[] params = { c.getCid(), c.getCname(), c.getGender(),
    c.getBirthday(), c.getCellphone(), c.getEmail(),
    c.getDescription() };// 为每一个用户进行赋值操作
    qr.update(sql, params);// 处理开始
    } catch (Exception e) {
    throw new RuntimeException();// 抛出异常
    }
    }


    public pageBean<customer> findall(int pc, int ps) {
    // 他的pageBean对象pb
    // 设置pb的pc和ps
    // 得到beanList,设置给pb
    // 返回pb
    pageBean<customer> pb = null;
    try {
    pb = new pageBean<customer>();// 创建一个pb对象
    pb.setPc(pc);// 向pb对象中设置当前的页数
    pb.setPs(ps);// 向pb对象设置每页的记录数
    String sql = "select count(*) from t_customer";// 遍历所有的数据得到总数
    Number number = (Number) qr.query(sql, new ScalarHandler());// 得到总数,返回值为number类型
    pb.setTr(number.intValue());// 将number类型转化为int类型存入pb对象之中去
    sql = "select *from t_customer order by cname limit ?,?";// 创建sql语句
    List<customer> beanList = qr.query(sql,
    new BeanListHandler<customer>(customer.class), (pc - 1)
    * ps, ps);// 通过传入的当前页数和每行的记录数,返回当前所对应的每一个对象
    pb.setBeanList(beanList);
    } catch (Exception e) {
    throw new RuntimeException(e);
    }
    return pb;
    }


    public customer load(String cid) {
    // 加载客户
    try {
    String sql = "select *from t_customer where cid=?";
    return qr
    .query(sql, new BeanHandler<customer>(customer.class), cid);
    } catch (Exception e) {
    throw new RuntimeException();
    }
    }


    // 编辑客户
    public void edit(customer c) {
    try {
    String sql = "update t_customer set cname=?,gender=?,birthday=?,cellphone=?,email=?,description=? where cid=?";
    Object[] parmas = { c.getCname(), c.getGender(), c.getBirthday(),
    c.getCellphone(), c.getEmail(), c.getDescription(),
    c.getCid() };


    qr.update(sql, parmas);
    } catch (SQLException e) {
    throw new RuntimeException(e);
    }
    }


    public void delete(customer c) {
    String sql = "delete from t_customer where cid=?";
    try {
    qr.update(sql, c.getCid());
    } catch (SQLException e) {
    throw new RuntimeException(e);
    }


    }


    // public List<customer> quer(customer criteria) {
    // // 给出一个sql的前半句话
    // StringBuilder sql = new StringBuilder(
    // "select *from  t_customer where 1=1");
    // // 判断条件在追加子句
    // List<Object> param = new ArrayList<Object>();
    // // 创建一个Arraylist用来转载参数
    // String cname = criteria.getCname();
    // if (cname != null && !cname.trim().isEmpty()) {
    // sql.append(" and cname=?");
    // param.add(cname);
    // }
    // String gender = criteria.getGender();
    // if (gender != null && !gender.trim().isEmpty()) {
    // sql.append(" and gender=?");
    // param.add(gender);
    // }
    // String cellphone = criteria.getCellphone();
    // if (cellphone != null && !cellphone.trim().isEmpty()) {
    // sql.append(" and cellphone=?");
    // param.add(cellphone);
    // }
    // String email = criteria.getEmail();
    // if (email != null && !email.trim().isEmpty()) {
    // sql.append(" and email=?");
    // param.add(email);
    // }
    // // 给出参数
    // try {
    // return qr.query(sql.toString(), new BeanListHandler<customer>(
    // customer.class), param.toArray());
    // } catch (SQLException e) {
    // throw new RuntimeException(e);
    // }
    // }


    public pageBean<customer> quer(customer criteria, int pc, int ps) {
    pageBean<customer> pb=null;
    try {
    // 创建pageBean对象
    // 设置已有的属性,pc和ps
    // 得到tr
    // 得到beanList对象


    // 创建pb,设置已有的属性
    pb = new pageBean<customer>();
    pb.setPc(pc);
    pb.setPs(ps);


    // 得到tr
    // 先得到sql的前半句话
    // 在向sql中追加后半句话
    StringBuffer cntsql = new StringBuffer(
    "select count(*)from  t_customer");
    StringBuffer wheresq = new StringBuffer(" where 1=1");
    List<Object> param = new ArrayList<Object>();
    // 创建一个Arraylist用来转载参数
    String cname = criteria.getCname();
    if (cname != null && !cname.trim().isEmpty()) {
    wheresq.append(" and cname=?");
    param.add(cname);
    }
    String gender = criteria.getGender();
    if (gender != null && !gender.trim().isEmpty()) {
    wheresq.append(" and gender=?");
    param.add(gender);
    }
    String cellphone = criteria.getCellphone();
    if (cellphone != null && !cellphone.trim().isEmpty()) {
    wheresq.append(" and cellphone=?");
    param.add(cellphone);
    }
    String email = criteria.getEmail();
    if (email != null && !email.trim().isEmpty()) {
    wheresq.append(" and email=?");
    param.add(email);
    }
    // 执行select count(*)from----


    Number num = (Number) qr.query(cntsql.append(wheresq).toString(),
    new ScalarHandler(), param.toArray());
    pb.setTr(num.intValue());


    // 得到beanList对象
    StringBuffer sql = new StringBuffer("select *from  t_customer");
    StringBuffer limitsql = new StringBuffer(" limit ?,? ");
    // parma中需要给出limit后面两个问好的值
    param.add((pc - 1) * ps);
    param.add(ps);
    // 执行代码
    List<customer> beanList = qr.query(
    sql.append(wheresq).append(limitsql).toString(),
    new BeanListHandler<customer>(customer.class),
    param.toArray());
    pb.setBeanList(beanList);
    } catch (SQLException e) {
    throw new RuntimeException(e);
    }
    return pb;
    }


    }



  • 相关阅读:
    Linux系统下mysql修改密码遇到的问题
    Description: Field ud in com.yjj.service.impl.UserServiceImpl required a bean of type 'com.yjj.dao.UserDao' that could not be found. Action: Consider defining a bean of type 'com.yjj.dao.UserDao'
    地址的三级联动(jQuery+servlet)
    人生苦短,我用Python(目录)
    数据库增量同步
    并发编程
    Python Web 之 Flask
    jQuery 选择器
    常见性能优化方法的一些总结
    常见的浏览器兼容性问题总结
  • 原文地址:https://www.cnblogs.com/csnd/p/16675743.html
Copyright © 2020-2023  润新知