- 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);
response.sendRedirect(request.