JSF在托管bean中获取当前操作 [英] JSF get current action in managed bean
问题描述
当用户单击任何 commandButton
时,将在托管bean中调用相应的操作。
是否可以从 @PostConstruct
方法或从事件监听器方法获取此操作名称?
When user clicks any commandButton
, then corresponding action is called in managed bean.
Is it possible to get this action name from @PostConstruct
method or from event listener method ?
推荐答案
按钮的名称=值对本身可用作常规方式的HTTP请求参数。想象一下,生成的命令按钮的HTML表示看起来像这样
The button's name=value pair is by itself available as HTTP request parameter the usual way. Imagine that the generated HTML representation of the command button look like this
<input type="submit" name="formId:buttonId" value="Submit" ... />
然后它作为请求参数出现,名称为 formId:buttonId
具有非空值。 JSF在应用请求值阶段中使用此信息来确定是否按下了按钮。这发生在与按钮组件关联的渲染器的 decode()
方法中,大致如下:
Then it's present as a request parameter with name formId:buttonId
with a non-null value. JSF uses exactly this information during the Apply Request Values phase to determine if the button was pressed or not. This happens during the decode()
method of the renderer associated with the button component, roughly as follows:
if (externalContext.getRequestParameterMap().containsKey(component.getClientId(context))) {
component.queueEvent(new ActionEvent(component));
}
或者当它涉及ajax请求时,按钮的名称可以作为 javax.faces.Source
请求参数的值。
Or when it concerns an ajax request, then the button's name is instead available as value of the javax.faces.Source
request parameter.
if (component.getClientId(context).equals(externalContext.getRequestParameterMap().get("javax.faces.Source"))) {
component.queueEvent(new ActionEvent(component));
}
无论哪种方式, ActionEvent
最终存储为 UIViewRoot
的私有字段,该字段位于公共API的 no 中。所以,除非你抓住反射和实现特定的黑客,这是故事的结尾。
Either way, the ActionEvent
is ultimately stored as a private field of UIViewRoot
which is in no way available by a public API. So, unless you grab reflection and implementation specific hacks, it's end of story here.
要确定按下按钮,你最好的选择是手动检查请求参数图同样作为JSF本身的方式。
To determine the button pressed your best bet is to manually check the request parameter map the same way as JSF itself does.
根据具体的功能要求,从问题中不是很清楚,另一种方法可能是使用 actionListener
或< f:actionListener>
所有 UICommand
感兴趣的组件,或使用< action-listener>
在 faces-config.xml
中注册一个全局的。在调用真实的操作
之前,将调用。
Depending on the concrete functional requirement, which is not exactly clear from the question, an alternative may be to use actionListener
or <f:actionListener>
on all UICommand
components of interest, or to use <action-listener>
in faces-config.xml
to register a global one. This will be invoked right before the real action
is invoked.
这篇关于JSF在托管bean中获取当前操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!