jquery easy ui 1.5最新版本 簡單的許可權分配

來源:互聯網
上載者:User

標籤:多模組   dem   串連   管理   user   path   nes   登入   ini   

jquery easy 1.5 Maven+servlet+jsp+jdbc實現許可權管理分配

                 首先,我先說明下,我只是一個菜鳥,我只是在編程道路上摸索的一個的小碼農。做開發三年了,這好像是我第一次寫部落格。以前接觸過類似於jquery easy  ui 的DWZ,是我第一家公司用的UI架構。所以用easy ui感覺很快能上手。都是基於jquery 的ui架構。好,廢話少說。此次項目我是用最基礎的jsp+servlet+jdbc寫的,同時加入了maven管理jar包以及發布。應該很好理解的.

              

1.準備工作

       先從官網下載原始碼:點擊開啟連結,

      下載完成後看如下目錄項:locale myself 和src都是些組件的js,easy ui把這些js都封裝好了,所以我們不需要考慮這些。

     

    接著我們需要搭建項目環境,我的項目用到了maven,關於maven環境搭建大家可以在網上找點資料看下,在此就不多加贅述了。貼下pom.xml配置具體

<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><!-- 加入mysql驅動依賴包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.27</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><!--引入json包 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.1</version></dependency><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.8.3</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.1</version></dependency><dependency><groupId>net.sf.ezmorph</groupId><artifactId>ezmorph</artifactId><version>1.0.6</version></dependency> <dependency>           <groupId>net.sf.json-lib</groupId>            <artifactId>json-lib</artifactId>             <version>2.4</version>             </dependency>

<build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>2.2</version><configuration><version>3.0</version><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version><configuration><server>tomcat7</server><port>8000</port><url>http://localhost:8000/manager/text</url><!--連接埠號碼 --><uriEncoding>UTF-8</uriEncoding><path>/zycweb</path><username>admin</username><password>admin</password></configuration></plugin></plugins></build>

一切都準備好後,首先考慮我們 許可權管理架構的邏輯,使用者登入以後先查詢該使用者擁有的角色,再查詢該角色下擁有的菜單模組。同時管理員可以分配角色擁有哪些模組,以及所謂的增刪查改。這個是我們考慮需要做的,資料庫採用的是mysql。表結構如下,由於只是簡單的許可權控制模組,沒有考慮增刪查改這些許可權控制操作(以後如果有時間再加上吧),如果資料庫只用到了五張表。。


        2.登入擷取許可權菜單

         使用者名稱登入擷取哪些菜單呢?首先獲得使用者角色下擁有的菜單資訊。easy ui 返回給用戶端響應的都是json格式資料,所以我們登入成功進入到首頁面要是json格式,這樣才能給我們想要的結果。那登入後的介面是啥樣呢?首先從下載好的easyui demo 檔案夾裡找到layout布局檔案夾這個組件中,找到一個demo,你會看到它們是怎麼引入js和css的。所以嘛,照搬,從full.html引入它的js css

把該引入的css js都引入到本項目中。我是把項目中所有用到的js css都引入到一個公用頁面中了taglib.jsp中。好了應該也出來了。layout組件分上下左右中五塊地區,首先我們就要考慮west地區,因為這塊地區是樹菜單。這塊剛開始是犯難了,到底是一下子全部載入出來樹呢,還是點擊一下載入出來呢(也就是非同步載入,這樣應該很容易理解吧,哈哈),考慮到有的項目可能很多模組,回應時間不能過長,還是選擇非同步載入。非同步載入需要考慮先載入一級菜單和二級菜單,然後二級菜單下有沒有三級菜單,需要做個判斷,如果有返回的json格式必須有個狀態state:closed.具體伺服器端關鍵代碼如下:(由於剛開始寫沒有用json第三方包都是手寫拼的所以是這樣的)

