2

I would like to execute a method once the link is being clicked and the new page is loaded. I have tried the <action/> tag as but this didn't help either :

<url-mapping id="myApp/seller/addProduct">
    <pattern value="/myApp/seller/addProduct" />
    <view-id value="/pages/seller/add_product.xhtml" />
    <action onPostback="false">#{myBean.barMethod}</action>
    <action>#{myBean.myMethod}</action>
</url-mapping>

@Bean
public class MyBean{
   //source code omitted
   public void myMehtod(){
      //source code omitted
   }
}

I tried debugging the method execution. The call is never made after the page is loaded. How can I solve this problem please?

UPDATE : After debugging this problem for hours, I came to the result that the first action myBean.barMethod() is always executed while the second myBean.myMethod() is never fired which I don't understand pretty much why??

Community
  • 1
  • 1
ecdhe
  • 421
  • 4
  • 17

2 Answers2

0

You cannot directly pass arguments to action methods using pretty-config.xml, you would need to bind a value to a bean property, like so:

https://www.ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/Configuration.html#config.pathparams

<url-mapping id="viewItem"> <pattern value="/store/item/#{ iid : bean.itemId }/" /> <view-id value="/faces/shop/item.jsf" /> <action>#{bean.loadItem}</action> </url-mapping>

Additionally, subsequent action methods beyond the first will not be executed if navigation has been invoked by a prior action-method:

https://www.ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/Configuration.html#config.actions.navigation

That said, if you are not binding a value from the URL, why not just call a different method in the bean, or acquire the value to be passed another way?

Lincoln
  • 3,151
  • 17
  • 22
-1

In 1.2x and beyond JSF version you can use @PostConstruct on the bean method to be invoked when the page is rendered first time.

In your case bind the page add_product.xhtml to a bean and decorate the method to be invoked with @PostConstruct

Saif Kamaal
  • 192
  • 1
  • 9
  • 1
    This does not solve my problem. The method should be executed not only when the page is rendered the first time, but rather each time the user is being forwarded to it. – ecdhe Mar 12 '18 at 10:19
  • If the bean is in RequestScope then definitely it will get fired each time – Saif Kamaal Mar 12 '18 at 10:37
  • I have to create a new bean just for this purpose since MyBean is session scoped. It is more a hack than a solution for this problem I would say. My final code won't be clean though. – ecdhe Mar 12 '18 at 10:57