博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jdbc,数据库案例:客户信息管理系统:业务层接口和实现,数据控制servlet
阅读量:6040 次
发布时间:2019-06-20

本文共 5691 字,大约阅读时间需要 18 分钟。

hot3.png

业务层接口和实现:

public interface BussinessService {

 /**
  * 查询所有客户信息
  *
  */
 @Deprecated
 List<Customer> findAll();
 /**
  * 增加客户信息
  *  c
  */
 void addCustomer(Customer c);
 /**
  * 根据主键删除客户信息
  *  customerId
  */
 void delCustomer(String customerId);
 /**
  * 分居主键查询客户信息
  *  customerId
  *  null
  */
 Customer findCustomerById(String customerId);
 /**
  * 更新客户信息
  *  c
  *  CustomerIdCannotBeEmpty
  */
 void updateCustomer(Customer c) throws CustomerIdCannotBeEmpty;
 /**
  * 输入页码,
  *  pageNum
  *  返回pange对象
  */
 Page findPage(String num);
}

public class BussinessServletImpl implements BussinessService {

 
 private CustomerDao dao=new CustomerDaoImpl();
 @Deprecated
 public List<Customer> findAll() {
  return dao.findAll();
 }

 public void addCustomer(Customer c) {

  c.setId(UUID.randomUUID().toString());
  dao.add(c);
 }

 public void delCustomer(String customerId) {

  dao.delete(customerId);
 }

 public Customer findCustomerById(String customerId) {

  return dao.findById(customerId);
 }

 public void updateCustomer(Customer c) throws CustomerIdCannotBeEmpty {

  if(c.getId()==null){
   throw new CustomerIdCannotBeEmpty("参数有误,请输入正确的客户信息");
  }
  dao.update(c);
 }

 public Page findPage(String num) {

  int pageNum=1;//默认值为1
  if(num!=null){
   pageNum=Integer.parseInt(num);
  }
  int totalRecords=dao.getTotalRecordsNum();
  Page page=new Page(pageNum, totalRecords);
  List<Customer> records=dao.findPageCustomers(page.getStartIndex(), page.getPageSize());
  page.setRecords(records);
  return page;
 }

}

数据控制servlet:

public class Controller extends HttpServlet {

