问题
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.
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>
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 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.
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}"/>
...and myBeautifulBean.handleSubmit is never called.
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).
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.
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:
- to have chain calls of backing beans methods triggered by a p:commandButton action handler ;
- declaring every such action handler as a f:actionListener child of the commandButton.
I am looking for a more event oriented way.
回答1:
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>
.
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.
来源:https://stackoverflow.com/questions/13267933/jsf-triggering-methods-on-submit