Question

i want to redirect to two different pages according if the user introduces a right password or not. I tried sendRedirect and forward but it didnt work. I have a Managed Bean that has the condition, and the xhtml pages that have to been shown according the condition.

Managed bean

@ManagedBean
@SessionScoped
public class datos {
private String usuario, contraseña, response=null;

public datos() {
}

public String getUsuario() {
    return usuario;
}

public void setUsuario(String usuario) {
    this.usuario = usuario;
}

public String getContraseña() {  
    return contraseña;
}

public void setContraseña(String contraseña) {    
    this.contraseña = contraseña;
}

public void getResponse() throws IOException{
    if(contraseña.equals("1111"))
               **REDIRECT TO PAGE 1**    
    else {
               **REDIRECT TO PAGE 2**
    }
}

}        

Thanks for your help!

Was it helpful?

Solution

Use ExternalContext#redirect

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
if(loginSuccess) {
      externalContext.redirect(url1);
} else {
      externalContext.redirect(url2);
}

OTHER TIPS

It works!! I implemented this if it helps to somebody else: Into the JavaBean:

 public void comprobarContraseña() throws IOException{
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
if(contraseña.equals("1111")) {
  externalContext.redirect("tienda.xhtml");
} else {
  externalContext.redirect("login.xhtml");
}
}   

And in the commandButton inside the xhtml:

<h:commandButton id="submit" value="Envia" action="#{datos.comprobarContraseña}" />

Thank you all!

If you are using command button/link you could write an action method in Bean .specify that your navigation output there.

<h:commandButton value="login"  action="#{yourBeanName.Login}"

In Managed Bean

public String Login()
{

    if(correct login credential)
     {
        return "success";
      }
   else
     {

         return "failure"; 
     }

}  

Write proper navigation rule in faces-config.xml.

I hope this will solve your problem.

Both Code fro jsf to jsp and jsf .

      <h:commandLink value="[#{msg['cancel.hyperlink']}]"
      action="#{editSchemeHandlerCitiPost.redirectToSchemeList}"
      immediate="false"  />


     public String  redirectToSchemeList() throws IOException{
    if(getServletRequest().getRequestURI()!=null && getServletRequest().getRequestURI().contains("editScheme.jsf"))//current page 
    {

       FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("org.ajax4jsf.application.AjaxStateManager.VIEW_STATES_MAP");
        FacesContext.getCurrentInstance().getExternalContext().redirect("/ns/jsp/jsf/close.jsf");
    }else{
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("org.ajax4jsf.application.AjaxStateManager.VIEW_STATES_MAP");
        FacesContext.getCurrentInstance().getViewRoot().setViewId("/sch.do?actionKey=list");
        FacesContext.getCurrentInstance().getExternalContext().redirect("/ns/sch.do?actionKey=list");
    }
    return "";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top