Passing redirect-param in faces-config in jsf-2.2

烈酒焚心 提交于 2021-02-07 17:09:00

问题


In old jsf the following code was working

<navigation-rule>
    <from-view-id>/page1.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>true</from-outcome>
        <to-view-id>/page2.xhtml</to-view-id>
        <redirect>
            <view-param>
                <name>id</name>
                <value>#{myBean.id}</value>
            </view-param>
        </redirect>
    </navigation-case>
</navigation-rule>

page1.xhtml code :

<f:metadata>
    <f:viewParam id="id" name="id" value="#{myBean.id}"  />
    <f:viewAction action="#{myBean.init()}"/>
</f:metadata>

Java code :

public class MyBean(){
   private double id;

   public boolean init(){
      if(id > 0)
         return true;
      else
         return false;
   }
}

In the success scenario when page1.xhtml?id=0 page1 will be opened while page1.xhtml?id=1 navigation to page2 with parameter id=1.

navigation to page2.xhtml?id=1 with parameter is needed since in page2 on PostConstruct or <f:viewAction> parameter is read and needed to find object according to this id

Using jsf 2.2 with mojarra javax.faces-2.2.8 implementation in faces-config.xml file there is no <view-param> there is <redirect-param> changing them gives no success scenario where navigation is without id where it will navigate to page2.xhtml instead of page2.xhtml?id=1


回答1:


You can do it the old way. Instead of using <redirect-param> use <view-param>. The xsd (http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd) will mark it as false but mojarra javax.faces-2.2.8 will silently use it the right way you want it.

Edit: The XSD will be fixed in the Mojarra 2.3 release, see the other 'answer'

Example:

<?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 http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2">
    <navigation-rule>
        <navigation-case>
            ...
            <redirect>
                <view-param>
                    <name>foo</name>
                    <value>bar</value>
                </view-param>
            </redirect>
        </navigation-case>
    </navigation-rule>
</faces-config>



回答2:


Try using include-view-params="true" attribute in <redirect> element of faces-config.xml instead of declaring parameters using <view-param> tag.
Also be sure to declare <f:viewParam> in the target page (page2.xhtml).
I believe I made your example work as you expected like this:

Navigation rule in faces-config.xml:

<navigation-rule>
    <from-view-id>/page1.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>true</from-outcome>
        <to-view-id>/page2.xhtml</to-view-id>
        <redirect include-view-params="true" />
    </navigation-case>
</navigation-rule>

page1.xhtml code (unchanged):

<f:metadata>
    <f:viewParam id="id" name="id" value="#{myBean.id}" />
    <f:viewAction action="#{myBean.init()}"/>
</f:metadata>

page2.xhtml code:

<f:metadata>
    <f:viewParam id="id" name="id"/>
</f:metadata>

ManagedBean (unchanged):

public class MyBean {
    private double id;

    public boolean init(){
        if(id > 0)
            return true;
        else
            return false;
    }

    // getter & setter for id
}



回答3:


Use the old <view-param> tag on configuration, as Jaxt0r mentioned on https://stackoverflow.com/a/34773335/2521247

This is a bug on JSF that is fixed for 2.3. See https://java.net/jira/browse/JAVASERVERFACES-3399



来源:https://stackoverflow.com/questions/27149787/passing-redirect-param-in-faces-config-in-jsf-2-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!