簡體   English   中英

在Play框架1.2.5中處理來自本地Postgres的數據

[英]processing data from local postgres in play framework 1.2.5

我正在使用playframwork 1.2.5開發一個簡單的登錄應用程序,並建立了與本地postgres數據庫的連接。 問題是試圖從數據庫讀取以驗證登錄信息和用戶是否匹配。

問題是我在url:... / application / signin上得到一個空白頁面,而從signIn方法呈現的html頁面應輸出以下內容:TODO寫內容

ps:我上傳的jdbc驅動是lib

謝謝

    App/conf
    # If you need a full JDBC configuration use the following :
   db.url=jdbc:postgresql:postgres
   db.driver=org.postgresql.Driver
   db.user=postgres
   db.pass=******
   #
  ---------------------------------------------
     the login page
     <body>
        #{form @Application.signIn()}
        <form>
            Online ID <input type="text" name ="onlineID" value="${onlineID}"><br>
            Passcode  <input type="text" name ="passcode" value="${passcode}">
            <input type ="submit" value="Sign in">
        </form>
       #{/form}

      </body>
    ---------------------------------------------
   controller.App.java
   public static void signIn(String onlineID, String passcode) {

         login existing = new login(onlineID,passcode);
         boolean verify=existing.check(existing.getOnlineID(),existing.getPassCode());
         if(verify)
           render("@Application.account");

    }


    ---------------------------------------------
    model.login.java

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import play.data.validation.*;
    import play.db.DB;

    public class login {
    @Required public String onlineID;
    @Required public String passcode;

    public login(){}
    public login(String onlineID,String passcode){
    this.onlineID=onlineID;
    this.passcode=passcode;
    }

    public void setOnlineID(String onlineID){
    this.onlineID=onlineID;
    }
    public String getOnlineID(){
    return this.onlineID;
    }

    public void setPassCode(String passcode){
    this.passcode=passcode;
    }
    public String getPassCode(){
    return this.passcode;
    }

    public boolean check(String onlineID,String passcode){

            boolean verify=false;
            Connection conn=null;
            Statement stmt=null;
            try{

                conn = DB.getConnection();
                stmt=conn.createStatement();
                String sql;
                sql="SELECT Cust_ID,password from corebanking.customer";
                ResultSet rs = stmt.executeQuery(sql);

                while(rs.next()){
                     int uID=rs.getInt("Cust_ID");
                     String ssn=rs.getString("SSN");
                     String pas=rs.getString("password");
                     String loginID=Integer.toString(uID);

                     if (onlineID.equals(loginID) && passcode.equals(pas)){
                         verify=true; 
                     }

                }

                rs.close();
                stmt.close();
                conn.close();
            }catch(SQLException se){
                se.printStackTrace();

            }catch(Exception e){
                e.printStackTrace();
            }finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
       return verify;
       }


      }

      ---------------------------------------------
      routes
      GET     /                                       Application.index

      GET    /                                       controllers.Application.signIn()

問題是當您訪問頁面:... / application / signin以顯示登錄表單時。 由於登錄頁面不是靜態資源,因此Play Framework必須通過控制器才能進入該頁面。

但是,您的signin方法中的Application控制器在第一個請求中未獲取任何參數,並且verify變量為false,因此返回空白頁。

您應該在Application控制器中創建另一個方法,如下所示:

public static void showLogin() {
    render("/path/to/login.html");
}

您的第一個請求應該發送到... / application / showLogin。 然后,您將獲得登錄頁面,並在提交了正確的用戶名和密碼后,應該會看到“帳戶”頁面。

但是,當您丟失用戶名或密碼時,仍然會出現空白屏幕。 您應該在登錄方法中添加以下內容

public static void signIn(String onlineID, String passcode) {

     login existing = new login(onlineID,passcode);
     boolean verify=existing.check(existing.getOnlineID(),existing.getPassCode());
     if(verify)
       render("@Application.account");
     // the following code is added
     else 
       render("@Application.authenticationFailed")

}

我希望這能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM