JSF-提交时的触发方法 [英] JSF - Triggering methods on submit

查看:115
本文介绍了JSF-提交时的触发方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于JSF 2.1的新应用程序中使用复合,标签或自定义组件.

I am using composite, tag or custom components in my new JSF 2.1 powered application.

我仍然经常忽略正确(或至少推荐)进行这项技术的方法.

I still often ignores the right (or at least recommended) way to proceed with this technology.

当用户单击保存"按钮时,我希望我的组件执行某些操作.实际上,保存按钮是Primefaces命令按钮.

I would like my components to perform some action when the user is clicking on the "save" button. Practically, the save button is a Primefaces commandButton.

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

So, I am using button code such as:

<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方法:

for the "save" button and in my components, I use the preRenderComponent to trigger an handleSubmit backing bean method :

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

handleSumit看起来像:

handleSumit looks like:

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

有效 .

This works.

阅读stackoverflow并尝试遵循@BalusC建议,我正在使用omnifaces(1.2)并尝试使用postInvokeAction事件,原因在

Reading stackoverflow and trying to follow @BalusC adviced, I am using omnifaces (1.2) and trying to use the postInvokeAction event, for reasons explained in OmniFaces InvokeActionEventListener showcase.

因此,我将事件代码更改为:

So, I am changing my event tag to:

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

...从不调用myBeautifulBean.handleSubmit.

...and myBeautifulBean.handleSubmit is never called.

我当然拥有无所不能的依赖关系,而其他组件(验证器等)也可以正常工作. InvokeActionListener已正确初始化(或在我看来如此).

I am, of course, having omnifaces as a dependency and other components (validators, etc.) just works. The InvokeActionListener is properly initialized (or seems so to me).

是否应在特定时间注册postInvokeAction?我注意到在无所不包的示例中,{pre | post} InvokeAction事件始终在f:metadata标签中声明为子级.

Should the postInvokeAction be registered at a specific time ? I noticed that in omnifaces examples, {pre|post}InvokeAction events are always declared as children in f:metadata tags.

我发现了很多在f:metadata之外声明preRenderView事件的示例,例如:

I found lots of example where preRenderView events are declared outside of f:metadata, like: http://www.mkyong.com/jsf2/jsf-2-prerenderviewevent-example/

顺便说一句,如果我以这种方式犯错,我很高兴向您学习.但我想避免:

By the way, if I am plain wrong proceeding this way, I will be glad to learn from your wisdom. But I want to avoid:

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

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

I am looking for a more event oriented way.

推荐答案

展示,此事件仅在UIViewRootUIFormUIInputUICommand上有效.因此,如果要使用通用钩子(在视图上),则需要将其放在UIViewRoot上.这需要通过将其放置在<f:metadata>中来完成.

As mentioned and shown in the InvokeActionEventListener showcase, this event works on UIViewRoot, UIForm, UIInput and UICommand only. So if you want a generic hook (on the view), then you'd need to put it on UIViewRoot. This needs to be done by placing it in <f:metadata>.

preRenderComponentpreRenderView不需要特定的父组件,您几乎可以将其放置在视图中的任何位置,并且始终将其注册到最接近的父UI组件.对于preInvokeAction/postInvokeAction来说,这没有多大意义,因为不是每种类型的组件都参与调用动作.例如,将其放在<h:panelGroup>中就不会很自我记录.

The preRenderComponent and preRenderView doesn't require a specific parent component, you can practically place it everywhere in the view, it's always be registered to the closest parent UI component. This does not make much sense for the preInvokeAction/postInvokeAction as not every kind of component participates in invoke action. Placing it in for example a <h:panelGroup> wouldn't have been very self-documentatory.

这篇关于JSF-提交时的触发方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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