业务层接口和实现:
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); }}