基本的登录框(Basic Login)

This is my first stab at a tutorial, so hopefully it works out! Thanks to crafter for his code examples in this thread: http://extjs.com/forum/showthread.php?t=26320


Contents

[hide]

基本的登录框(Basic Login) Entry Point

Let's assume the entry point to your application is index.asp, and it is structured as follows:


<html>
	<head>
	<link>	
	<script></script>
	<script></script>
	<script></script>	
	</head>
	<body></body>
</html>

Obviously, modify the paths to your EXT directory accordingly. See the source for Login.js below

基本的登录框(Basic Login) Login.js

Next comes login.js. This guy handles all the heavy lifting, and for me, has all the pieces I was missing coming from a more traditional way of thinking about user authentication. It creates the form, renders it to a popup window, presents the window to the user, sends the submission via ajax, and handles the success and failure response depending on whether your user entered successful credentials.

基本的登录框(Basic Login)基本的登录框(Basic Login)Ext.onReady(function()基本的登录框(Basic Login){
基本的登录框(Basic Login)    Ext.QuickTips.init();
基本的登录框(Basic Login) 
基本的登录框(Basic Login)    
// Create a variable to hold our EXT Form Panel. 
基本的登录框(Basic Login)
    // Assign various config options as seen.     
基本的登录框(Basic Login)基本的登录框(Basic Login)
    var login = new Ext.FormPanel(基本的登录框(Basic Login)
基本的登录框(Basic Login)        labelWidth:
80,
基本的登录框(Basic Login)        url:
'login.asp'
基本的登录框(Basic Login)        frame:
true
基本的登录框(Basic Login)        title:
'Please Login'
基本的登录框(Basic Login)        width:
230
基本的登录框(Basic Login)        padding:
200
基本的登录框(Basic Login)        defaultType:
'textfield',
基本的登录框(Basic Login)    monitorValid:
true,
基本的登录框(Basic Login) 
基本的登录框(Basic Login)    
// Specific attributes for the text fields for username / password. 
基本的登录框(Basic Login)
    // The "name" attribute defines the name of variables sent to the server.
基本的登录框(Basic Login)基本的登录框(Basic Login)
        items:[基本的登录框(Basic Login)
基本的登录框(Basic Login)                fieldLabel:
'Username'
基本的登录框(Basic Login)                name:
'loginUsername'
基本的登录框(Basic Login)                allowBlank:
false 
基本的登录框(Basic Login)基本的登录框(Basic Login)            }
,基本的登录框(Basic Login)
基本的登录框(Basic Login)                fieldLabel:
'Password'
基本的登录框(Basic Login)                name:
'loginPassword'
基本的登录框(Basic Login)                inputType:
'password'
基本的登录框(Basic Login)                allowBlank:
false 
基本的登录框(Basic Login)            }
],
基本的登录框(Basic Login) 
基本的登录框(Basic Login)    
// All the magic happens after the user clicks the button     
基本的登录框(Basic Login)基本的登录框(Basic Login)
        buttons:[基本的登录框(Basic Login)
基本的登录框(Basic Login)                text:
'Login',
基本的登录框(Basic Login)                formBind: 
true,     
基本的登录框(Basic Login)                
// Function that fires when user clicks the button 
基本的登录框(Basic Login)基本的登录框(Basic Login)
                handler:function()基本的登录框(Basic Login)
基本的登录框(Basic Login)基本的登录框(Basic Login)                    login.getForm().submit(
基本的登录框(Basic Login)
基本的登录框(Basic Login)                        method:
'POST'
基本的登录框(Basic Login)                        waitTitle:
'Connecting'
基本的登录框(Basic Login)                        waitMsg:
'Sending data基本的登录框(Basic Login)',
基本的登录框(Basic Login) 
基本的登录框(Basic Login)            
// Functions that fire (success or failure) when the server responds. 
基本的登录框(Basic Login)
            // The one that executes is determined by the 
基本的登录框(Basic Login)
            // response that comes from login.asp as seen below. The server would 
基本的登录框(Basic Login)
            // actually respond with valid JSON, 
基本的登录框(Basic Login)
            // something like: response.write "{ success: true}" or 
基本的登录框(Basic Login)
            // response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 
基本的登录框(Basic Login)
            // depending on the logic contained within your server script.
基本的登录框(Basic Login)
            // If a success occurs, the user is notified with an alert messagebox, 
基本的登录框(Basic Login)
            // and when they click "OK", they are redirected to whatever page
基本的登录框(Basic Login)
            // you define as redirect. 
基本的登录框(Basic Login)
 
基本的登录框(Basic Login)基本的登录框(Basic Login)                        success:
function()基本的登录框(Basic Login)
基本的登录框(Basic Login)基本的登录框(Basic Login)                            Ext.Msg.alert(
'Status''Login Successful!'function(btn, text)基本的登录框(Basic Login){
基本的登录框(Basic Login)基本的登录框(Basic Login)                   
if (btn == 'ok')基本的登录框(Basic Login){
基本的登录框(Basic Login)                                
var redirect = 'test.asp'
基本的登录框(Basic Login)                                window.location 
= redirect;
基本的登录框(Basic Login)                                   }

基本的登录框(Basic Login)                    }
);
基本的登录框(Basic Login)                        }
,
基本的登录框(Basic Login) 
基本的登录框(Basic Login)            
// Failure function, see comment above re: success and failure. 
基本的登录框(Basic Login)
            // You can see here, if login fails, it throws a messagebox
基本的登录框(Basic Login)
            // at the user telling him / her as much.  
基本的登录框(Basic Login)
 
基本的登录框(Basic Login)基本的登录框(Basic Login)                        failure:
function(form, action)基本的登录框(Basic Login)
基本的登录框(Basic Login)基本的登录框(Basic Login)                            
if(action.failureType == 'server')基本的登录框(Basic Login)
基本的登录框(Basic Login)                                obj 
= Ext.util.JSON.decode(action.response.responseText); 
基本的登录框(Basic Login)                                Ext.Msg.alert(
'Login Failed!', obj.errors.reason); 
基本的登录框(Basic Login)基本的登录框(Basic Login)                            }
else基本的登录框(Basic Login)
基本的登录框(Basic Login)                                Ext.Msg.alert(
'Warning!''Authentication server is unreachable : ' + action.response.responseText); 
基本的登录框(Basic Login)                            }
 
