Extjs4---用MVC做的後臺管理系統 之三

貌似有點小bug,有時登錄成功後不能自動跳轉到首頁,需要刷新一下,把struts.xml配置中配置爲<result type="redirect">就沒問題了,希望有高手能告訴哪寫的不對,暫時只能用這種方式解決了

完整代碼參考:http://www.luchg.com/resource/showResource_5.html

如果發現代碼有什麼問題歡迎提出

這次主要是實現了登錄功能以及登錄驗證,到此後臺管理系統的框架已經搭建差不多了,剩下的工作就是對數據的操作了

又不清楚的可以結合前幾篇文章,這篇是對前面幾個功能的整合:

登錄功能:http://blog.csdn.net/lc448986375/article/details/8025305

自定義攔截器進行登錄驗證:http://blog.csdn.net/lc448986375/article/details/8027432

後臺管理系統之二:http://blog.csdn.net/lc448986375/article/details/8019731


首先,在後臺管理系統之二的版本上加了登錄功能,可以參考http://blog.csdn.net/lc448986375/article/details/8025305

需要修改的是登錄成功後頁面的跳轉:

buttons:[
						         {
						        	 text:'登錄',
						        	 width:80,
						        	 height:30,
						        	 handler:function(){
						        		 //獲取當前的表單form
						        		 var form = this.up('form').getForm();
						        		 //判斷否通過了表單驗證,如果不能空的爲空則不能提交
						        		 if(form.isValid()){
						        			 //alert("可以提交");
						        			 form.submit(
						        					 {
						        						 clientValidation:true,
						        						 waitMsg:'請稍候',
						        						 waitTitle:'正在驗證登錄',
						        						 url:'user_login',
						        						 success:function(form1,action){
						        							 //登錄成功後的操作,跳轉到toIndex.action
						        							 window.location.href = 'toIndex' 
						        						 },
						        						 failure:function(form,action){
						        							 Ext.MessageBox.show({
						                                         width:150,
						                                         title:"登錄失敗",
						                                         buttons: Ext.MessageBox.OK,
						                                         msg:action.result.msg
						                                     })
						        						 }
						        					 		
						        					 }
						        			 )
						        		 }
						        	 }
						         },
						         {
						        	 text:'取消',
						        	 width:80,
						        	 height:30,
						        	 handler:function(){
						        		 //點擊取消,關閉登錄窗口
						        		 var form = this.up('form');
						        		 form.close();
						        	 }
						         }
						]

其他的並沒有改變,登錄成功後跳轉到LoginAction.java的toIndex.action:

package action;


import java.io.ByteArrayInputStream;
import java.util.Map;


import model.Admin;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport implements SessionAware {
	//接收name,必須與js中的textfield的name相同,否則取不到值
	private String name;
	private String password;
	//得到頁面傳來的驗證碼
	private String CheckCode;
	private Map session;
	
	//用於告訴前臺時候登錄成功
	private boolean success;
	
	public String login(){
		//得到生成的驗證碼
		String strCode = (String) session.get("randomCode");
System.out.println("UserAction>randomCode:"+strCode);
		if("admin".equals(name) && "admin".equals(password) && CheckCode.toLowerCase().equals(strCode.toLowerCase())){
			//做個小例子,沒有連接數據庫
			session.put("nowUser", new Admin(1,"admin","admin",1));
			success = true;
		}else{
			success = false;
		}
		return SUCCESS;
	}
	
	public String toIndex() {
		return SUCCESS;
	}

配置文件struts.xml:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

	<package name="admin" namespace="/" extends="json-default">
	
    	<interceptors>
    		<interceptor name="loginInterceptor" class="interceptor.LoginInterceptor"></interceptor>
    	</interceptors>
		
		<global-results>
    		<result name="toLogin">/login.html</result>
    	</global-results>
		
		<action name="user_login" class="action.LoginAction" method="login">
			<result type="json" />
		</action>
		<action name="toIndex" class="action.LoginAction" method="toIndex">
			<interceptor-ref name="loginInterceptor"></interceptor-ref>
			<result>/index.html</result>
		</action>
			
		<action name="getCode" class="action.YanZhengMaAction" method="getCode">
			<result type="stream">
				<param name="contentType">image/jpeg</param>
				<param name="inputName">bais</param>
              	<param name="bufferSize">2048</param>
			</result>
		</action>
	</package>

    <package name="json" extends="json-default">
    	<interceptors>
    		<interceptor name="loginInterceptor" class="interceptor.LoginInterceptor"></interceptor>
    	</interceptors>
    	<global-results>
    		<result name="toLogin">/login.html</result>
    	</global-results>
    	
        <action name="users" class="action.UserAction" method="users">
        	<interceptor-ref name="loginInterceptor"></interceptor-ref>
            <result type="json" />
        </action>
    </package>
</struts>

跳轉到:index.html中,然後就是後臺管理系統之二:http://blog.csdn.net/lc448986375/article/details/8019731的內容了

接下來需要對用戶登錄進行驗證,雖然後臺管理只有一個頁面,但是可以在地址欄中直接輸入請求進行操作,所以我們需要對每個用戶的請求進行驗證,所以在每個action中都要配置自定義的攔截器,我不知道有沒有更好的方式,如果有知道的希望能交流一下


爲了防止用戶直接進入index.html頁面,我把這個頁面放入了WEB-INF下面,如果有更好的方法也希望能提出建議,學習一下


下面是效果圖:

登錄頁面:


登錄成功後跳轉到後臺首頁:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章