【问题标题】:Java Server Faces error when trying to use an if method in my Bean class尝试在我的 Bean 类中使用 if 方法时出现 Java Server Faces 错误
【发布时间】:2013-09-29 09:13:25
【问题描述】:

我正在尝试设置一个投票应用程序,该应用程序将显示用户是否能够在我的 bean 类中使用 if 语句进行投票,但是这个 Unable to find matching navigation case with from-view-id '/home .xhtml' 用于操作“#{user.checkAge(user.age)}”,结果为“无效用户,请重试!!!”。我对 Java Server Faces 还不是很了解,我试着弄乱配置文件并用谷歌搜索错误,但我无法修复它。谁能帮帮我。

这是我的代码:

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

【问题讨论】:

  • 姓名和年龄不应该是 h:inputText 吗?你不是从用户那里读到的吗?如果不是,您将在哪里获得年龄价值?

标签: jsf if-statement navigation


【解决方案1】:

User.checkAge() 用作 JSF 动作。因此,它的返回值被解释为输入 JSF 导航逻辑的“结果”。 JSF 尝试查找当前视图 (/home.xhtml) 的导航规则和您的“结果”以找出接下来要显示的页面。它找不到任何东西。

您可能希望从 checkAge 返回 null(或使方法无效),从而导致 JSF 导航再次显示当前页面。但是,我没有看到将显示 User.msg 的任何地方。因此,您可能希望导航到执行此操作的另一个页面并返回导航规则的正确结果。 (注意:您的用户在 index.html 中输入数据,因此您可能希望在此处引用您的输入验证操作并返回“/home.xhtml”。)有关现代 JSF 导航的更多详细信息,请参阅Faces Navigation not really working in JSF2

请注意,在 JSF 中有一种基于标签 h:messages(和 h:message)和 FacesContext.getCurrentInstance().addMessage()(和验证)显示消息的机制。示例见http://www.javabeat.net/2008/04/display-error-messages-in-jsf-hmessages-part-1-2/


你可以试试:

  1. 在 home.xhtml 中:在 &lt;f:form&gt; 之后添加 Msg: &lt;h:outputText value="#{user.msg}"/&gt;&lt;br/&gt;
  2. 在 index.xhtml 中:将 action="home" 替换为 action="#{user.checkAge(user.age)}"
  3. 在 User.java 中:在 checkAge() 中将 return msg; 替换为 return "home";(或 "/home.xhtml")。

假设 index.html 是您的起始页面,这将检查输入的数据并导航到主页(假设 action="home" 已经工作)。

【讨论】:

  • 很难理解导航,我仍然不知道该做什么:(
【解决方案2】:

这不是最好的方法,只是为了更好地理解著名的配置文件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 否则。

【讨论】:

  • 很好的例子...谢谢。如果我要以正确的方式去做,我会怎么做:)
  • 不客气。正确的方法是 M.@halfbit 指出的,你应该克服faces-config 的使用,并在你的 backing-bean 中使用 JSF 操作,这些操作是返回 String 的方法,这些方法依次被解释为 &lt;to-view-id&gt; faces-config 中的元素(假设您使用的是 JSF 2.X 版本)。为此,您应该删除该配置文件,并将 JSF 操作方法修改为:public String checkAge(int checkAgeVoter) { if (checkAgeVoter &gt;= 18) return "votepage?faces-redirect=true"; else return "errorpage?faces-redirect=true"; }
猜你喜欢
  • 2020-07-31
  • 1970-01-01
  • 2015-12-10
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 2019-11-24
相关资源
最近更新 更多