文章目录
-
Servlet代码演示
-
package cn.javabs.store.servlet; import cn.javabs.store.entity.Category; import cn.javabs.store.service.CategoryService; import cn.javabs.store.service.impl.CategoryServiceImpl; import com.alibaba.druid.sql.visitor.functions.If; 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 javax.swing.text.EditorKit; import java.io.IOException; import java.util.List; /** * 类目的Servlet */ @WebServlet("/categoryServlet") public class CategoryServlet extends HttpServlet { //实例化CategoryService CategoryService categoryService = new CategoryServiceImpl(); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 0.在本方法内部进行分别调用如下分类操作的方法 // ring methodName = "addCategory"; // 去接收index.jsp页面中的methodName参数 String methodName = request.getParameter("methodName"); // 1.如果字符串"addCategory"等于methodName的值(内容) if ("addCategory".equals(methodName)) { addCategory(request, response); // 2.如果字符串"delCategory"等于methodName的值(内容) } else if ("delCategory".equals(methodName)) { delCategory(request, response); // 3.如果字符串"editCategory"等于methodName的值(内容) } else if ("editCategory".equals(methodName)) { editCategory(request, response); // 4.如果字符串"findAllCategories"等于methodName的值(内容) } else if ("findAllCategories".equals(methodName)) { findAllCategories(request, response); } else if ("editCategoryView".equals(methodName)) { editCategoryView(request, response); } // 5.如果methodName的值(内容)没有匹配的方法,则执行下方输入语句 else { System.out.println("哥们,你要的方法不存在,检查是否单词有误!"); } } //------------------------------------------------------------------------------------------------------------------------------------------------------- /** * 查询全部分类 */ public void findAllCategories(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Category> categoryList = categoryService.findAllCategries(); System.out.println(categoryList); request.setAttribute("list", categoryList); //list是一个标记、记号 request.getRequestDispatcher("/categoryList.jsp").forward(request, response); } /** * 根据id指示查询 */ public void findCategoryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Category category = categoryService.findCategoryById(2); System.out.println("因为index.jsp访问了页面中查询全部的方法,内容:" + category); } /** * 添加分类 */ public void addCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 接受前台的参数 String cname = request.getParameter("cname"); Category category = new Category(); category.setCid(null); //因为数据库会自动生成 //category.setCname("内存卡"); //目前的数据是 写死的 不是前台传递过来的 category.setCname(cname); //目前的数据是 写活的 是前台传递过来的cname int rowNumber = categoryService.addCategory(category); if (rowNumber > 0) { System.out.println("插入成功!"); } else { System.out.println("插入失败!"); } } /** * 修改分类 之 数据回显 */
-