soulemane moumie
soulemane moumie

Reputation: 1175

Multiple outcomes in implicit navigation with JSF 2.0

Please I am missing a point here. How can I handle a request with multiple outcomes in JSF 2.0 using the implicit navigation For example using the explicit navigation I can write the following:

 <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>

However How can I achieve the same thing with the implicit navigation, I have tried the following :

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

only one case is mentioned but I want to forward either to Success.xhtml or Failure.xhtml depending on the outcome.

Here a some additional information for the implicit navigation.

Managed 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";
} 

}

Form:

<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>

Current error message:

Unable to find matching navigation case with from-view-id '/product/DeleteItem.xhtml' for action '#{deletionBean.deleteProduct}' with outcome 'product/Failure'

Upvotes: 0

Views: 1494

Answers (1)

Aritz
Aritz

Reputation: 31649

Supposing you'll perform an action method to delete your product, you should make your method return the desired outcome. Using JSF 2, there's no need of using outcome ids anymore, even you can declare them in your faces-config.xml file, JSF ties a provided outcome with a specific page:

<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";
    }
}

That will POST the server and behaves like an HTML form submit button (that's why it's called commandLink). Nevertheless if you want just to perform a GET to an specific view, you can use its view id in the outcome:

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

See also:

Implicit navigation in JSF 2

Upvotes: 2

Related Questions