-1

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!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
leonishan
  • 1
  • 2
  • 1
    just a question, don't you have any problems with variable named `contraseña` – Mikhail Timofeev Oct 31 '13 at 08:52
  • 1
    share your folders structure and code how your are redirecting to the other page. Not redirecting means do you get any error or just get a blank page or what ? – BholaVishwakarma Oct 31 '13 at 08:59
  • - I dont have any problem with `contraseña`. - I want to redirect to a page named `tienda.xhtml` or, if the condition is negative redirect to `login.xhtml`. The problem is that I dont know if redirection could be done inside the bean or must be done in another class, and if is in another class I dont know how to implement too. Recently I tried ` FacesContext.getCurrentInstance().getExternalContext().dispatch("tienda.xhtml");` but I get this **error**: `/tienda.xhtml @10,62 value="#{datos.response}": The class 'modelos.datos' does not have the property 'response'.` – leonishan Oct 31 '13 at 09:12

4 Answers4

1

Use ExternalContext#redirect

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
if(loginSuccess) {
      externalContext.redirect(url1);
} else {
      externalContext.redirect(url2);
}
vels4j
  • 11,208
  • 5
  • 38
  • 63
0

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!

leonishan
  • 1
  • 2
-1

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.

sar
  • 1,277
  • 3
  • 21
  • 48
-1

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 "";
}
Rishi Gautam
  • 1,948
  • 3
  • 21
  • 31