基本的登录框(Basic Login)                            login.getForm().reset(); 
基本的登录框(Basic Login)                        }
 
基本的登录框(Basic Login)                    }
); 
基本的登录框(Basic Login)                }
 
基本的登录框(Basic Login)            }

基本的登录框(Basic Login)    }
);
基本的登录框(Basic Login) 
基本的登录框(Basic Login) 
基本的登录框(Basic Login)    
// This just creates a window to wrap the login form. 
基本的登录框(Basic Login)
    // The login object is passed to the items collection.       
基本的登录框(Basic Login)基本的登录框(Basic Login)
    var win = new Ext.Window(基本的登录框(Basic Login){
基本的登录框(Basic Login)        layout:
'fit',
基本的登录框(Basic Login)        width:
300,
基本的登录框(Basic Login)        height:
150,
基本的登录框(Basic Login)        closable: 
false,
基本的登录框(Basic Login)        resizable: 
false,
基本的登录框(Basic Login)        plain: 
true,
基本的登录框(Basic Login)        items: [login]
基本的登录框(Basic Login)    }
);
基本的登录框(Basic Login)    win.show();
基本的登录框(Basic Login)}
);
基本的登录框(Basic Login)

基本的登录框(Basic Login) Login.asp

Here is the server processing for your login. I'm going to paste the following overly simplistic code to show the responses that go back, and ultimately determine which function in login.js fires (success or failure). However, this is where you would make the call to the database with the username/password variables, do your authentication, and then send either of these responses depending on whether or not what the user provided a valid set of credentials.


 
 . =  
	. 

	. 
 
 

基本的登录框(Basic Login) Login.php

<?php
 =  ?  : ;
 
 == 
     ;
  
     ;

?>

基本的登录框(Basic Login) Login.cfm



    

    

基本的登录框(Basic Login) Test.asp

You will notice a line in login.js that redirects to Test.asp if a successful login happens. This can obviously be whatever page your main application will be. In my situation, users can have any number of combinations of navigation options, so the next step would be to validate with whatever session management mecahanism you use, that the person accessing test.asp is authenticated to do so. Further, I would pull down whatever navigation options are assigned to that user and build my toolbar accordingly. Since I'm still trying to figure that part out, that will be another tutorial :)

Hopefully this is somewhat helpful and thanks again to crafter for most of the js code.

上一篇:解决jmeter请求不成功或者报403错误


下一篇:服务器网口做trunk问题查证