java - Web.xml 中的 FacesServlet 和 URL 映射

标签 java jsf servlets jakarta-ee

嗨,

我们在 Web.xml 中声明 FacesServlet 及其 URL 映射。据我了解,

FacesServlet 在服务器启动时仅加载一次。 URL 映射仅在第一次从外部上下文访问 JSP 应用程序时使用。

一位 JSF 的新学习者问了我这个问题,这两个东西仅被应用程序使用一次。这是真的吗?还有其他方法不包含在 web.xml 中吗?

我应该回答什么?

已更新

例如,我使用 URL http://localhost:8080/webapp/index.jsf 访问应用程序。当我们访问此 URL 时,将调用 FacesServlet 并呈现 View 。以下是我的问题:

  • 在 JSF 中,我们从未见过更改地址栏中的 URL。那么,它如何处理具有相同 URL 的新请求?
  • 在 faces-config.xml 中,我们给出的导航案例如下:

    to-view-id>failure.jsp/to-view-id>

    • 为什么我们不需要将 View 名称指定为 failure.jsf?我们只是在faces-config.xml 中提供*.jsp。内部是如何处理的?

最佳答案

FacesServlet loaded only once at the server startup.

正确。

URL mapping is used only when first time JSP application accessed from the external context.

不正确。它已在每个传入 HttpServletRequest 上进行了测试。容器还应该如何知道要调用哪个 servlet?

Also is there any other way by not including in the web.xml?

如果您使用的是支持 Servlet 3.0 的 servletcontainer,您也可以通过 @WebServlet 来执行此操作注解。然而,JSF 2.0 被设计为向后兼容 Servlet 2.5,因此它不附带该注释,您需要在 web.xml 中显式声明它。 .

另请参阅:


更新根据新的一系列问题(每个问题都应该属于自己的问题,但是啦)

In JSF, we never seen changing the URL in the address bar. In that case, how it is handling the new request with the same URL?

仅当 RequestDispatcher#forward() 在幕后转发时才会发生这种情况发生。在转发中,servlet 容器基本上重用 View (JSP/XHTML 页面)的相同 HTTP 请求/响应。它不会强制/指示网络浏览器发送全新的请求。另一方面, HttpServletResponse#sendRedirect() 将指示客户端(网络浏览器)触发 GET 请求,因此 URL 将发生变化。您可以通过添加 <redirect/> 在 JSF sice 中强制执行此操作。到<navigation-case> 。请注意,由于这会导致请求,因此初始请求的所有请求范围的 bean 将丢失。

Why we need not give the view name as failure.jsf? We are just giving the *.jsp in the faces-config.xml. How it is handled internally?

FacesServlet知道自己的url-pattern .

关于java - Web.xml 中的 FacesServlet 和 URL 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4818366/

相关文章:

Java定时器与GUI同步

java - 使用 servlet 填充 cq5 对话框中的下拉列表

java - 将 ResultSet 从 servlet 传递到 JSP

java - 使用camera2拍照时如何获得曝光时间?

java - Android Mediaplayer - 基于seekbar进度更新textview中音频播放时间

jsf - p :fileUpload doesn't work with pe:blockUI in same page

jsf - 与 p :dialog? 相比,PrimeFaces 对话框框架是否会破坏 View 范围

JSF 2.0 : How to get the URL that is entered in the browser's address bar

Java Servlet 过滤器 : I have to add headers before passing to the chain, 文档另有说明

来自字符串的 Java 星期几