2
votes

I am aware that custom action can be done using following standard way (for jsf 2.1)

<f:metadata>
    <f:viewParam name="query" value="#{bean.query}" />
    <f:event type="preRenderView" listener="#(bean.action}" />
</f:metadata>

Given my request is always GET type, and with url as, /context/page1.xhmtl?query=jsf

can i invoke my custom action in setter method setQuery() instead of listener method setAction() ? Means I will be invoking my action code (such as setting corresponding view bean) in apply model values phase instead of pre render-response phase.

Please share if any downside to this approach, as this is the only way, it is working well in my application setup, and not working properly with prerender listener method.

Update: The reason for my app not working properly with prerender listener method, could be that prerender method is being called after the 'Start' of render-response, not before it. I was expecting to call my prerender listener method, before the start of render-response, ideally at the end of or right after 'invoke application' phase. Does it make sense to expect that way ? as I am thinking it is too late to be called after render response phase start.

Thanks very much.

1
I'd discourage this. The setQuery() method will be called on postback, too. And this is just the technical side. Nobody (should) expect your setter to do anything else than, well, setting query. To give you more well founded answer - what do you need to do you can't do with the current setup? - mabi
@mabi I'm agree with you, however not in the first part of your answer. f:viewParam tags are only evaluated only for GET requests. Have a look at this. Appart from that, doing any business logic in getter/setter methods is just wrong. - Xtreme Biker
The downside to your current approach? Massive performance degradation. That setter will be called multiple times per view render. There are a number of questions that detail the mechanics of this already:stackoverflow.com/q/2786834/1530938, stackoverflow.com/q/4281261/1530938, stackoverflow.com/q/2090033/1530938 - kolossus
Thank you all for your responses. I have added an update to my query. mabi: I agree with XBiker that setQuery will not be called for postback requests, and viewparams will only be evaluated for get requests. kolossus: All the articles that you have mentioned seems to be only for getters and not really for setters. - user3073999
@user3073999 have you tried that? I can't currently, but my reading of UIViewParam is that value will be validated/converted/set on postback like every other UIInput. - mabi

1 Answers

0
votes

As per the comments, invoking an Action from the view but as part of the Invoke Application phase can be done with the help of omnifaces' InvokeActionEventListener:

<f:metadata>
  <f:event type="postInvokeAction" listener="#{bean.action}" />
</f:metadata>

When deploying with JSF-2.2 you could also use:

<f:metadata>
  <f:viewAction action="#{bean.action}" />
</f:metadata>