【发布时间】: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 子级。
我正在寻找一种更加面向事件的方式。
【问题讨论】: