0

Bean クラスで if ステートメントを使用してユーザーが投票できるかどうかを表示する投票アプリをセットアップしようとしていますが、この Unable to find matching navigation case with from-view-id '/home.xhtml'アクション「#{user.checkAge(user.age)}」の結果「無効なユーザー、再試行してください!!!」。私はまだJava Server Facesについてよく理解していません.configファイルをいじってエラーをグーグルで調べてみましたが、修正できません. 誰でも私を助けてください。

これが私のコードです:

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;
    }
}
4

2 に答える 2

0

これは最善の方法ではありませんが、有名な構成ファイルによってナビゲーションがどのように行われるかをよく理解するためですfaces-config:

まず、msg属性を省略して、ビジネス メソッドを次のように変更できます。

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

これで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>

そして、2 つのテスト ページを追加します:votepage.xhtml成功した場合とerrorpage.xhtmlそうでない場合。

于 2013-09-29T11:54:10.560 に答える