【问题标题】:JSF - Triggering methods on submitJSF - 提交时触发方法
【发布时间】:2012-11-07 10:38:10
【问题描述】:

我在我的新 JSF 2.1 应用程序中使用复合、标记或自定义组件。

我仍然经常忽略继续使用这项技术的正确(或至少是推荐的)方法。

我希望我的组件在用户单击“保存”按钮时执行一些操作。实际上,保存按钮是 Primefaces 命令按钮。

所以,我正在使用按钮代码,例如:

<p:commandButton id="example" type="submit" value="Confirmer les modifications" process="targetComponent" update="<compoents to update list>">
    <f:param name="save" value="true>
</p:commandButton>

对于“保存”按钮和我的组件,我使用 preRenderComponent 来触发一个 handleSubmit 支持 bean 方法:

<f:event type="preRenderComponent" listener="#{myBeautifulBean.handleSubmit}"/>

handleSumit 看起来像:

public void handleSubmit() {
    FacesContext context = FacesContext.getCurrentInstance();
    String saveMandats = JSFUtils.getRequestParameter("save");
    if(context.isPostback() && !context.isValidationFailed() && (saveMandats != null) && !saveMandats.isEmpty())
        confirmeModifsSelection();
}

有效

阅读 stackoverflow 并尝试遵循 @BalusC 的建议,我正在使用 omnifaces (1.2) 并尝试使用 postInvokeAction 事件,原因在 OmniFaces InvokeActionEventListener showcase 中解释。

所以,我将我的事件标签更改为:

<f:event type="postInvokeAction" listener="#{myBeautifulBean.handleSubmit}"/>

...myBeautifulBean.handleSubmit 永远不会被调用。

当然,我将omnifaces 作为依赖项,而其他组件(验证器等)也可以正常工作。 InvokeActionListener 已正确初始化(或者在我看来是这样)。

postInvokeAction 是否应该在特定时间注册?我注意到在omnifaces 示例中,{pre|post}InvokeAction 事件总是在 f:metadata 标记中声明为子项。

我发现了很多在 f:metadata 之外声明 preRenderView 事件的示例,例如:http://www.mkyong.com/jsf2/jsf-2-prerenderviewevent-example/

顺便说一句,如果我这样做是完全错误的,我很高兴向你学习。但我想避免:

  • 通过 p:commandButton 操作处理程序触发支持 bean 方法的链式调用;
  • 将每个此类操作处理程序声明为 commandButton 的 f:actionListener 子级。

我正在寻找一种更加面向事件的方式。

【问题讨论】:

    标签: jsf omnifaces


    【解决方案1】:

    InvokeActionEventListener showcase 中所述和所示,此事件仅适用于UIViewRootUIFormUIInputUICommand。所以如果你想要一个通用的钩子(在视图上),那么你需要把它放在UIViewRoot 上。这需要通过将其放在&lt;f:metadata&gt; 中来完成。

    preRenderComponentpreRenderView 不需要特定的父组件,您实际上可以将它放置在视图中的任何位置,它总是注册到最近的父 UI 组件。这对于preInvokeAction/postInvokeAction 没有多大意义,因为不是每种 类型的组件都参与调用操作。例如,将其放置在 &lt;h:panelGroup&gt; 中就不是很自文档了。

    【讨论】:

    • 不,它们必须位于顶级视图中。但据我了解,您想在复合组件中使用postInvokeAction
    • 复合、标签或自定义(我用各种)。
    • 好的,我找到了一个非常简单的解决方法:我将 {pre|post}InvokeAction 事件添加到 h:inputHidden。
    猜你喜欢
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    相关资源
    最近更新 更多