使用param从动作导航JSF

zer*_*erg 1 jsf jsf-2.2

我有以下导航规则:

<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>
Run Code Online (Sandbox Code Playgroud)

TestMB:

public String test() {
    return "test?id=1";
}
Run Code Online (Sandbox Code Playgroud)

和test.xhtml:

<h:commandLink action="#{testMB.test}">click</h:commandLink>
Run Code Online (Sandbox Code Playgroud)

但是,如果这是行不通的话return "test?id=1";.我不想使用return "/pages/test/edit.xhtml?id=1";,我想使用抽象名称test.

kol*_*sus 5

如果您要在URL中添加GET参数,请知道您不能使用<navigation-case>(test在您的示例中).如果你有一个特定的URL /页面,唯一可行的方法是:

  public String test() {

      return "edit.xhtml?id=1&faces-redirect=true";
  }
Run Code Online (Sandbox Code Playgroud)

重定向+参数机制仅适用于指向的明确页面/ 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>
Run Code Online (Sandbox Code Playgroud)

redirect标签是什么指定的faces-redirect参数,你想要的GET参数一起id.


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

   <redirect>
        <view-param>
            <name>id</name>
            <value>#{yourBean.redirectValue}</value>
        </view-param>
    </redirect>
Run Code Online (Sandbox Code Playgroud)