未应用导航规则

Navigation rule not applied

我需要在提交表单后导航到不同的页面,具体取决于用户在逗号分隔的数字字段中输入的值。

comsep和submit按钮的取值代码如下:

      <h:inputText id="comsep" value="#{bean.comsep}" ></h:inputText>
      <h:commandButton value="Submit" action ="bean.func"></h:commandButton>

这是豆子:

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ManagedProperty;

    @ManagedBean
    public class Bean 
    {
      public String comsep;

     public Bean()
    {
    }

     public String func()
     {
        double avg = calculate();
        if (avg == 10) 
            {   return "casea";   } 
        else 
            {   return "caseb";   }
     }

    public double calculate() 
       {
       String[] pos = comsep.split(",");
       double avg = 0;
       for (int i = 0; i < pos.length; i++)
       avg = avg + Integer.parseInt(pos[i]);
       avg = avg / pos.length;
       return avg;
       }


public String getComsep() {
    return comsep;
}

public void setComsep(String comsep) {
    this.comsep = comsep;
}

}

这是面孔-config.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee          h  http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

   <navigation-rule>
   <from-view-id>/Home.xhtml</from-view-id> 

   <navigation-case> 
   <from-action>#{bean.func}</from-action>
   <from-outcome>casea</from-outcome> 
   <to-view-id>/Page1.xhtml</to-view-id> 
   </navigation-case> 
   <navigation-case> 
   <from-action>#{bean.func}</from-action>
   <from-outcome>caseb</from-outcome> 
   <to-view-id>/Page2.xhtml</to-view-id> 
   </navigation-case> 
   </navigation-rule>
   </faces-config>

如果 avg == 10 则预期结果为第 1 页,否则为第 2 页,但这并没有发生。当我点击提交时,没有任何反应,但 home.xhtml 被刷新了。

我是 JSF 的新手,对于菜鸟的错误,如果有的话,我深表歉意。

commandButtonaction 属性需要 EL 表达式而不是字符串才能从 bean 调用方法:

<h:commandButton ... action="#{bean.func}"></h:commandButton>

您可以在使用 2.0 JSF 版本或更新版本时放弃使用 "faces-config.xml" 文件。