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

4.3k views Asked by At

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

3

There are 3 answers

0
Evgeny Kharitonov On

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
}
0
JaXt0r On

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>
0
german1410 On

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://github.com/eclipse-ee4j/mojarra/issues/3403