【问题标题】:Navigation rule not applied未应用导航规则
【发布时间】:2016-07-14 16:17:41
【问题描述】:

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

以下是获取comsep值并提交按钮的代码:

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

}

这是 faces-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 则预期结果是 page1,否则为 page2,但这并没有发生。当我点击提交时,什么都没有发生,但 home.xhtml 被刷新了。

我对 JSF 还很陌生,对于新手的错误,如果有的话,我深表歉意。

【问题讨论】:

  • 所以它适用于h:inputtext?并且无需在标题中添加“标签”。同时总结标题中的问题。请从一些好的非 jsf 1.2 教程开始
  • @Kukeltje 不,它也不适用于 h:inputtext。

标签: jsf navigation xhtml


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢,但你能说得更清楚点吗?我在网上看到了这样的例子,它们在提交操作时调用了一个 bean 方法 - bean.func 如果它需要一个 EL,人们如何执行导航?因为 faces-config.xml 有根据字符串写的导航用例。
  • 在这种情况下,人们通过在该方法操作的字符串返回语句中直接指定目标页面名称来进行导航,例如:public String func(){ ... return "somePage.xhtml"; }
  • 谢谢奥马尔。你的解决方案是对的。但是,我犯了另一个愚蠢的错误,即在没有卷曲和 # 的情况下调用提交按钮上的操作;动作="studentService.display()" .
  • 不客气。这是预期的行为,只要操作方法属性值无效(不是 EL 表达式或不是有效的页面名称),页面就会刷新或什么也没有发生。
猜你喜欢
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多