JSF 导航来自带有参数的动作

JSF navigation from action with param

我有以下导航规则:

<navigation-rule>
    <from-view-id>/pages/*</from-view-id>
    <navigation-case>
        <from-outcome>test</from-outcome>
        <to-view-id>/pages/test/edit.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

测试MB:

public String test() {
    return "test?id=1";
}

和test.xhtml:

<h:commandLink action="#{testMB.test}">click</h:commandLink>

但如果 return "test?id=1"; 则不起作用。我不想用return "/pages/test/edit.xhtml?id=1";,我想用抽象名test

如果您希望将 GET 参数添加到 URL,请注意您不能使用 <navigation-case>(在您的示例中为 test)。唯一可行的方法是,如果您有一个特定的 URL/page,如:

  public String test() {

      return "edit.xhtml?id=1&faces-redirect=true";
  }

重定向+参数机制仅适用于明确的 page/URL 指向。它与 JSF 提供的 navigation-case 机制不兼容。


如果出于某种原因,您需要导航案例为导航模式,您需要在 faces-config 文件中定义导航参数:

    <to-view-id>/pages/test/edit.xhtml</to-view-id>
    <redirect>
        <view-param>
            <name>id</name>
            <value>1</value>
        </view-param>
    </redirect>

redirect 标签指定了 faces-redirect 参数,以及您想要的 GET 参数 id .


更好的形式是您不对值进行硬编码,而是通过值绑定使其动态化:

   <redirect>
        <view-param>
            <name>id</name>
            <value>#{yourBean.redirectValue}</value>
        </view-param>
    </redirect>