5.Web层Servlet层
Servlet创建方式:项目名右键new--Servlet,使用@WebServlet注解,省去配置web.xml的时间
1 package com.wang.web; 2 3 import java.io.IOException; 4 import java.math.BigDecimal; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.wang.pojo.HomeCost; 14 import com.wang.service.HomeCostService; 15 import com.wang.utils.WebUtils; 16 17 /** 18 * 访问地址url:localhost:8080/homeCost/manager/homeCostServlet 19 * Servlet implementation class HomeCostServlet 20 */ 21 @WebServlet("/manager/homeCostServlet") 22 public class HomeCostServlet extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24 private HomeCostService homeCostService = new HomeCostService(); 25 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 doPost(request, response); 28 } 29 30 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 String action = request.getParameter("action"); 32 if("add".equals(action)) { 33 add(request, response); 34 }else if("delete".equals(action)) { 35 delete(request, response); 36 }else if("update".equals(action)) { 37 update(request, response); 38 }else if("list".equals(action)) { 39 list(request, response); 40 }else if("getHomeCostById".equals(action)) { 41 getHomeCostById(request, response); 42 }else if("query".equals(action)) { 43 query(request, response); 44 } 45 } 46 47 //添加消费记录 48 protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 49 //获取网页提交的参数 50 String name = request.getParameter("name"); 51 BigDecimal money = WebUtils.bigdecimal(request.getParameter("money"), new BigDecimal(0.00)); 52 //封装成类对象 53 HomeCost homeCost = new HomeCost(name,money); 54 //执行添加操作,返回1,添加成功,反之失败 55 if(homeCostService.add(homeCost) == 1) { 56 //页面重定向 57 response.sendRedirect(request.getContextPath()+"/manager/homeCostServlet?action=list"); 58 }else { 59 //请求转发 60 request.setAttribute("msg", "添加失败,联系管理员"); 61 request.setAttribute("homeCost", homeCost); 62 request.getRequestDispatcher("/cost_edit.jsp").forward(request, response); 63 } 64 } 65 //删除消费记录 66 protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 67 //获取id 68 int id = WebUtils.parseInt(request.getParameter("id"), 0); 69 //执行删除操作,返回1,删除成功,反之失败 70 if (homeCostService.delete(id) == 1) { 71 //页面重定向 72 response.sendRedirect(request.getContextPath()+"/manager/homeCostServlet?action=list"); 73 }else { 74 //请求转发 75 request.setAttribute("msg", "删除失败,联系管理员"); 76 request.getRequestDispatcher("/manager/homeCostServlet?action=list").forward(request, response); 77 } 78 79 } 80 //修改消费记录 81 protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 82 //获取参数 83 int id = WebUtils.parseInt(request.getParameter("id"), 0); 84 String name = request.getParameter("name"); 85 BigDecimal money = WebUtils.bigdecimal(request.getParameter("money"), new BigDecimal(0.00)); 86 String date = request.getParameter("date"); 87 //封装 88 HomeCost homeCost = new HomeCost(id,name,money,date); 89 //执行删除操作,返回1,修改成功,反之失败 90 if(homeCostService.update(homeCost) == 1) { 91 response.sendRedirect(request.getContextPath()+"/manager/homeCostServlet?action=list"); 92 }else { 93 request.setAttribute("msg", "修改失败,联系管理员"); 94 request.setAttribute("homeCost", homeCost); 95 request.getRequestDispatcher("/cost_edit.jsp").forward(request, response); 96 } 97 } 98 //查询全部消费记录 99 protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 100 List<HomeCost> homeCost = homeCostService.list(); 101 request.setAttribute("homeCost", homeCost); 102 request.getRequestDispatcher("/manager.jsp").forward(request, response); 103 104 } 105 //通过id查询该条消费记录 106 private void getHomeCostById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 107 int id = WebUtils.parseInt(request.getParameter("id"), 0); 108 HomeCost homeCost = homeCostService.getHomeCostById(id); 109 request.setAttribute("homeCost", homeCost); 110 request.getRequestDispatcher("/cost_edit.jsp").forward(request, response); 111 } 112 //通过关键词查询 113 protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 114 String keyword=request.getParameter("keyword"); 115 List<HomeCost> homeCost = homeCostService.query(keyword); 116 request.setAttribute("homeCost", homeCost); 117 request.getRequestDispatcher("/query.jsp").forward(request, response); 118 119 } 120 121 122 } HomeCostServlet.java
6.工具类
1 package com.wang.utils; 2 3 import java.math.BigDecimal; 4 5 public class WebUtils { 6 7 //该类为工具类,封装常用的函数,catch类型转换异常,保障程序健壮性 8 9 10 /** 11 * String转BigDecimal发生异常返回默认值 12 * @param str 字符串 13 * @param defaultValue 默认值 14 * @return BigDecimal值 15 */ 16 public static BigDecimal bigdecimal(String str,BigDecimal defaultValue) { 17 try { 18 return new BigDecimal(str); 19 } catch (Exception e) { 20 //e.printStackTrace(); 21 } 22 return defaultValue; 23 } 24 25 /** 26 * 字符串转int 27 * @param string 被转换的字符串 28 * @param defaultValue 默认值 29 * @return int 30 */ 31 public static int parseInt(String string,int defaultValue){ 32 try { 33 return Integer.parseInt(string); 34 } catch (NumberFormatException e) { 35 //e.printStackTrace(); 36 } 37 return defaultValue; 38 } 39 40 41 } WebUtils.java
标签:homeCost,BigDecimal,request,家庭,记账,action,import,response From: https://www.cnblogs.com/azure011328/p/17991487