0

I'm trying to set up a voting app that will display whether the user is able to vote or not using an if statement in my bean class but this Unable to find matching navigation case with from-view-id '/home.xhtml' for action '#{user.checkAge(user.age)}' with outcome 'Invalid User, Please Try Again!!!'. Im not very understanding of Java Server Faces yet and ive tried messing around with the config files and googling the error but i cant fix it. Can anyone help me please.

Here is my code:

Home.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <head>
        <title>Results Page</title>
    </head>
    <body>
        <h:form>
           Name: <h:outputText id="outTxt" value="#{user.name}"/><br></br>
           Age: <h:outputText id="outTxt2" value="#{user.age}"/><br></br>
            <h:commandButton id="cmdBtn" value="Check" action="#{user.checkAge(user.age)}"/>
        </h:form>
    </body>
</html>

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Home Page</title>
    </h:head>
    <h:body>

    <h:body> 
       <h:form>
           Name: <h:inputText id="inTxt" value="#{user.name}"/><br></br>
           Age: <h:inputText id="inTxt2" value="#{user.age}"/><br></br>
            <h:commandButton id="cmdBtn" value="Check" action="home"/>
        </h:form>
    </h:body>
    </h:body>
</html>

User.java

package MyPackage;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class User 
{
private String name;
private int age;
private String msg;

    public String getMsg() 
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public int getAge() 
    {
        return age;
    }

    public void setAge(int age) 
    {
        this.age = age;
    }
     public String checkAge(int checkAgeVoter)
    {
        if(checkAgeVoter >= 18)
        {
            msg = "Valid User, Access Granted!!!";
        }
        else
        {
            msg = "Invalid User, Please Try Again!!!";
        }
        return msg;
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Aren't Name and age are supposed to be h:inputText ?? aren't you reading them from user? If not where would you get value for age? – Kishor Prakash Sep 29 '13 at 09:30

2 Answers2

1

User.checkAge() is used as JSF action. Therefore its return value is interpreded as "outcome" which is fed into JSF navigation logic. JSF tries to look up a navigation rule for the current view (/home.xhtml) and your "outcome" to find out which page to display next. It can not find any.

You might want to return null from checkAge (or make the method void) causing JSF navigation to display the current page again. However, I did not see any place where User.msg would be displayed. So you might want to navigate to another page which does that and return the proper outcome for a navigation rule. (Note: Your user inputs data in index.html so you might want to reference your input validating action there and return "/home.xhtml".) For more details on navigation in modern JSF, see Faces Navigation not really working in JSF2

Note that there is a mechanism for displaying messages in JSF based on the tags h:messages (and h:message) and FacesContext.getCurrentInstance().addMessage() (and validation). For an example, see http://www.javabeat.net/2008/04/display-error-messages-in-jsf-hmessages-part-1-2/


You might try:

  1. In home.xhtml: After <f:form> add Msg: <h:outputText value="#{user.msg}"/><br/>.
  2. In index.xhtml: Replace action="home" with action="#{user.checkAge(user.age)}".
  3. In User.java: In checkAge() replace return msg; with return "home"; (or "/home.xhtml").

Assuming index.html is your starting page, this checks entered data and navigates to home page (assuming action="home" already worked).

Community
  • 1
  • 1
halfbit
  • 3,414
  • 1
  • 20
  • 26
0

This is not the best way to do it, but just to well inderstand how the navigation goes on by the famous configuration file faces-config :

First, you can omit the msg attribute, and modify the business method as :

public String checkAge(int checkAgeVoter) {
    if (checkAgeVoter >= 18) return "granted";
    else return "denied";
}

With this faces-config :

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">

<navigation-rule>
<from-view-id>/index</from-view-id>
    <navigation-case>
        <from-outcome>home</from-outcome>
        <to-view-id>/home.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
</navigation-rule>

<navigation-rule>
<from-view-id>/home.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>granted</from-outcome>
        <to-view-id>/votepage.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
    <navigation-case>
        <from-outcome>denied</from-outcome>
        <to-view-id>/errorpage.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
</navigation-rule>

And adding 2 test pages: votepage.xhtml in case of success, and errorpage.xhtml else.

Omar
  • 1,430
  • 1
  • 14
  • 31
  • Great example... Thank you. If i was to do it the proper way how would i go about it :) – Becky O Brien Sep 29 '13 at 12:24
  • You're welcome. The proper way is what M. @halfbit noted, you should overcome the use of `faces-config`, and use the JSF actions in your backing-bean which are methods that return `String`, that are by turn, interepted as `` element in `faces-config` (assuming that you're using JSF 2.X version). To do so, you should delete that configuration file, and modify the JSF action method as : `public String checkAge(int checkAgeVoter) { if (checkAgeVoter >= 18) return "votepage?faces-redirect=true"; else return "errorpage?faces-redirect=true"; }` – Omar Sep 29 '13 at 14:13