1、@RequestMapping
package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface RequestMapping {String value();
}
2、@PostMapping
package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface PostMapping {String value();
}
3、@GetMapping
package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface GetMapping {String value();
}
4、FruitController
- 之前是FruitServlet现在不让他成为Servlet,改名成FruitController,不在继承HttpServlet
package com.csdn.fruit.servlet;
import com.csdn.fruit.dto.PageInfo;
import com.csdn.fruit.dto.PageQueryParam;
import com.csdn.fruit.dto.Result;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.fruit.service.FruitService;
import com.csdn.fruit.service.impl.FruitServiceImpl;
import com.csdn.fruit.util.RequestUtil;
import com.csdn.fruit.util.ResponseUtil;
import com.csdn.mymvc.annotation.GetMapping;
import com.csdn.mymvc.annotation.PostMapping;
import com.csdn.mymvc.annotation.RequestMapping;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@RequestMapping("/fruit")
public class FruitController {private FruitService fruitService = new FruitServiceImpl();@GetMapping("/index")protected void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Integer pageNo = 1;String pageNoStr = req.getParameter("pageNo");if (pageNoStr != null && !"".equals(pageNoStr)) {pageNo = Integer.parseInt(pageNoStr);}String keyword = "";String keywordStr = req.getParameter("keyword");if (keywordStr != null) {keyword = keywordStr;}PageQueryParam pageQueryParam = new PageQueryParam(pageNo, 5, keyword);PageInfo<Fruit> pageInfo = fruitService.getFruitPageInfo(pageQueryParam);Result result = Result.OK(pageInfo);ResponseUtil.print(resp, result);}@PostMapping("/add")protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class);fruitService.addFruit(fruit);ResponseUtil.print(resp, Result.OK());}@GetMapping("/del")protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Integer fid = Integer.parseInt(req.getParameter("fid"));fruitService.delFruit(fid);ResponseUtil.print(resp, Result.OK());}@GetMapping("/edit")protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Integer fid = Integer.parseInt(req.getParameter("fid"));Fruit fruit = fruitService.getFruitById(fid);ResponseUtil.print(resp, Result.OK(fruit));}@GetMapping("/getFname")public void getFname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String fname = req.getParameter("fname");Fruit fruit = fruitService.getFruitByFname(fname);ResponseUtil.print(resp, fruit == null ? Result.OK() : Result.Fail());}@PostMapping("/update")protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class);fruitService.updateFruit(fruit);ResponseUtil.print(resp, Result.OK());}
}
5、DispatcherServlet
package com.csdn.mymvc.core;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
@WebServlet("/*")
public class DispatcherServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String[] staticResourceSuffixes = {".html", ".jsp", ".jpg", ".png", ".gif", ".css", ".js", ".ico"};String uri = req.getRequestURI();if (Arrays.stream(staticResourceSuffixes).anyMatch(uri::endsWith)) {RequestDispatcher defaultDispatcher = req.getServletContext().getNamedDispatcher("default");defaultDispatcher.forward(req,resp);}}
}