 private BussinessService bs=new BussinessServletImpl();
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setCharacterEncoding("UTF-8");
  response.setContentType("text/html;charset=UTF-8");
  String op=request.getParameter("op");
  if("showAllCustomers".equals(op)){
   showAllCustomers(request,response);
  }else if("addCustomer".equals(op)){
   addCustomer(request,response);
  }else if("editCustomerUI".equals(op)){
   editCustomerUI(request,response);
  }else if("editCustomer".equals(op)){
   editCustomer(request,response);
  }else if("delOneCustomer".equals(op)){
   delOneCustomer(request,response);
  }else if("delMulti".equals(op)){
   delMulti(request,response);
  }
 }

 

 private void delMulti(HttpServletRequest request,

   HttpServletResponse response) throws IOException  {
  String ids[]=request.getParameterValues("ids");
  if(ids!=null&&ids.length>0){
   for(int i=0;i<ids.length;i++){
    bs.delCustomer(ids[i]);
   }
  }
  response.sendRedirect(request.getContextPath());
 }

 

 private void delOneCustomer(HttpServletRequest request,

   HttpServletResponse response) throws IOException {
  String customerId=request.getParameter("customerId");
  bs.delCustomer(customerId);
  response.sendRedirect(request.getContextPath());
 }
 
 
 private void editCustomer(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException{
    //将信息封装到formBean
    CustomerFormBean formBean=WebUtil.fillBean(request, CustomerFormBean.class);
    //信息不正确,数据回显
    if(!formBean.validate()){
     request.setAttribute("formBean", formBean);
     request.getRequestDispatcher("/addCustomer.jsp").forward(request, response);
    }
    //填充模型,注意类型转化
    ConvertUtils.register(new DateLocaleConverter(),Date.class);
    Customer c=new Customer();
    try {
     BeanUtils.copyProperties(c, formBean);
    } catch (Exception e) {
     throw new RuntimeException("填充模型时出现异常");
    }
    //单独处理爱好选项
    String preference[]=request.getParameterValues("preference");
    if(preference!=null&&preference.length>0){
     StringBuffer sb=new StringBuffer();
     for(int i=0;i<preference.length;i++){
      if(i>0){
       sb.append(",");
      }
      sb.append(preference[i]);
     }
     c.setPreference(sb.toString());
    }
    //保存数据
    try {
     bs.updateCustomer(c);
    } catch (CustomerIdCannotBeEmpty e) {
     e.printStackTrace();
    }
    //用重对象将页面跳转到默认主页,防止重复提交
    response.sendRedirect(request.getContextPath());
 }

 

 private void editCustomerUI(HttpServletRequest request,

   HttpServletResponse response) throws ServletException, IOException {
  String customerId=request.getParameter("customerId");
  Customer c=bs.findCustomerById(customerId);
  request.setAttribute("c", c);
  request.getRequestDispatcher("/editCustomer.jsp").forward(request, response);
 }
 private void addCustomer(HttpServletRequest request,
   HttpServletResponse response) throws IOException, ServletException  {
  //将信息封装到formBean
  CustomerFormBean formBean=WebUtil.fillBean(request, CustomerFormBean.class);
  //信息不正确,数据回显
  if(!formBean.validate()){
   request.setAttribute("formBean", formBean);
   request.getRequestDispatcher("/addCustomer.jsp").forward(request, response);
  }
  //填充模型,注意类型转化
  ConvertUtils.register(new DateLocaleConverter(),Date.class);
  Customer c=new Customer();
  try {
   BeanUtils.copyProperties(c, formBean);
  } catch (Exception e) {
   throw new RuntimeException("填充模型时出现异常");
  }
  //单独处理爱好选项
  String preference[]=request.getParameterValues("preferences");
  if(preference!=null&&preference.length>0){
   StringBuffer sb=new StringBuffer();
   for(int i=0;i<preference.length;i++){
    if(i>0){
     sb.append(",");
    }
    sb.append(preference[i]);
   }
   c.setPreference(sb.toString());
  }
  //保存数据
  bs.addCustomer(c);
  //用重对象将页面跳转到默认主页,防止重复提交
  response.sendRedirect(request.getContextPath());
 }

 private void showAllCustomers(HttpServletRequest request,

   HttpServletResponse response) throws ServletException, IOException{
  List<Customer> cs=bs.findAll();
  request.setAttribute("cs", cs);
  String num=request.getParameter("num");
  Page page=bs.findPage(num);
  page.setServletUrl("/servlet/Controller?op=showAllCustomers");
  request.setAttribute("page", page);
  request.getRequestDispatcher("/listCustomers.jsp").forward(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)

   throws ServletException, IOException {
  doGet(request, response);
 }

}

转载于:https://my.oschina.net/u/1589656/blog/285185

你可能感兴趣的文章
ASP生成静态页面的方法
查看>>
HDU 1325 Is It A Tree? 判断是否为一棵树
查看>>
Shell命令-文件压缩解压缩之gzip、zip
查看>>
个人总结
查看>>
uva 673 Parentheses Balance
查看>>
Bzoj 2252: [2010Beijing wc]矩阵距离 广搜
查看>>
css 禁止选中文本
查看>>
bzoj2165
查看>>
算术运算表达式正则及分析
查看>>
Oracle 12c 多租户 手工创建 pdb 与 手工删除 pdb
查看>>
shell初涉
查看>>
[浪子学编程][MS Enterprise Library]ObjectBuilder之创建策略祥解(二)
查看>>
windows添加和删除服务
查看>>
关于云栖,有点无语的几个地方,管理能不能管?
查看>>
Windows线程的同步与互斥
查看>>
C#进阶系列——MEF实现设计上的“松耦合”(四):构造函数注入
查看>>
AngularJs ng-change事件/指令(转)
查看>>
linux系统下安装两个或多个tomcat
查看>>
ProtoBuffer 简单例子
查看>>
iOS多线程开发系列之(一)NSThread
查看>>