Web.xml中的FacesServlet和URL映射 [英] FacesServlet and URL Mapping in Web.xml

查看:128
本文介绍了Web.xml中的FacesServlet和URL映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我们在中声明 FacesServlet 及其URL映射的web.xml 。根据我的理解,



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



JSF的新学习者之一问我这些问题,这两个应用程序只使用一次。这是真的吗?还有没有其他方法可以不包括在web.xml中?



我应该回答什么?



已更新



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




  • 在JSF中,我们从未见过更改地址栏中的URL。在这种情况下,它是如何使用相同的URL处理新请求的?

  • 在faces-config.xml中,我们给出的导航案例如下:



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




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



解决方案


FacesServlet仅在服务器启动时加载一次。


正确。


仅当从外部上下文访问第一次JSP应用程序时才使用URL映射。


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


还有其他方法不包含在web.xml中吗?


如果你正在使用支持Servlet 3.0的servletcontainer,你也可以通过 @WebServlet 注释。但是,JSF 2.0旨在向后兼容Servlet 2.5,因此它不附带该注释,您需要在 web.xml 中明确声明它。 / p>

另请参阅:








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


在JSF中,我们从未见过更改地址栏中的URL。在这种情况下,它是如何使用相同的URL处理新请求的?


只有在封面下方才会发生这种情况< a href =http://download.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html#forward%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29 =nofollow noreferrer > 发生RequestDispatcher#forward() 。在转发中,servlet容器基本上重用 相同的 HTTP请求/响应视图(JSP / XHTML页面)。它不强制/指示webbrowser发送全新的请求。另一方面, HttpServletResponse #sendRedirect() 将指示客户端(webbrowser)触发 GET请求,从而网址会改变。您可以通过将< redirect /> 添加到< navigation-case> 来强制执行此操作。 。请注意,由于这会导致 new 请求,因此初始请求的所有请求范围bean都将丢失。


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


FacesServlet 知道自己的 url-pattern


HI,

We are declaring the FacesServlet and its URL mapping in the Web.xml. From my understanding,

FacesServlet loaded only once at the server startup. URL mapping is used only when first time JSP application accessed from the external context.

One of the new learner for JSF has asked me the questions, these two things are used only once by the application. Is it true? Also is there any other way by not including in the web.xml?

What I should answer?

Updated

For example, I am accessing the application using the URL http://localhost:8080/webapp/index.jsf. When we are accessing this URL, FacesServlet invoked and view is rendered. The following is my question:

  • 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?
  • In faces-config.xml we are giving the navigation cases as follows:

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

    • 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 loaded only once at the server startup.

Correct.

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

Incorrect. It's been tested on every incoming HttpServletRequest. How else should the container know which servlet to invoke?

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

If you're using a servletcontainer which supports Servlet 3.0, you can also do this by @WebServlet annotation. JSF 2.0, however, is designed to be backwards compatible with Servlet 2.5, so it doesn't ship with that annotation and you need to explicitly declare it in web.xml.

See also:


Update as per the new series of questions (which should each belong in its own question, but ala)

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?

This happens only if under the covers a forward by RequestDispatcher#forward() takes place. In a forward, the servletcontainer basically reuses the same HTTP request/response for a view (JSP/XHTML page). It does not force/instruct the webbrowser to send a brand new request. On the other hand, the HttpServletResponse#sendRedirect() will instruct the client (the webbrowser) to fire a new GET request and thus the URL will change. You can force this in the JSF sice by adding <redirect/> to the <navigation-case>. Note that since this causes a new request, all request scoped beans of the initial request will be lost.

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?

The FacesServlet knows its own url-pattern.

这篇关于Web.xml中的FacesServlet和URL映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