PART 01
package com.atguigu.fruit.serlvets;
import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.springmvc.ViewBaseServlet;
import com.atguigu.myssm.util.StringUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
/**
* @author crystal
* @create 2022-11-19 17:17
*/
@WebServlet("/fruit.do")
public class FruitServlet2 extends ViewBaseServlet {
FruitDAO fruitDAO = new FruitDAOImpl();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String operate = request.getParameter("operate");
if(operate == null){
operate = "index";
}
switch(operate){
case "index":
index(request,response);
break;
case "edit":
edit(request,response);
break;
case "del":
del(request,response);
break;
case "add":
add(request,response);
break;
case "update":
update(request,response);
break;
default:
throw new RuntimeException("输入的opetate的值有误!");
}
}
private void index(HttpServletRequest request,HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
Integer pageNo = 1;
String oper = request.getParameter("oper");
String keyword = null;
if(StringUtil.isNotEmpty(oper) && "search".equals(oper)){
pageNo = 1;
keyword = request.getParameter("keyword");
if(StringUtil.isEmpty(keyword)){
keyword = "";
}
session.setAttribute("keyword",keyword);
}else{
String pageNoStr = request.getParameter("pageNo");
if(StringUtil.isNotEmpty(pageNoStr)){
pageNo = Integer.parseInt(pageNoStr);
}
Object keywordObj = session.getAttribute("keyword");
if(keywordObj != null){
keyword = (String) keywordObj;
}else{
keyword = "";
}
}
session.setAttribute("pageNo",pageNo);
List<Fruit> fruitList = fruitDAO.getAll(keyword, pageNo);
session.setAttribute("fruitList",fruitList);
Integer fruitCount = fruitDAO.getFruitCount(keyword);
int pageCount = (fruitCount + 5 - 1) / 5;
session.setAttribute("pageCount",pageCount);
super.processTemplate("index",request,response);
}
private void edit(HttpServletRequest request,HttpServletResponse response) throws IOException {
String fidStr = request.getParameter("fid");
if(StringUtil.isNotEmpty(fidStr)){
int fid = Integer.parseInt(fidStr);
Fruit fruit = fruitDAO.getFruitById(fid);
request.setAttribute("fruit",fruit);
super.processTemplate("edit",request,response);
}
}
private void update(HttpServletRequest request,HttpServletResponse response) throws IOException {
String fidStr = request.getParameter("fid");
Integer fid = Integer.parseInt(fidStr);
String fname = request.getParameter("fname");
String priceStr = request.getParameter("price");
int price = Integer.parseInt(priceStr);
String fcountStr = request.getParameter("fcount");
Integer fcount = Integer.parseInt(fcountStr);
String remark = request.getParameter("remark");
fruitDAO.updateFruit(new Fruit(fid,fname, price ,fcount ,remark ));
response.sendRedirect("fruit.do");
}
private void del(HttpServletRequest request,HttpServletResponse response) {
String fidStr = request.getParameter("fid");
if (StringUtil.isNotEmpty(fidStr)) {
int fid = Integer.parseInt(fidStr);
fruitDAO.delFruit(fid);
}
}
private void add(HttpServletRequest request,HttpServletResponse response) throws IOException {
String fname = request.getParameter("fname");
Integer price = Integer.parseInt(request.getParameter("price")) ;
Integer fcount = Integer.parseInt(request.getParameter("fcount"));
String remark = request.getParameter("remark");
Fruit fruit = new Fruit(0,fname , price , fcount , remark ) ;
fruitDAO.insertFruit(fruit);
response.sendRedirect("fruit.do");
}
}
PART 02:使用反射技术
package com.atguigu.fruit.controllers;
import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.springmvc.ViewBaseServlet;
import com.atguigu.myssm.util.StringUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
/**
* @author crystal
* @create 2022-11-16 15:15
*/
public class FruitController e