2013-12-19 29 views
0

請在這裏忽略一點。我如何使用隱式導航 例如使用明確的導航處理JSF 2.0中有多個結果的請求,我可以寫:使用JSF 2.0進行隱式導航的多個結果

<navigation-rule> 
    <from-view-id>/products/DeleteItem.xhtml</from-view-id> 
    <navigation-case> 
     <from-outcome>itemDeleted</from-outcome> 
     <to-view-id>/products/Success.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>itemNotExist</from-outcome> 
     <to-view-id>/products/Failure.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 

但是我怎樣才能達到同樣的事情與隱式導航,我曾嘗試以下:

<h:link value="Delete a Product" outcome="Success" /> 

只有一例被提及,但我想要麼轉發給Success.xhtml或Failure.xhtml取決於結果。

這裏有一些隱式導航的附加信息。

管理的bean:

import javax.ejb.EJB; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 

@ManagedBean 
@RequestScoped 
public class DeletionBean { 
@EJB 
private app.session.ProductFacade ejbProductFacade; 


private int id=0; 


public DeletionBean() { 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String deleteProduct(){ 
    String result; 
    result=ejbProductFacade.deleteItem(id); 

    if(result.equals("success")) 
    { 
     status="Successfully performed"; //msg to be shown 

     return "product/DeleteItem"; 
    } 

    else return "product/Failure"; 
} 

} 

<h:form> 
       <table border="1" width="5" cellspacing="1" cellpadding="1"> 

         <tr> 
          <td> Product ID </td> 

          <td> 

           <h:inputText id="pid" size="10" value="#{deletionBean.id}" title="Product ID" required="true" requiredMessage="Product ID required"/> 
          </td> 
         </tr> 

         <tr> 

          <td colspan="2"> 
          <h:commandLink value="Delete" action="#{deletionBean.deleteProduct}" /> 
          </td> 

         </tr> 

       </table> 

      </h:form> 

當前錯誤消息:

無法找到匹配導航的情況下從-view-id的「/product/DeleteItem.xhtml」行動「#{deletionBean.deleteProduct}」的結局「產品/失敗」

回答

2

假如你執行action方法刪除你的產品,你應該讓你的方法返回所需的結果。使用JSF 2,就沒有必要使用結果的ID了,甚至你可以宣佈他們在你faces-config.xml中文件,JSF關係所提供的結果與特定的頁面:

<h:commandLink action="#{bean.deleteProduct(product)}" 
    value="Delete product" /> 
public String deleteProduct(Product p){ 
    //Try to delete the product and return "products/xxx", 
    //which will be converted to "/contextname/products/xxx.xhtml" 
    try{ 
     daoService.delete(p); 
     return "/products/Success"; 
    catch(Exception e){ 
     return "/products/Failure"; 
    } 
} 

那將POST服務器和行爲像一個HTML表單提交按鈕(這就是爲什麼它被稱爲commandLink)。不過,如果你只想執行GET到特定視圖,您可以使用它的視圖id的結果:

<h:link value="Go to success doing nothing" 
    outcome="/products/Success" /> 

參見:

Implicit navigation in JSF 2

+0

感謝@Xtreme騎自行車爲了快速響應,我已經提出了修改,但是我收到以下錯誤消息 –

+0

嘗試使用完整路徑。編輯我的答案。 –

+0

感謝@ Xtreme Biker的快速響應我已經提到了修改,但我收到以下錯誤消息。 **無法找到具有結果'product/Failure'的行爲'#{deletionBean.deleteProduct}'的from-view-id'/product/DeleteItem.xhtml'的匹配導航案例** –