2

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 Dec 13 '13 at 19:32
  • @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](http://stackoverflow.com/a/6377957/1199132). Appart from that, doing any business logic in `getter`/`setter` methods is just wrong. – Xtreme Biker Dec 14 '13 at 14:59
  • 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:http://stackoverflow.com/q/2786834/1530938, http://stackoverflow.com/q/4281261/1530938, http://stackoverflow.com/q/2090033/1530938 – kolossus Dec 16 '13 at 00:40
  • 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 Dec 17 '13 at 09:59
  • @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 Dec 17 '13 at 10:03
  • Not quite sure I understand the requirement for your action, but does [InvokeActionEventListener](http://showcase.omnifaces.org/eventlisteners/InvokeActionEventListener) help you? – mabi Dec 17 '13 at 10:08
  • @mabi: That is a very good suggestion. I am tying omnifaces 1.6.3, but it seems to have some dependency on weld/cdi ? I am getting exceptions while jboss server start using myfaces (**An error occured while initializing MyFaces: Class or g.jboss.as.weld.webtier.jsf.WeldApplicationFactory is no javax.faces.application .ApplicationFactory**). Any idea how to fix it or not sure if I can use omnifaces 1.5 version which has no cdi. – user3073999 Dec 17 '13 at 11:03
  • You should be able to deploy omnifaces in a non-CDI context. Maybe ask another question with your setup details and the error you're seeing? – mabi Dec 17 '13 at 11:11
  • Thank you, mabi. Please set InvokeActionEventListener answer to this question. – user3073999 Dec 17 '13 at 11:21

1 Answers1

0

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>
mabi
  • 5,141
  • 1
  • 34
  • 70