转发方法在页面加载时触发,而不是在 link 点击时触发

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

调试时,我注意到方法 myBean.forward() 在我被转发到包含下面漂亮的 link 的 home.xhtml 页面时被触发,甚至在点击之前漂亮-link(即产品标签):

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>

漂亮-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();

  }
}

有人可以向我解释为什么会发生这种情况以及如何避免这种情况(即只有在单击 link 时才应触发该方法)。谢谢。

您似乎将 JSF ValueExpression 绑定到 <pretty:link> 组件,但此组件的 ValueExpressions 是在页面呈现时计算的。

这是因为 <pretty:link> 组件旨在创建在浏览器中呈现的 URL 以允许添加书签,而无需回调到 JSF 中以执行方法。这意味着您不能在运行时动态更改 <pretty:link> 的值。

您最好使用 <h:commandLink>(它在回发时执行操作方法,在单击时执行,而不是在呈现时执行),然后您可以从操作方法执行 server-side 导航,因为那似乎是您期望的行为。

基本上,<pretty:link> 旨在做与您想要的完全相反的事情。