1

home.xhtml

<p:button outcome="product" value="Login Simple Button"/>
<p:commandButton value="Login" action="#{homeController.validate}" update="logindialog"/>

product.xhtml

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

Simple p:button navigation and successfully invokes readProductGroup(); but p:commandButton doesn’t even though homeController.validate() method returns ‘product’ and successfully navigates to product.xhtml.

With p:button, I don’t get the flexibility to invoke server methods, use update attribute etc. which I need to.

What I need is, to be able to use the attributes like update, action/actionListener etc in home.xhtml page button and at the same time when navigated to product.xhtml, should be able to invoke the function productController.readProductGroup under f:metadata at the time of pageload.

Please suggest.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user5281896
  • 123
  • 2
  • 11
  • Didn't get any error, just that the method didn't get called which I wanted to invoke. Based on suggestion below used product?faces-redirect=true and made it work. – user5281896 Mar 04 '16 at 16:12

1 Answers1

1

The <f:viewAction> is designed to run on GET requests, not on POST requests. To invoke actions on POST requests, you should be using <h:commandXxx>.

If you really want to run <f:viewAction> on POST requests too for some reason, then just set its onPostback attribute to true.

<f:viewAction ... onPostback="true" />

On the other hand, it's also very good possible you don't understand much about idempotence, navigation and GET versus POST. Navigating by POST is considered bad practice. You could apply the POST-redirect-GET pattern by sending a redirect in action method by appending the ?faces-redirect=true query string to the outcome.

public void validate() {
    // ...

    return "product?faces-redirect=true";
}

See also How to navigate in JSF? How to make URL reflect current page (and not previous one)


Unrelated to the concrete problem, the action method name leaves me the impression you are also not aware of properly using JSF validators. In that case, carefully read JSF 2.0 validation in actionListener or action method.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • A lot of great material in the provided links. product?faces-redirect=true solved the problem. Thanks a lot for sharing all these details. – user5281896 Mar 04 '16 at 15:11