<span style="font-size:12px;"> if (newModelList != null && newModelList.size() > 0) {// 使用者權限是否擁有菜單                        for (int i = 0; i < newModelList.size(); i++) {                            long roleModelId = newModelList.get(i).getModelId();                            for (int j = 0; j < listModelByParentId.size(); j++) {// 查詢菜單是否擁有此角色菜單,作比較                                TModel tModel = listModelByParentId.get(j);                                if (tModel.getId() == roleModelId) {                                    List<TModel> listSecond = modelDao.getRoleModelByParentId(tModel.getId());                                    jsonStr.append("{\"id\":" + tModel.getId() + ",\"text\":\"" + tModel.getName() + "\",");                                    if (listSecond != null && listSecond.size() > 0) {                                        jsonStr.append("\"state\":\"closed\"");                                    } else {                                        jsonStr.append("\"state\":\"open\"");                                    }                                    if (tModel.getUrl() != null && !"".equals(tModel.getUrl())) {                                        jsonStr.append(",\"url\":\"" + tModel.getUrl() + "\"");                                    } else {                                        // jsonStr.append("\"url\":\"servlet/LoginServlet\"");                                    }                                    jsonStr.append("},");                                }                            }                        }                    }                    if (jsonStr.toString().endsWith(",")) {                        String newJson = jsonStr.toString().substring(0, jsonStr.toString().length() - 1);                        jsonStr.replace(0, jsonStr.toString().length(), newJson);                    }                    if (parentId != null && parentId.equals("0")) {                        jsonStr.append("]}]");                    } else {                        jsonStr.append("]");                    }</span>
整取返回的菜單格式應該如

伺服器已經給用戶端發送json格式資料了,那麼前端該怎麼樣實現非同步載入呢?具體代碼如下:

<span style="font-size:12px;">   $('#tree').tree({      url:'${zyc}/servlet/TreeServlet?parentId=0' ,      animate:true,      checkbox:false,      lines:true,      onClick: function(node){           if(node.url!="" && typeof(node.url)!='undefined'){           addtab(node.text,"${zyc}/"+node.url);              }             },          onBeforeExpand:function(node,param){                         $('#tree').tree('options').url = "../servlet/TreeServlet?parentId=" + node.id;  //動態擷取節點                 }       });</span>

沒錯,用戶端就是這樣簡單幾行代碼就實現了非同步載入樹,是不是很適合我們這些菜鳥玩玩呢?


此處特別說明(onBeforeExpand函數是關鍵意思是指載入樹展開之前會觸發這個函數再自動追加節點)


3.增刪查改

     增刪查改也就是點擊菜單,獲得相應的連結地址,這個我就說明下載入的js tab吧,center地區加上一個初始的tabs頁面如下代碼:

 <div class="easyui-tabs" id="tabs" fit="true" border="false"><div title="歡迎頁面" style="padding:20px;overflow:hidden;"> <div style="margin-top:20px;"><h3></h3></div></div>     </div>
樹菜單點擊的時候載入tabs函數

   function addtab(text,url){         if ($("#tabs").tabs('exists', text)) {   //若選項卡已存在,選擇該選項卡                  $("#tabs").tabs('select', text);            } else {                  var content = "<iframe frameborder='0' scrolling='auto' style='width:100%;height:100%' src=" + url + "></iframe>";                     $("#tabs").tabs('add', {   //產生新的選項卡,                      title : text,                      closable : true,                      content :content,                      tools:[{                iconCls:'icon-mini-refresh',                handler:function(){                //window.location.reload("${zyc}/"+url);                 var tab = $('#tabs').tabs('getSelected'); //獲得當前tab                 var url = $(tab.panel('options').content).attr('src');                 $('#tabs').tabs('update', {                           tab : tab,                           options : {                           href : url                           }                     });                                                           }                    }]                });              }         }       
增刪查改我就不寫了,網上很多例子的。如下:



4模組管理

    模組管理也就是增刪查改之類的,

     

     樹菜單其實還是west地區的那個串連地址,其實這塊應該是全部的model,但是我圖方便沒改,意義是一樣的。只不過這個樹是全部載入出來的,具體的關鍵代碼如下:

  

var node = $('#tree').tree('getSelected');if (node) {$('#tree').tree('expandAll', node.target);} else {$('#tree').tree('expandAll');}
意思是獲得改樹每個子節點,如果沒展開就展開。修改的comboxtree同理在此就不代碼了





5.角色管理

    角色管理也就分配許可權複雜點其它也就是增刪查改。

  

此處用的treeggrid組件,說下邏輯吧json格式具體參照demo裡的格式,然後返回到用戶端,屬性分別是id,name(這兩項是不允許修改的是必選項),所以返回的屬性必須有這兩個屬性,其次多加了了ischecked屬性,因為多了一列checkbox。具體代碼我就不copy了(感覺寫了好多就不多寫了!請見諒)



其實感覺裡面東西好多,但是無非就是那些用到的組件,只要看看demo,再網上找點資料應該還是能解決掉的。由於本人技術能力有限,就只能寫這樣子了,望大家見解,不喜勿噴!可以互相交流下。謝謝!



jquery easy ui 1.5最新版本 簡單的許可權分配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.