SubList分页-014-SubListServlet类

1.添加成员:SubListStudentServiceImpl
2.实现doPost:

  • 接受request参数
  • 组装查询条件
  • 使用SubListStudentServiceImpl获取查询结果
  • 存储结果
  • 转发

测试代码

package top.itcourse.page.controller;

import java.io.IOException;
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 top.itcourse.page.model.Constant;
import top.itcourse.page.model.InputParameter;
import top.itcourse.page.model.Pager;
import top.itcourse.page.model.Student;
import top.itcourse.page.service.StudentService;
import top.itcourse.page.service.SubListStudentServiceImpl;



/*
 * SubListServlet类:
 *  1.添加成员:SubListStudentServiceImpl
 *  2.实现doPost:
 *      接受request参数
 *      组装查询条件
 *      使用SubListStudentServiceImpl获取查询结果
 *      存储结果
 *      转发
 */
public class SubListServlet extends HttpServlet {
    
    private static final long serialVersionUID = 3993891107643551740L;
    
    
    private StudentService studentService = new SubListStudentServiceImpl();
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("访问成功~~~~~~");
        // 1.接受request参数
            // 学生姓名
        String name = request.getParameter("name");
            // 学生性别
        String genderStr = request.getParameter("gender");
        int gender = Constant.DEFAULT_GENDER;
        if( genderStr != null ) {
            gender = Integer.parseInt(genderStr);
        }
            // 存储参数
        InputParameter parameter = new InputParameter(name,gender);
        
            // 当前页
        String currentPageStr = request.getParameter("currentPage");
        int currentPage = Constant.DEFAULT_CURRENT_PAGE;
        
        if( currentPageStr != null ) {
            currentPage = Integer.parseInt(currentPageStr);
        }
            // 一页最多记录数目
        String pageSizeStr = request.getParameter("pageSize");
        int pageSize = Constant.DEFAULT_PAGE_SIZE;
        if( pageSizeStr != null ) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        
        // 2.组装查询条件
        Student searchModel = new Student();
        System.out.println("name: " + name);
        searchModel.setName(name);
        searchModel.setGender(gender);
        
        
        // 3.使用studentService获取查询结果
        Pager result = studentService.findStudent(searchModel, currentPage, pageSize);
        
        // 4.存储结果
        request.setAttribute("result", result); 
        request.setAttribute("parameter", parameter);
        
        System.out.println("Pager: " + result);
        
        // 5.转发
        System.out.println(request.getContextPath() + "/SubListView/index.jsp");
        request.getRequestDispatcher("/SubListView/index.jsp").forward(request, response);
//      return ;
    }
}

源码下载

关注下方的微信公众号,回复:java_div_page.code



SubList分页-014-SubListServlet类_第1张图片



欢迎加入交流群:451826376


更多信息:www.itcourse.top

完整教程PDF版本下载

你可能感兴趣的:(SubList分页-014-SubListServlet类)