Search code examples
jsfprettyfaces

Forward method is fired at page loading time not at pretty link clicking time


While debugging, I have noticed that the method myBean.forward() get fired when I am getting forwarded to the home.xhtml page containing the pretty-link below and that even before clicking on the pretty-link (i.e. product label) :

home.xhtml

<pretty:link target="_self" mappingId="#{myBean.forward(product, true)}"  >
       <f:param value="#{vo.product.hash }" />        
       <ice:outputText value="#{vo.product.label}"/>
</pretty:link>

pretty-config.xml

<url-mapping id="myApp/seller/addProduct">
        <pattern value="/myApp/seller/addProduct/#{fooBean.productHash}" />
        <view-id value="/pages/seller/products/addProduct.xhtml" />
        <action>#{myBean.myMethod}</action>          
</url-mapping>

MyBean.java

public class MyBean{
    //source code omitted
    public String forward(Product p, boolean pretty){

        if(prettyLink)  
           return "myApp/seller/addProduct";    
        else 
           return fooBean.getContextService().getBaseURL()+"/myApp/seller/addProduct/"+p.getHash();

  }
}

Could someone please explain me why this occurs and how to avoid it (i.e. the method should be fired only when the link is being clicked). Thank you.


Solution

  • You appear to be binding a JSF ValueExpression to a <pretty:link> component, but ValueExpressions are for this component are evaluated at page rendering time.

    This is because the <pretty:link> component is designed to create a URL that is rendered in the browser to allow bookmarking, without calling back into JSF to execute a method. This means you cannot dynamically change the value of a <pretty:link> at runtime.

    You would be better off using an <h:commandLink> (which executes an action method on postback, when clicked, not when rendered), then you can perform server-side navigation from your action method since that appears to be the behavior you are expecting.

    Basically, <pretty:link> is designed to do the exact opposite of what you want.