Question

i'm trying to implement a very basic (at this point) login screen with JSF 2 (using NetBeans). i've found several examples, from which i copied the source code, but so far unsuccessfully. in my login.xhtml file i have a 2 input fields and a command button -

<h:commandButton value="go!" actionListener="#{LoginBean.CheckValidUser}" />

in my LoginBean, the validation function looks like this -

public String CheckValidUser() { 
    if(username.equals("admin") && password.equals("admin")){ 
        return "welcome"; 
    } 
    else{ 
       return "fail";
    } 
} 

and to my faces-config.xml i've added the following navigation rule:

<navigation-rule>
    <description>login success</description>
    <from-view-id>/login.xhtml</from-view-id>
    <navigation-case>
        <from-action>#{LoginBean.CheckValidUser}</from-action> 
        <from-outcome>welcome</from-outcome>
        <to-view-id>/welcome.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
</navigation-rule>

anyhow - i just won't work, i enter the correct UN and PW but i don't get to 'welcome.xhmtl'. when i debug the code, i see in the validation function it works as expected, but i can i know that the faces-config.xml is configured correctly?

what did i miss? cheers, eRez

Was it helpful?

Solution

Here,

<h:commandButton value="go!" actionListener="#{LoginBean.CheckValidUser}" />

You should be using action instead of actionListener. Fix it accordingly:

<h:commandButton value="go!" action="#{LoginBean.CheckValidUser}" />

Action listeners are supposed to return void and they are not intented to navigate.

See also:


Unrelated to the concrete problem, remove the <navigation-rule> altogether. It's unnecessary since JSF 2.0 with the new Implicit Navigation support. An outcome of "welcome" will implicitly go to "welcome.xhtml" already. Make sure that you're reading JSF 2.x targeted resources/books/tutorials and not JSF 1.x ones. JSF 2.x is a major change as compared to JSF 1.x.

See also:

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