• 2020-12-18日报博客-周五


    1. 学到的东西:

    案例:图书管理系统的实现

    package servlet;
    
    import domain.Book;
    import domain.User;
    import org.apache.commons.beanutils.BeanUtils;
    import service.BookService;
    import service.UserService;
    import service.impl.BookServiceImpl;
    import service.impl.UserServiceImpl;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.util.Map;
    
    @WebServlet("/addBookServlet")
    public class AddBookServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.设置编码
            request.setCharacterEncoding("utf-8");
            //2.获取参数
            Map<String, String[]> map = request.getParameterMap();
            //3.封装对象
            Book book = new Book();
            try {
                BeanUtils.populate(book,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
    
            //4.调用Service保存
            BookService service = new BookServiceImpl();
            service.addBook(book);
    
            //5.跳转到userListServlet
            response.sendRedirect(request.getContextPath()+"/bookListServlet");
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
    
    
    package servlet;
    
    import domain.User;
    import org.apache.commons.beanutils.BeanUtils;
    import service.UserService;
    import service.impl.UserServiceImpl;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.util.Map;
    
    @WebServlet("/addUserServlet")
    public class AddUserServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.设置编码
            request.setCharacterEncoding("utf-8");
            //2.获取参数
            Map<String, String[]> map = request.getParameterMap();
            //3.封装对象
            User user = new User();
            try {
                BeanUtils.populate(user,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
    
            //4.调用Service保存
            UserService service = new UserServiceImpl();
            service.addUser(user);
    
            //5.跳转到userListServlet
            response.sendRedirect(request.getContextPath()+"/userListServlet");
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
    
    
    package servlet;
    
    import domain.User;
    import org.apache.commons.beanutils.BeanUtils;
    import service.UserService;
    import service.impl.UserServiceImpl;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.util.Map;
    
    @WebServlet("/addUserServlet")
    public class AddUserServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.设置编码
            request.setCharacterEncoding("utf-8");
            //2.获取参数
            Map<String, String[]> map = request.getParameterMap();
            //3.封装对象
            User user = new User();
            try {
                BeanUtils.populate(user,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
    
            //4.调用Service保存
            UserService service = new UserServiceImpl();
            service.addUser(user);
    
            //5.跳转到userListServlet
            response.sendRedirect(request.getContextPath()+"/userListServlet");
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
    
    
    package servlet;
    
    import domain.PageBean;
    import domain.User;
    import service.UserService;
    import service.impl.UserServiceImpl;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Map;
    
    @WebServlet("/findUserByPageServlet")
    public class FindUserByPageServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
    
            //1.获取参数
            String currentPage = request.getParameter("currentPage");//当前页码
            String rows = request.getParameter("rows");//每页显示条数
    
            if(currentPage == null || "".equals(currentPage)){
    
                currentPage = "1";
            }
    
    
            if(rows == null || "".equals(rows)){
                rows = "5";
            }
            
            //获取条件查询参数
            Map<String, String[]> condition = request.getParameterMap();
    
    
            //2.调用service查询
            UserService service = new UserServiceImpl();
            PageBean<User> pb = service.findUserByPage(currentPage,rows,condition);
    
            System.out.println(pb);
    
            //3.将PageBean存入request
            request.setAttribute("pb",pb);
            request.setAttribute("condition",condition);//将查询条件存入request
            //4.转发到list.jsp
            request.getRequestDispatcher("/list.jsp").forward(request,response);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
    
    

    2. 遇到的问题:

    3. 明日计划:继续完成图书管理系统

  • 相关阅读:
    May LeetCoding Challenge3 之 HashMap的使用
    May LeetCoding Challenge2 之 HashSet的使用
    May LeetCoding Challenge1 之 标准二分查找法
    30-Day Leetcoding Challenge Day21
    30-Day Leetcoding Challenge Day19
    30-Day Leetcoding Challenge Day18
    ALV 单元格编辑回车响应
    ALV模版1
    web dynpro 开发配置
    convert fm
  • 原文地址:https://www.cnblogs.com/gongyunlong-blogs/p/14161614.html
Copyright © 2020-2023  润新知