21

I cannot redirect to another page if the code is like this:

<h:commandButton type="button" value="Enter" action="index?faces-redirect=true" >

But the redirect works if the code is:

<h:commandButton type="button" value="Enter" action="index?faces-redirect=true" >
    <f:ajax />
</h:commandButton>

Could anyone explain this? Thanks!

-----------------------EDIT------------------------

The entire xhtml code for your reference:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>

</h:head>

<h:body>
<h:form id="form">
    <h:commandButton id="enterModelButton" type="button" value="Enter" action="index?faces-redirect=true" >
        <f:ajax />
    </h:commandButton>
</h:form>
</h:body>
</html>
ethanjyx
  • 1,970
  • 7
  • 28
  • 50

1 Answers1

52

The <h:commandButton type="button"> doesn't generate a submit button. It just generates a "dead" button without any effects, purely intented to be used for custom onclick="..." scripts and like. This is somewhat a leftover of the dark JSF 1.0/1.1 era, when it wasn't nicely possible to just use plain vanilla HTML in JSF for this kind of things.

The <f:ajax> performs the submit by ajax powers through JSF-generated onclick. It doesn't care about the button's type.

Essentially, removing type="button" and relying on its default type="submit" should fix your problem.

<h:commandButton value="Enter" action="index?faces-redirect=true" />

However, all with all, if this is the real code and you don't actually intend to invoke a bean action, then you're going in completely the wrong direction as to implementing the functional requirement of navigating to a different page by a button. You should be using <h:button> instead.

<h:button value="Enter" outcome="index" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555