javaWeb增删改查基本写法

本文详细介绍了在JavaWeb中进行创建、读取、更新和删除(CRUD)操作的基本步骤,涉及Controller、BaseController、BeanUtil、Service及DAO等关键组件的使用。
摘要由CSDN通过智能技术生成
  1. Controller
package com.controller;

import com.BaseController;
import com.entity.Catalog;
import com.entity.Course;
import com.github.pagehelper.PageInfo;
import com.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//目录
@WebServlet("/catalog")
public class CatalogController extends BaseController {
   
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
   
            this.list(request, response);
        } else if ("toAdd".equals(operation)) {
   
            this.toAdd(request, response);
        } else if ("save".equals(operation)) {
   
            this.save(request, response);
        } else if ("toEdit".equals(operation)) {
   
            this.toEdit(request, response);
        } else if ("edit".equals(operation)) {
   
            this.edit(request, response);
        } else if ("delete".equals(operation)) {
   
            this.delete(request, response);
        }
    }

    //列表查询
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
   
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
   
            size = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = catalogService.findAll(page, size);
        //将数据保存到指定的位置
        request.setAttribute("page", all);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/catalog/list.jsp").forward(request, response);
    }

    //跳转到添加页面
    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        //加载学科信息
        List<Course> all = courseService.findAll();
        request.setAttribute("courseList", all);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/catalog/add.jsp").forward(request, response);
    }

    //添加
    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        //将数据获取到,封装成一个对象
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class, "yyyy-MM-dd");
        //调用业务层接口
        catalogService.save(catalog);
        //跳转回到页面list
        response.sendRedirect(request.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值