无法找到与from-view-id'/ login.xhtml'匹配的导航案例,以获取结果'login-success.xhtml'的行为'#{user.login}'

时间:2014-03-06 20:32:33

标签: jsf jsf-2 login

我无法通过此登录表单登录,而不是登录成功页面,我得到以下

      Unable to find matching navigation case with from-view-id '/login.xhtml' for action '#{user.login}' with outcome 'login-success.xhtml?faces-redirect=true'

login.xhtml //这是登录的登录页面

      <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html">
      <h:head>
    <title>Login Here</title>
      </h:head>
     <h:body>
    <h:form>
        <br />
        <h3 align="center">Login Here!</h3>
        <table align="center" border="2" width="40%" cellpadding="5">
            <tr>
                <td>Name:</td>
                <td><h:inputText id="name" value="#{user.name}" required="true"
                                 requiredMessage="Enter name!" label="name" maxlength="20"
                                 autocomplete="no" /></td>
            </tr>

            <tr>
                <td>Password:</td>
                <td><h:inputSecret id="password" value="#{user.password}"
                                   required="true" requiredMessage="Enter Password!" label="password"
                                   maxlength="15" redisplay="true" autocomplete="no" />          </td>

            </tr>
            <tr>
                <td colspan="3" align="center"><h:commandButton id="btn"
                                                                value="Login" type="submit" action="#{user.login}" /></td>
            </tr>
         </table>
         </h:form>
         </h:body>
          </html>

UserManagedBean.java

       package beans;

       import java.sql.Connection;
       import java.sql.ResultSet;
       import java.sql.Statement;
       import javax.faces.bean.ManagedBean;
       import javax.faces.bean.SessionScoped;
       import javax.faces.context.FacesContext;
       import javax.servlet.http.HttpSession;


       @ManagedBean(name = "user")
       @SessionScoped
 public class UserManagedBean {

/**
 * Creates a new instance of UserManagedBean
 */
public UserManagedBean() {

}
private String name;
private String password;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

DatabaseClass obj = new DatabaseClass();
public Connection connection = null;
Statement statement = null;
ResultSet res;

/*  public String register()
 {
 System.out.println("----------- register Method Called ---------");
 try {
 obj.dbRegister(name, password, email, contact, address);

 } catch (Exception e) {
 e.printStackTrace();
 }
 return "register-success?faces-redirect=true";
 }*/
public String login() {
    System.out.println("----------- register Method Called ---------");
    try {
        //  obj.login(name, password);
        connection = obj.getMySqlConnection();

        statement = connection.createStatement();

        System.out.println("-------- LOGIN in User Table -------------------");
        ResultSet rs;
        rs = statement.executeQuery("select * from members where uname='" + name + "' and pass='" + password + "'");
        if (rs.next()) {
            FacesContext context = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
            session.setAttribute("userid", name);
            return "login-success.xhtml?faces-redirect=true";
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return "login-failure.xhtml?faces-redirect=true";
}

public String logout() {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
    if (session == null) {
        return "logout-invalid";
    } else {
        session.invalidate();
        return "login";
    }

}

}

面-config.xml中

   <?xml version='1.0' encoding='UTF-8'?>
   <faces-config version="2.2"
          xmlns="http://xmlns.jcp.org/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">


   <navigation-rule>
    <from-view-id>/login.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/login-success.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>failure</from-outcome>
        <to-view-id>/login-failure.jsp</to-view-id>
    </navigation-case>
    </navigation-rule>
   </faces-config>

请帮帮我..

提前致谢

1 个答案:

答案 0 :(得分:0)

根据faces-config中的导航情况,从login方法返回“success”而不是“login-success.xhtml?faces-redirect = true”。如果要重定向,请将导航规则更改为

 <navigation-rule>
    <from-view-id>/login.xhtml</from-view-id>
    <navigation-case>
      <from-outcome>success</from-outcome>
      <to-view-id>/login-success.jsp</to-view-id>
      <redirect />
    </navigation-case>
    <navigation-case>
      <from-outcome>failure</from-outcome>
       <to-view-id>/login-failure.jsp</to-view-id> 
       <redirect />
    </navigation-case>
</navigation-rule>