简体   繁体   中英

Invoke action in f:viewAction while navigating using p:commandButton

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM