Question

I'm trying to make a redirection from index.xhtml to registerFirstTime.xhtml.

The code in the page index.xhtml is:

<h:form id="content" style="margin-top: 1em;">
    <p:panel id="panel" header="LOGIN">
        <p:messages id="msgs" globalOnly="true"/>
        <h:panelGrid columns="3">
            <h:outputLabel value="e-mail" />
            <p:inputText id="name" required="true" value="#{UserBean.email}"
                requiredMessage="Required: e-mail" display="icon">
            </p:inputText>
            <p:message id="msgName" for="name"/>
            <h:outputLabel value="Password" />
            <p:password id="password" required="true" value="#{UserBean.password}"
                requiredMessage="Required: Password" display="icon" feedback="false">
            </p:password>
            <p:message id="msgPassword" for="password"/>
        </h:panelGrid>
        <h:panelGrid columns="2">
            <p:commandButton value="Login" actionListener="#{UserBean.validate}" update="msgNombre, msgPassword, msgs"/>
            <h:commandButton value="Register for the first time" action="register"/>
        </h:panelGrid>
    </p:panel>
</h:form>

While, the content of the redirection, in faces-config.xml, is:

<navigation-rule>
    <from-view-id>index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>register</from-outcome>
        <to-view-id>registerFirstTime.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

Until the input name and password are filled, you can't do the redirect. How I can get do the redirection of the record without taking into account the required fields? Thanks! =)

Was it helpful?

Solution

Add immediate="true" to the command button which should skip processing of all input field which do not have the immediate="true" attribute.

<h:commandButton value="Register for the first time" action="register" immediate="true" />

Unrelated to the concrete problem, please note that you're technically not sending a redirect here. This basically sends a forward, i.e. the URL in the browser address bar remains the URL of the login page. You need to add <redirect/> to the <navigation-case>. Also please note that navigation cases are an leftover of JSF 1.x. Since JSF 2.x you can perform "implicit navigation" whereby you just specify the view ID as outcome.

<h:commandButton value="Register for the first time" action="registerFirstTime?faces-redirect=true" immediate="true" />

This way you can get rid of the navigation case altogether.

OTHER TIPS

@BalusC solution should do the trick, as always. But I'd like to point out a couple of things since you seem to be using Primefaces. Both are unrelated to the question but might help you out at some point.

First, you could use implicit navigation (introduced in JSF2). That way you wouldn't need to define all the navigation rules in your faces-config.xml file (I work on an old JSF 1.2 project and hate the need to define the navigation roles for everything). Here's how you'd do it:

<h:commandButton value="Register for the first time" action="registerFirstTime.xhtml?faces-redirect=true"/>

The faces-redirect parameter forces a redirect instead of a forward, in case you need that.

Also, say you want to properly process some values, but not all of them. In that case, you can use the process attribute of p:commandButton or p:ajax. For example

<h:commandButton value="Register for the first time" action="registerFirstTime.xhtml?faces-redirect=true" process="@this, name"/>

This would make JSF process only the button (@this) and the component with id="name" (your e-mail field). Again, it probably doesn't apply to this question, but it's something I use often.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top