40

I'am using Primefaces to make my app a little bit more beautiful. I noticed that p:commandButton and h:commandButton don't work the same way.

The p:commandButton calls the method and nothing else hapens. The h:commandButton calls the method and returns a navigation.

I have got trouble with redirecting after pressing the login button when using p:commandButton. How to deal with that?


I do have an update parameter:

<p:commandButton value="Login" action="#{login.login}" type="submit" update="msgs" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sven
  • 6,288
  • 24
  • 74
  • 116

6 Answers6

54

<p:commandButton> needs one of the following to work correctly:

  • the update attribute to list id's of components to re-render (for AJAX requests)
  • the attribute ajax="false" to make a normal, non-ajax submit.

If you have neither, the button does per default an AJAX request with no UI feedback.

Additionally, I've had problems with neither option working when the surrounding <h:form> had an enctype attribute.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 1
    You want `ajax="false"` if you want to have a synchronous request with a navigation. As to the `update` parameter, see my comment in [one of your previous questions](http://stackoverflow.com/questions/4605118/jsf2-exeptions-while-submiting-to-bean) which you didn't respond to yet. – BalusC Jan 09 '11 at 13:39
  • Sry, just forgot about that -.- – Sven Jan 09 '11 at 13:56
  • As to the `enctype="multipart/form-data"` issue, for that you need to ensure that you've configured the PrimeFaces `FileUploadFilter` as per the instructions in their User Guide. – BalusC Jan 26 '11 at 19:42
  • @BalusC: Ah, and now I know where that `enctype` came from - the file upload turned out to be unnecessary, but the attribute stayed around to mess up things... Thanks! – Michael Borgwardt Jan 26 '11 at 21:11
31

I think what Cagatay ment was:

Does not navigate the page:

<p:commandButton action="home.xhtml" value="Go Home"/>

Works (redirects the page):

<p:commandButton action="home.xhtml?faces-redirect=true" value="Go Home"/>

Works (redirects the page)

<p:commandButton action="home?faces-redirect=true" value="Go Home"/>

if .xhtml is your default postfix defined in web.xml:

<context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

More info can be found in the primefaces forum, for example in this discussion about navigation

I hope that helped a bit...

Gamenoob
  • 3
  • 5
alfonx
  • 6,936
  • 2
  • 49
  • 58
8

You must use redirect if you are doing navigation and request is done with ajax.

Cagatay Civici
  • 6,406
  • 1
  • 29
  • 34
4

I am having a similar problem.

I am using primefaces and primefaces mobile and the navigation fails in Firefox when using <p:commandButton> with ajax="false" and action="find.xhtml?faces-redirect=true".

The only way I could make it work also in Firefox was by using <h:commandButton>:

<h:commandButton value="#{msg.find}" process="find" 
                     action="find.xhtml?faces-redirect=true" />
zzirna
  • 69
  • 2
  • 7
0

none of the above solution worked for me. You can still go for simple javascript

<p:commandButton onclick="window.location.href =home.xhtml"
                    style="height: 20px" type="button" title="title" icon="ui-icon-transferthick-e-w"/>
Toumi
  • 2,925
  • 4
  • 37
  • 31
0
<h:commandButton 
                             styleClass="btn btn-primary-active upgrade" data-dismiss="modal" 
                             value="Upgrade Account" 
                             action="profile.do?faces-redirect=true&amp;tab=plan" 
                             oncomplete="$('#freeGiftModal').hide();" process="@this" partialSubmit="true" immediate="true"/>

I used the commandbutton to navigate to profile page, and its working fine for me.

  • 1
    Your answer isffectively not different from others and in a way it is worse. Besides it not beimg a `p:commandButton`, which with the right attributes 'just works' it containsttributes that are not valid for an `h:commandButton`, mor that one of the other answers – Kukeltje Nov 11 '18 at 08:14