javax.faces.webapp.FacesServlet.java 没有以下方法
doGet(), if the servlet supports HTTP GET requests
doPost(), for HTTP POST requests
doPut(), for HTTP PUT requests
doDelete(), for HTTP DELETE requests
它只有 service() 方法。
doGet()/doPost()/doPut()/doDelete()方法仅特定于javax.servlet.http.HttpServlet.java类
javax.servlet.http.HttpServlet.java 已扩展为通过 HTTP 协议(protocol)为 j2ee Web 应用程序进行通信。 而 javax.faces.webapp.FacesServlet.java 也用于通过 HTTP 进行 j2ee Web 应用程序通信。
我的问题是,当 javax.faces.webapp.FacesServlet.java 没有 HTTP 特定方法时,JSF 应用程序如何通过 HTTP 进行通信?
最佳答案
那些HttpServlet#doXxx()
方法只是一个抽象。不使用它们并不意味着 servlet 不使用 HTTP(此外,如果这是真的,则首先不会调用 servlet)。
如果 servlet 仅重写 Servlet#service()
方法,则仅意味着该 servlet 对所有 HTTP 方法具有全局捕获点。如果有必要,它仍然可以在某些时候通过 HttpServletRequest#getMethod()
确定正在使用的实际 HTTP 方法。 。这种方法使程序员无需在所有 doXxx()
方法上复制粘贴相同的代码。
如果您阅读 FacesServlet
source code ,您会发现下面的 block ,其中确定了 HTTP 方法(行号与 JSF 2.2 API 匹配):
671 private boolean isHttpMethodValid(HttpServletRequest request) {
672 boolean result = false;
673 if (allowAllMethods) {
674 result = true;
675 } else {
676 String requestMethodString = request.getMethod();
677 HttpMethod requestMethod = null;
678 boolean isKnownHttpMethod;
679 try {
680 requestMethod = HttpMethod.valueOf(requestMethodString);
681 isKnownHttpMethod = true;
682 } catch (IllegalArgumentException e) {
683 isKnownHttpMethod = false;
684 }
685 if (isKnownHttpMethod) {
686 result = allowedKnownHttpMethods.contains(requestMethod);
687 } else {
688 result = allowedUnknownHttpMethods.contains(requestMethodString);
689 }
690
691 }
692
693 return result;
694 }
基本上,实际的 HTTP 方法仅与 FacesServlet 相关,以确定它是否是受支持的方法。如果是,则 servlet 将继续以相同的方式处理 HTTP 请求,而不管使用哪种方法。
关于java - javax.faces.webapp.FacesServlet 如何通过 HTTP 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32004170/