登录页面使用JSF

时间:2012-06-13 10:16:56

标签: mysql jsf jsf-2 login

任何人都可以帮我读取数据库值,如果成功则将其传递给jsf中的下一页。

这是我的代码。

      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
template="/templates/template.xhtml"
xmlns:rich="http://richfaces.org/rich">

<ui:define name="body">
    <h:form>

        <rich:panel header="Employee Log In Page" style="width: 315px">
            <h:panelGrid columns="2">

                <h:outputText value="EmployeeId: " />

                <h:inputText id="employeeId" value="">

                    <f:validateLength minimum="30" maximum="30" />

                </h:inputText>

                <h:outputText id= "password" value="PassWord: " />

                <h:inputText value="">

                    <f:validateLength minimum="15" maximum="15" />
                </h:inputText>
                <h:commandButton action="#{ }" value="Log In" />
            </h:panelGrid>
        </rich:panel>
    </h:form>
</ui:define>
</ui:composition>

Connectivity.java

    import java.sql.DriverManager;
   import java.sql.ResultSet;
  import java.util.ArrayList;

  import antlr.collections.List;

   import com.model.UserDetails;
 import com.mysql.jdbc.Connection;
  import com.mysql.jdbc.Statement;


 public class Connectivity {

public static void main(String[] argv) {

    System.out.println("-------- MySQL JDBC Connection Testing ------------");

int i=0;

Connection con ;
Statement ps;
ResultSet rs;



  try
  {
  Class.forName("com.mysql.jdbc.Driver");
  con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/LeaveApp",
"root","root");

  ps = (Statement) con.createStatement();
  rs = ps.executeQuery("select * from Login");
  while(rs.next()){
  System.out.println(rs.getString(1));
  System.out.println(rs.getString(2));
  System.out.println(rs.getString(3));

  i++;

  }

  }
  catch (Exception e)
  {
  System.out.println("Error Data : " + e.getMessage());
  }
}  
}

UserDetails.java

       package com.model;

       public class UserDetails {
public int login_id;
public String user_name;
public String user_password;
public int getLogin_id() {
    return login_id;
}
public void setLogin_id(int login_id) {
    this.login_id = login_id;
}
public String getUser_name() {
    return user_name;
}
public void setUser_name(String user_name) {
    this.user_name = user_name;
}
public String getUser_password() {
    return user_password;
}
public void setUser_password(String user_password) {
    this.user_password = user_password;
}


}

我需要验证登录页面。如何做。

1 个答案:

答案 0 :(得分:0)

在视图中设置值和操作方法:

<h:outputText value="EmployeeId: " />
<h:inputText id="employeeId" value="#{userDetails.employeeId}">
   <f:validateLength minimum="30" maximum="30" />
</h:inputText>
<h:outputText id="password" value="PassWord: " />
<h:inputText value="#{userDetails.password}">
   <f:validateLength minimum="15" maximum="15" />
</h:inputText>
<h:commandButton action="#{userDetails.login}" value="Log In" />

并声明这些字段并在您的支持bean中定义此方法:

private Integer employeeId;
private String password;

public String login() {      
   String result = null;
   boolean isValidUser = userDao.login(employeeId, password);  // verification user's data in db   
   if (isValidUser) {
      result = "success";
   } else { 
      result = "fail";
   }
   return result;
}

public Integer getImployeeId() {
   return employeeId;
}

public void setEmployeeId(Integer employeeId) {
   this.employeeId = employeeId;
}

public String getPassword() {
   return password;
}

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