FacesContext和ExternalContext有什么区别 [英] What is the difference between FacesContext and ExternalContext

查看:172
本文介绍了FacesContext和ExternalContext有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FacesContextExternalContext有什么区别?什么时候可以使用其中一个?一个有什么,另一个有什么?

What is the difference between FacesContext and ExternalContext? When can I use one or other? What has the one and what has the other?

以下示例摘自《 JavaServer Faces第三版》一书:

The following sample is from the book JavaServer Faces 3rd edition:

 <h:commandButton ... actionListener="#{rushmore.handleMouseClick}" />

后备豆:

public void handleMouseClick(ActionEvent e) {
    FacesContext context = FacesContext.getCurrentInstance();
    String clientId = e.getComponent().getClientId(context);
    Map<String, String> requestParams = context.getExternalContext().getRequestParameterMap();
    // ...
}

为什么ExternalContext中的请求参数?什么是clientId?启动应用程序时,它是由JSF生成的吗?

Why is request parameter in ExternalContext? What is clientId? Is it generated by JSF when the application is started?

推荐答案

仔细查看其javadoc,以了解它们提供了哪些方法以及这些方法究竟能做什么.

Carefully look in their javadocs to see what methods they offer and what exactly those methods all do.

  • FacesContext javadoc
  • ExternalContext javadoc

如果您仔细查看javadoc中列出的那些方法,您会发现FacesContext通常提供对JSF特定工件的访问,这些工件与底层" Servlet或Portlet API毫无关系, JSF被设计为在其之上运行.例如.创建转换器,验证器,组件,EL表达式等,并获取有关视图根,支持的语言环境等的信息,并添加阶段侦听器,系统事件侦听器等.所有针对JSF API的内容.

If you look closer at those methods listed in the javadoc, you'll notice that the FacesContext generally offers access to JSF-specific artifacts which are further in no way related to the "underlying" Servlet or Portlet API for which JSF is been designed to run on top of. E.g. creating converters, validators, components, EL expressions, etcetera and obtaining information about view root, supported locales, etcetera and adding phase listeners, system event listeners, etcetera. Everything which is specific to JSF API.

而且,ExternalContext通常提供对Servlet或Portlet特定工件的访问,而JSF当前正在使用这些工件.例如,在Servlet容器上运行时, HTTP Servlet请求 HTTP Servlet响应 HTTP会话 javadoc .是的,还单击该链接,您将看到它明确提到了servlet请求:

And, ExternalContext generally offers access to Servlet or Portlet-specific artifacts which JSF is currently using "under the covers". E.g., when running on a Servlet container, the HTTP servlet request, HTTP servlet response, HTTP session and Servlet context and inherently also all of their artifacts. Click those links, you'll see that they in turn offer methods which are also been delegated by the ExternalContext, such as getRequestParameterMap(). See also the javadoc. Yes, also click that link, you'll see that it explicitly mentions the servlet request:

Servlet:这必须是通过javax.servlet.ServletRequest方法getParameter()getParameterNames()可用的参数集.

Servlet: This must be the set of parameters available via the javax.servlet.ServletRequest methods getParameter() and getParameterNames().

两者上下文都不能提供什么.因此,绝对没有理由偏爱一个或另一个.只需使用适合您需要执行的工作的

There's nothing which can be offered by the both contexts. So there would be absolutely no reason to prefer the one or the other. Just use the right one for the job you need to perform.

关于客户机ID,它确实是由JSF生成的,但绝对不是在服务器启动时生成的.它是基于每个视图为每个单个JSF组件生成的.对于像<h:inputText>这样的输入组件,它生成HTML <input>元素,它也像这样成为name属性

As to the client ID, it's indeed generated by JSF, but definitely not on server's startup. It's just generated for every single JSF component on a per-view basis. In case of input components like <h:inputText>, which generates a HTML <input> element, it also becomes the name attribute like so

<input type="text" id="formId:inputId" name="formId:inputId" ... />

formId:inputId正是JSF客户端ID.它成为请求参数名称.命令按钮的HTML表示形式还有一个name,它最终作为请求参数名称,而按钮的值作为参数值.

The formId:inputId is exactly the JSF client ID. It becomes the request parameter name. The HTML representation of the command button has also a name which ends up as request parameter name with the button's value as parameter value.

这篇关于FacesContext和ExternalContext有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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