本文主要是介绍项目实战:设置静态资源放行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、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);}} }
2、web.xml
The default servlet for all web applications, that serves static resources. It processes all requests that are not mapped to other servlets with servlet mappings (defined either here or in your own web.xml file). 默认servlet是所有Web应用程序的默认servlet,用于提供静态资源。 它处理所有未映射到其他servlet的请求, 这些servlet映射(在此处或您自己的web.xml文件中定义)。
<servlet><servlet-name>default</servlet-name><servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class><init-param><param-name>debug</param-name><param-value>0</param-value></init-param><init-param><param-name>listings</param-name><param-value>false</param-value></init-param><load-on-startup>1</load-on-startup></servlet>
2、FruitServlet
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 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; @WebServlet("/fruit/*") public class FruitServlet extends HttpServlet {private FruitService fruitService = new FruitServiceImpl();@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// /fruit/indexString uri = req.getRequestURI();uri = uri.substring(1);String[] split = uri.split("/");String methodName = split[1];switch (methodName) {case "index" ->index(req,resp);case "add"-> add(req, resp);case "del"->del(req,resp);case "edit"->edit(req,resp);case "getFname"-> getFname(req, resp);case "update" -> update(req, resp);}}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);}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());}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());}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));}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());}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());} }
这篇关于项目实战:设置静态资源放行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!