gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:12 24 4
gpt4 key购买 nike

你好,

我们在 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() 时才会发生这种情况发生。在转发中,servletcontainer 基本上重用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com