JSF如何处理动作侦听器? [英] How does JSF process action listener?

查看:106
本文介绍了JSF如何处理动作侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇JSF如何知道我单击了按钮并执行了一些操作,甚至可以通过参数调用操作侦听器.我可以想象服务器注意到状态和EL并调用方法.

I'm curious how JSF know that I click the button, with some action and it is even possible to call an action listener with a parameter. I can imagine the server notice the state and EL and call the methods.

示例1:

<form>
   <p:commandButton actionListener="{bean.do_something(bean.info)}" />
</form>

示例2:

<form>
     <h:datatable values=... var="myvar">
        <h:column>
           <p:commandButton actionListener="{bean.do_something(myvar.info)}" />
        </h:column>
     </h:datatable>
</form>

推荐答案

在应用请求值阶段,

During the apply request values phase, the decode() method of all UIComponent instances in the component tree is executed. This is where the necessary HTTP request parameters are checked and collected. In case of UIInput components (<h:inputText> and friends), the submitted value is been obtained. In case of UICommand components (<h:commandButton> and friends), the ActionEvent is been queued.

如果使用<p:commandButton>,则所有魔术都发生在

In case of <p:commandButton> all the magic happens in CommandButtonRenderer#decode() whom a relevant part of the source code is extracted below (line numbers are from PrimeFaces 3.5):

34  public void decode(FacesContext context, UIComponent component) {
35      CommandButton button = (CommandButton) component;
36      if(button.isDisabled()) {
37          return;
38      }
39         
40      String param = component.getClientId(context);
41      if(context.getExternalContext().getRequestParameterMap().containsKey(param)) {
42          component.queueEvent(new ActionEvent(component));
43      }
44  }

如果您熟悉基本HTML ,您应该已经知道name=value每个输入元素对,并且仅将封闭形式的按下按钮作为请求参数发送到服务器. PrimeFaces命令按钮基本上会生成以下HTML,

If you're familiar with basic HTML, you should already know that the name=value pair of every input element and only the pressed button of the enclosing form is been sent as request parameter to the server. The PrimeFaces command button generates basically the following HTML,

<button type="submit" name="formId:buttonId" ... />

其中,formId:buttonId是从UIComponent#getClientId()打印的.正是这个值被用作HTTP请求参数名称(HTTP请求参数值是按钮的标签,但是在此不再相关).如果您熟悉在JSF之上运行的基本Servlet ,那么您还应该已经知道请求参数是HttpServletRequest#getParameter()可用,包括name=value对按钮.这允许区分按下的按钮.

where formId:buttonId is printed from UIComponent#getClientId(). It's exactly this value which is been used as HTTP request parameter name (the HTTP request parameter value is the button's label, but that's not further relevant here). If you're familiar with basic Servlets, which JSF runs on top of, then you should also already know that request parameters are available by HttpServletRequest#getParameter(), including the name=value pair of the buttons. This allows distinguishing the pressed button.

如您在上述decode()方法中所看到的,正是为了检查HTTP请求参数映射是否包含参数名称,也恰好使用了UIComponent#getClientId()值.如果是这样,则ActionEvent将排队,最终在调用应用程序阶段被调用.

As you see in the above decode() method, exactly this UIComponent#getClientId() value is also been used in order to check if the HTTP request parameter map contains the parameter name. If so, then an ActionEvent will be queued which ultimately get invoked during invoke application phase.

关于EL的论点,实际上不是火箭科学.整个EL表达式仅在调用应用程序阶段执行.并不是这样,它是在生成表单的HTML输出期间执行的,然后以某种方式作为请求参数传递的.不,它只是在实际调用应用程序阶段执行的.

As to the EL arguments, it's actually no rocket science. The whole EL expression is just executed during invoke application phase. It's not so that it's been executed during generating the HTML output of the form and then in some way passed as request parameter. No, it's just been executed during the actual invoke application phase.

这篇关于JSF如何处理动作侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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