用户角色

用户、角色、权限

一个用户应该包含多个角色,一个角色中应该有多个用户。用户对角色,角色对权限。

建立持久化类和映射文件

Role.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cn.itcast.shoa.domain.system;

import java.io.Serializable;
import java.util.Set;

public class Role implements Serializable{
private Long rid;
private String name;
private Long pid;//父节点的id
private String description;
private Set<User> users;
private Set<Privilege> privileges;
public Set<Privilege> getPrivileges() {
return privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
this.privileges = privileges;
}
public Long getRid() {
return rid;
}
public void setRid(Long rid) {
this.rid = rid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}

Role.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.shoa.domain.system.Role">
<id name="rid" length="5">
<generator class="increment"></generator>
</id>
<property name="pid" length="5"></property>
<property name="name" length="20"></property>
<property name="description" length="20"></property>

<set name="users" table="user_role">
<key>
<column name="rid"></column>
</key>
<many-to-many class="cn.itcast.shoa.domain.system.User" column="uid"></many-to-many>
</set>
<set name="privileges" table="role_privilege">
<key>
<column name="rid"></column>
</key>
<many-to-many class="cn.itcast.shoa.domain.system.Privilege" column="id"></many-to-many>
</set>
</class>
</hibernate-mapping>

Privilege.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cn.itcast.shoa.domain.system;

import java.io.Serializable;
import java.util.Set;

import org.apache.struts2.json.annotations.JSON;

public class Privilege implements Serializable{
private Long id;//主键
private Long pid;//父节点ID
private String name;//节点的名称
private String flag;//"1"为菜单 "2"为功能

private Boolean checked;

private String url;//跳转到的链接
private String target;//跳转到的frame的名字

private Boolean isParent;
private String icon;//图片的路径
private Set<Role> roles;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getTarget() {
return target;
}

public void setTarget(String target) {
this.target = target;
}

public Boolean getChecked() {
return checked;
}

public void setChecked(Boolean checked) {
this.checked = checked;
}


public Boolean getIsParent() {
return isParent;
}

public void setIsParent(Boolean isParent) {
this.isParent = isParent;
}


public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}


public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getPid() {
return pid;
}

public void setPid(Long pid) {
this.pid = pid;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getFlag() {
return flag;
}

public void setFlag(String flag) {
this.flag = flag;
}

/**
* 该注解的作用就是为了让该对象不传递到前台
* @return
*/
@JSON(serialize=false)
public Set<Role> getRoles() {
return roles;
}

public void setRoles(Set<Role> roles) {
this.roles = roles;
}

}

Privilege.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.shoa.domain.system.Privilege">
<id name="id" length="5">
<generator class="assigned"></generator>
</id>
<property name="pid" length="5"></property>
<property name="icon" length="100"></property>
<property name="isParent"></property>
<property name="name" length="20"></property>
<property name="flag" length="1"></property>

<set name="roles" table="role_privilege">
<key>
<column name="id"></column>
</key>
<many-to-many class="cn.itcast.shoa.domain.system.Role" column="rid"></many-to-many>
</set>
</class>
</hibernate-mapping>

在SessionFactoryTest进行junit测试并生成数据库

通过junit的方式使用PrivilegeTest中的testSavePrivilege_Menuitem方法给Privilege表中添加数据

在PrivilegeTest类中准备权限数据和功能数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package cn.itcast.shoa.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.shoa.domain.system.Privilege;

public class PrivilegeTest extends SpringUtils{
/**
* 插入两个类型的数据
* 1、菜单类型的数据
* 2、功能类型的数据
*/
@Test
public void testSavePrivilege_Menuitem(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

/***********************************************************************************/
/*
* 个人办公
*/
Privilege Privilegeitem1 = new Privilege();
Privilegeitem1.setId(1L);
Privilegeitem1.setIcon("css/images/MenuIcon/FUNC20082.gif");
Privilegeitem1.setName("办公自动化");
Privilegeitem1.setPid(0L);
Privilegeitem1.setFlag("1");
Privilegeitem1.setIsParent(true);

Privilege Privilege2 = new Privilege();
Privilege2.setId(2L);
Privilege2.setIcon("css/images/MenuIcon/FUNC20001.gif");
Privilege2.setName("个人办公");
//Privilege2.setChecked(false);
Privilege2.setPid(1L);
Privilege2.setFlag("1");
Privilege2.setIsParent(true);

Privilege Privilege21 = new Privilege();
Privilege21.setId(21L);
Privilege21.setIcon("css/images/MenuIcon/FUNC20054.gif");
Privilege21.setName("个人考勤");
//Privilege21.setChecked(false);
Privilege21.setPid(2L);
Privilege21.setFlag("1");
Privilege21.setIsParent(false);


Privilege Privilege22 = new Privilege();
Privilege22.setId(22L);
Privilege22.setIcon("css/images/MenuIcon/FUNC23700.gif");
Privilege22.setName("日程安排");
//Privilege22.setChecked(false);
Privilege22.setPid(2L);
Privilege22.setFlag("1");
Privilege22.setIsParent(false);

Privilege Privilege23 = new Privilege();
Privilege23.setId(23L);
Privilege23.setIcon("css/images/MenuIcon/FUNC20069.gif");
Privilege23.setName("工作计划");
//Privilege23.setChecked(false);
Privilege23.setPid(2L);
Privilege23.setFlag("1");
Privilege23.setIsParent(false);

Privilege Privilege24 = new Privilege();
Privilege24.setId(24L);
Privilege24.setIcon("css/images/MenuIcon/FUNC20056.gif");
Privilege24.setName("工作日记");
//Privilege24.setChecked(false);
Privilege24.setPid(2L);
Privilege24.setFlag("1");
Privilege24.setIsParent(false);

Privilege Privilege25 = new Privilege();
Privilege25.setId(25L);
Privilege25.setIcon("css/images/MenuIcon/time_date.gif");
Privilege25.setName("通讯录");
//Privilege25.setChecked(false);
Privilege25.setPid(2L);
Privilege25.setFlag("1");
Privilege25.setIsParent(false);
/*********************************************************************************/
/*
* 审批流转
*/
Privilege Privilege3 = new Privilege();
Privilege3.setId(3L);
///Privilege3.setChecked(false);
Privilege3.setIsParent(true);
Privilege3.setPid(1L);
Privilege3.setName("审批流转");
Privilege3.setFlag("1");
Privilege3.setIcon("css/images/MenuIcon/FUNC20057.gif");

Privilege Privilege31 = new Privilege();
Privilege31.setId(31L);
//Privilege31.setChecked(false);
Privilege31.setIsParent(false);
Privilege31.setPid(3L);
Privilege31.setFlag("1");
Privilege31.setName("审批流程管理");
Privilege31.setIcon("css/images/MenuIcon/manager.gif");

Privilege Privilege32 = new Privilege();
Privilege32.setId(32L);
//Privilege32.setChecked(false);
Privilege32.setIsParent(false);
Privilege32.setPid(3L);
Privilege32.setFlag("1");
Privilege32.setName("表单模板管理");
Privilege32.setIcon("css/images/MenuIcon/formmodel.gif");

Privilege Privilege33 = new Privilege();
Privilege33.setId(33L);
Privilege33.setIsParent(false);
//Privilege33.setChecked(false);
Privilege33.setPid(3L);
Privilege33.setFlag("1");
Privilege33.setName("发起申请");
Privilege33.setIcon("css/images/MenuIcon/FUNC241000.gif");

Privilege Privilege34 = new Privilege();
Privilege34.setId(34L);
Privilege34.setIsParent(false);
//Privilege34.setChecked(false);
Privilege34.setPid(3L);
Privilege34.setFlag("1");
Privilege34.setName("审批申请");
Privilege34.setIcon("css/images/MenuIcon/FUNC20029.gif");

Privilege Privilege35 = new Privilege();
Privilege35.setId(35L);
Privilege35.setIsParent(false);
//Privilege35.setChecked(false);
Privilege35.setPid(3L);
Privilege35.setName("状态查询");
Privilege35.setFlag("1");
Privilege35.setIcon("css/images/MenuIcon/FUNC20029.gif");
/************************************************************************************/
/*
* 知识管理
*/
Privilege Privilege4 = new Privilege();
Privilege4.setId(4L);
Privilege4.setIsParent(false);
//Privilege4.setChecked(false);
Privilege4.setPid(1L);
Privilege4.setFlag("1");
Privilege4.setName("知识管理");
Privilege4.setIcon("css/images/MenuIcon/FUNC20056.gif");
/*******************************************************************************/
/*
* 综合行政
*/
Privilege Privilege5 = new Privilege();
Privilege5.setId(5L);
Privilege5.setIsParent(true);
//Privilege5.setChecked(false);
Privilege5.setPid(1L);
Privilege5.setFlag("1");
Privilege5.setName("管理行政");
Privilege5.setIcon("css/images/MenuIcon/manager.gif");

Privilege Privilege51 = new Privilege();
Privilege51.setId(51L);
Privilege51.setIsParent(false);
//Privilege51.setChecked(false);
Privilege51.setPid(5L);
Privilege51.setName("考勤管理");
Privilege51.setFlag("1");
Privilege51.setIcon("css/images/MenuIcon/FUNC20070.gif");

Privilege Privilege52 = new Privilege();
Privilege52.setId(52L);
Privilege52.setIsParent(false);
//Privilege52.setChecked(false);
Privilege52.setPid(5L);
Privilege52.setFlag("1");
Privilege52.setName("会议管理");
Privilege52.setIcon("css/images/MenuIcon/FUNC20064.gif");

Privilege Privilege53 = new Privilege();
Privilege53.setId(53L);
Privilege53.setIsParent(false);
//Privilege53.setChecked(false);
Privilege53.setPid(5L);
Privilege53.setFlag("1");
Privilege53.setName("车辆管理");
Privilege53.setIcon("css/images/MenuIcon/radio_blue.gif");
/**************************************************************************************/
/*
* 人力资源管理
* 档案管理
* 培训记录
* 奖金记录
* 职位变更
* 人事合同
* 薪酬制度
*/
Privilege Privilege6 = new Privilege();
Privilege6.setId(6L);
Privilege6.setIsParent(true);
//Privilege6.setChecked(false);
Privilege6.setPid(1L);
Privilege6.setFlag("1");
Privilege6.setName("人力资源");
Privilege6.setIcon("css/images/MenuIcon/FUNC20001.gif");

Privilege Privilege61 = new Privilege();
Privilege61.setId(61L);
Privilege61.setIsParent(false);
//Privilege61.setChecked(false);
Privilege61.setPid(6L);
Privilege61.setFlag("1");
Privilege61.setName("档案管理");
Privilege61.setIcon("css/images/MenuIcon/FUNC20076.gif");

Privilege Privilege62 = new Privilege();
Privilege62.setId(62L);
Privilege62.setFlag("1");
Privilege62.setIsParent(false);
//Privilege62.setChecked(false);
Privilege62.setPid(6L);
Privilege62.setName("培训记录");
Privilege62.setIcon("css/images/MenuIcon/FUNC55000.gif");

Privilege Privilege63 = new Privilege();
Privilege63.setId(63L);
Privilege63.setFlag("1");
Privilege63.setIsParent(false);
//Privilege63.setChecked(false);
Privilege63.setPid(6L);
Privilege63.setName("奖赏记录");
Privilege63.setIcon("css/images/MenuIcon/FUNC55000.gif");

Privilege Privilege64 = new Privilege();
Privilege64.setId(64L);
Privilege64.setIsParent(false);
Privilege64.setFlag("1");
//Privilege64.setChecked(false);
Privilege64.setPid(6L);
Privilege64.setName("职位变更");
Privilege64.setIcon("css/images/MenuIcon/FUNC55000.gif");

Privilege Privilege65 = new Privilege();
Privilege65.setId(65L);
Privilege65.setIsParent(false);
//Privilege65.setChecked(false);
Privilege65.setPid(6L);
Privilege65.setFlag("1");
Privilege65.setName("人事合同");
Privilege65.setIcon("css/images/MenuIcon/FUNC55000.gif");

Privilege Privilege66 = new Privilege();
Privilege66.setId(66L);
Privilege66.setIsParent(false);
//Privilege66.setChecked(false);
Privilege66.setPid(6L);
Privilege66.setFlag("1");
Privilege66.setName("薪酬制度");
Privilege66.setIcon("css/images/MenuIcon/FUNC20001.gif");
/*****************************************************************************************/
/*
* 电子邮件
*/
Privilege Privilege7 = new Privilege();
Privilege7.setId(7L);
Privilege7.setIsParent(false);
//Privilege7.setChecked(false);
Privilege7.setPid(1L);
Privilege7.setFlag("1");
Privilege7.setName("电子邮件");
Privilege7.setIcon("css/images/MenuIcon/eml.gif");

/*******************************************************************/
/*
* 实用工具
* 车票预定
* GIS查询
* 邮政编码
*/
Privilege Privilege8 = new Privilege();
Privilege8.setId(8L);
Privilege8.setIsParent(true);
//Privilege8.setChecked(false);
Privilege8.setPid(1L);
Privilege8.setFlag("1");
Privilege8.setName("实用工具");
Privilege8.setIcon("css/images/MenuIcon/FUNC20076.gif");


Privilege Privilege81 = new Privilege();
Privilege81.setId(81L);
Privilege81.setIsParent(false);
//Privilege81.setChecked(false);
Privilege81.setPid(8L);
Privilege81.setFlag("1");
Privilege81.setName("车票预定");
Privilege81.setIcon("css/images/MenuIcon/FUNC220000.gif");


Privilege Privilege82 = new Privilege();
Privilege82.setId(82L);
Privilege82.setIsParent(false);
//Privilege82.setChecked(false);
Privilege82.setPid(8L);
Privilege82.setFlag("1");
Privilege82.setName("GIS查询");
Privilege82.setIcon("css/images/MenuIcon/search.gif");


Privilege Privilege83 = new Privilege();
Privilege83.setId(83L);
Privilege83.setIsParent(false);
//Privilege83.setChecked(false);
Privilege83.setFlag("1");
Privilege83.setPid(8L);
Privilege83.setName("邮政编码");
Privilege83.setIcon("css/images/MenuIcon/FUNC249000.gif");
/**************************************************************************/
/*
* 个人设置
* 个人信息
* 密码修改
*/
Privilege Privilege9 = new Privilege();
Privilege9.setId(9L);
Privilege9.setIsParent(true);
//Privilege9.setChecked(false);
Privilege9.setPid(1L);
Privilege9.setFlag("1");
Privilege9.setName("个人设置");
Privilege9.setIcon("css/images/MenuIcon/FUNC20001.gif");


Privilege Privilege91 = new Privilege();
Privilege91.setId(91L);
Privilege91.setIsParent(false);
//Privilege91.setChecked(false);
Privilege91.setPid(9L);
Privilege91.setFlag("1");
Privilege91.setName("个人信息");
Privilege91.setIcon("css/images/MenuIcon/FUNC20001.gif");


Privilege Privilege92 = new Privilege();
Privilege92.setId(92L);
Privilege92.setIsParent(false);
//Privilege92.setChecked(false);
Privilege92.setFlag("1");
Privilege92.setPid(9L);
Privilege92.setName("密码修改");
Privilege92.setIcon("css/images/MenuIcon/FUNC241000.gif");
/***********************************************************************************/
/*
* 系统管理
* 岗位管理
* 部门管理
* 用户管理
*/
Privilege Privilege10 = new Privilege();
Privilege10.setId(10L);
Privilege10.setIsParent(true);
//Privilege10.setChecked(false);
Privilege10.setPid(1L);
Privilege10.setFlag("1");
Privilege10.setName("系统管理");
Privilege10.setIcon("css/images/MenuIcon/system.gif");


Privilege Privilege101 = new Privilege();
Privilege101.setId(101L);
Privilege101.setIsParent(false);
//Privilege101.setChecked(false);
Privilege101.setPid(10L);
Privilege101.setFlag("1");
Privilege101.setName("岗位管理");
Privilege101.setIcon("css/images/MenuIcon/FUNC20001.gif");


Privilege Privilege102 = new Privilege();
Privilege102.setId(102L);
Privilege102.setIsParent(false);
//Privilege102.setChecked(false);
Privilege102.setPid(10L);
Privilege102.setName("部门管理");
Privilege102.setFlag("1");
Privilege102.setIcon("css/images/MenuIcon/department.gif");


Privilege Privilege103 = new Privilege();
Privilege103.setId(103L);
Privilege103.setIsParent(false);
//Privilege103.setChecked(false);
Privilege103.setPid(10L);
Privilege103.setFlag("1");
Privilege103.setName("用户管理");
Privilege103.setIcon("css/images/MenuIcon/FUNC20001.gif");
/**********************************************************************/
/*
* {
* 1,1
* 2,5
* 3,5
* 4,1
* 5,3
* 6,6
* 7,1
* 8,3
* 9,2
* 10,3
* }
*/

session.save(Privilegeitem1);

session.save(Privilege2);
session.save(Privilege21);
session.save(Privilege22);
session.save(Privilege23);
session.save(Privilege24);
session.save(Privilege25);


session.save(Privilege3);
session.save(Privilege31);
session.save(Privilege32);
session.save(Privilege33);
session.save(Privilege34);
session.save(Privilege35);

session.save(Privilege4);

session.save(Privilege5);
session.save(Privilege51);
session.save(Privilege52);
session.save(Privilege53);

session.save(Privilege6);

session.save(Privilege61);
session.save(Privilege62);
session.save(Privilege63);
session.save(Privilege64);
session.save(Privilege65);
session.save(Privilege66);

session.save(Privilege7);

session.save(Privilege8);
session.save(Privilege81);
session.save(Privilege82);
session.save(Privilege83);

session.save(Privilege9);
session.save(Privilege91);
session.save(Privilege92);

session.save(Privilege10);
session.save(Privilege101);
session.save(Privilege102);
session.save(Privilege103);
transaction.commit();
session.close();
}

/**
* 保存功能
*/
@Test
public void testSavePrivilege_Function(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

Privilege Privilege1 = new Privilege();
Privilege1.setId(201L);
Privilege1.setFlag("2");
Privilege1.setPid(0L);
Privilege1.setIcon("css/images/MenuIcon/system.gif");
Privilege1.setName("用户增加");


Privilege Privilege2 = new Privilege();
Privilege2.setId(202L);
Privilege2.setFlag("2");
Privilege2.setPid(0L);
Privilege2.setIcon("css/images/MenuIcon/system.gif");
Privilege2.setName("用户删除");

Privilege Privilege3 = new Privilege();
Privilege3.setId(203L);
Privilege3.setFlag("2");
Privilege3.setPid(0L);
Privilege3.setIcon("css/images/MenuIcon/system.gif");
Privilege3.setName("用户修改");

Privilege Privilege32 = new Privilege();
Privilege32.setId(210L);
Privilege32.setFlag("2");
Privilege32.setPid(0L);
Privilege32.setIcon("css/images/MenuIcon/system.gif");
Privilege32.setName("用户查询");

Privilege Privilege4 = new Privilege();
Privilege4.setId(204L);
Privilege4.setFlag("2");
Privilege4.setPid(0L);
Privilege4.setIcon("css/images/MenuIcon/system.gif");
Privilege4.setName("部门增加");


Privilege Privilege5 = new Privilege();
Privilege5.setId(205L);
Privilege5.setFlag("2");
Privilege5.setPid(0L);
Privilege5.setIcon("css/images/MenuIcon/system.gif");
Privilege5.setName("部门删除");

Privilege Privilege6 = new Privilege();
Privilege6.setId(206L);
Privilege6.setFlag("2");
Privilege6.setPid(0L);
Privilege6.setIcon("css/images/MenuIcon/system.gif");
Privilege6.setName("部门修改");

Privilege Privilege66 = new Privilege();
Privilege66.setId(211L);
Privilege66.setFlag("2");
Privilege66.setPid(0L);
Privilege66.setIcon("css/images/MenuIcon/system.gif");
Privilege66.setName("部门查询");

Privilege Privilege7 = new Privilege();
Privilege7.setId(207L);
Privilege7.setFlag("2");
Privilege7.setPid(0L);
Privilege7.setIcon("css/images/MenuIcon/system.gif");
Privilege7.setName("岗位增加");


Privilege Privilege8 = new Privilege();
Privilege8.setId(208L);
Privilege8.setFlag("2");
Privilege8.setPid(0L);
Privilege8.setIcon("css/images/MenuIcon/system.gif");
Privilege8.setName("岗位删除");

Privilege Privilege9 = new Privilege();
Privilege9.setId(209L);
Privilege9.setFlag("2");
Privilege9.setPid(0L);
Privilege9.setIcon("css/images/MenuIcon/system.gif");
Privilege9.setName("岗位修改");

Privilege Privilege99 = new Privilege();
Privilege99.setId(212L);
Privilege99.setFlag("2");
Privilege99.setPid(0L);
Privilege99.setIcon("css/images/MenuIcon/system.gif");
Privilege99.setName("岗位查询");

session.save(Privilege1);
session.save(Privilege2);
session.save(Privilege3);
session.save(Privilege32);
session.save(Privilege66);
session.save(Privilege99);
session.save(Privilege4);
session.save(Privilege5);

session.save(Privilege6);
session.save(Privilege7);
session.save(Privilege8);
session.save(Privilege9);

transaction.commit();
session.close();
}
}

准备角色数据使用RoleTest类

RoleTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cn.itcast.shoa.test;

import java.util.HashSet;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;

import cn.itcast.shoa.domain.system.Privilege;
import cn.itcast.shoa.domain.system.Role;
import cn.itcast.shoa.service.RoleService;

public class RoleTest extends SpringUtils{
@Test
public void testSaveRole(){
RoleService roleService = (RoleService)context.getBean("roleService");
SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
Session session = sessionFactory.openSession();
/**
* 用户角色、岗位角色、部门角色
*/
Role role1 = new Role();
role1.setName("用户角色");
role1.setDescription("拥有用户模块的crud的操作");
//建立角色和权限之间的关联
List<Privilege> privileges1 = session.createQuery("from Privilege where id in(38,39,40,41)").list();
//role1.setPrivileges(new HashSet<Privilege>(privileges1));
roleService.saveRole(role1);

Role role2 = new Role();
role2.setName("部门角色");
role2.setDescription("拥有部门模块的crud的操作");
//建立角色和权限之间的关联
List<Privilege> privileges2 = session.createQuery("from Privilege where id in(42,44,45,46)").list();
//role2.setPrivileges(new HashSet<Privilege>(privileges2));
roleService.saveRole(role2);

Role role3 = new Role();
role3.setName("岗位角色");
role3.setDescription("拥有岗位模块的crud的操作");
//建立角色和权限之间的关联
List<Privilege> privileges3 = session.createQuery("from Privilege where id in(43,47,48,49)").list();
//role3.setPrivileges(new HashSet<Privilege>(privileges3));
roleService.saveRole(role3);

}
}

前端页面js设置权限

目标:为了建立用户和角色之间的关系。

功能点:

  1. 把隐藏的div显示出来
  2. 动态的显示用户的名称
  3. 全选复选框按钮的功能

js的架构

抽象:

  1. 在js控制页面的过程中,数据的传递是一个方面的内容

  2. 在js控制页面的过程中,涉及到了事件的触发。

  3. 在点击触发事件时,执行所有页面的控制工作。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    var role = {
    //专门用来存放数据的
    data:{

    },
    init:{
    //初始化数据
    initData:function(){

    },
    //初始化事件
    initEvent:function(){

    }
    },
    //页面的控制操作
    option:{
    //涉及到div的操作
    divOpt:{

    },
    //涉及到用户的操作
    userOpt:{

    },
    //涉及到角色树的操作
    roleTree:{

    }
    }
    };

    点击设置角色的功能

    1. 显示div

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      /**
      * 1、显示div
      */
      role.option.divOpt.showDiv();
      //涉及到div的操作
      divOpt: {
      showDiv: function(){
      /**
      * 把所有的隐藏的div显示出来
      */
      $("div:hidden").show();
      }
      },
    2. 给user中的username和uid赋值

      1
      2
      3
      4
      5
      6
      //初始化数据initData: function(){	
      /** * 要求this为设置角色的超级链接 */
      role.data.user.username = $(this).parent().siblings("td:first").text();
      role.data.user.uid = $(this).parent().siblings("input[type='hidden']").val();
      /*** 2、给user中的username和uid赋值*/
      role.init.initData.call(this);//相当于this调用initData方法
    3. 显示用户名称

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      /**
      * 3、显示用户的名称
      */
      role.option.userOpt.showUsername();
      //涉及到的用户的操作
      userOpt: {
      /**
      * 显示用户名称
      */
      showUsername: function(){
      $("#userImage").text("用户:" + role.data.user.username);
      }
      },

    注意内容

    1. 当点击设置角色,角色树没有显示出来的时候,设置全选复选框为不可用状态。

    2. 当点击设置角色,角色树没有显示出来的时候,在角色树位置应该设置loding.gif,这样交互效果好。

      以上两点都是初始化的状态

    3. 当角色树加载出来后,全选复选框

      设置为可用的状态

      根据角色树上的复选框是否被全部选中,来决定全选复选框的状态,选中还是未选中

    4. 当角色树加载出来后,隐藏loding,显示角色树

    5. 当点击角色树上的某一个复选框的时候,判断

      角色树上复选框是否被全部选中将改变全选复选框的状态

用户角色

base-dao

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cn.itcast.shoa.dao.base;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

public interface BaseDao<E>{
/**
* 得到E代表的所有的实体对象
* @return
*/
public Collection<E> getAllEntry();
/**
* Serializable该类型可以接受所有的基本类型和String类型
* @param id
* @return
*/
public E getEntryById(Serializable id);

public void saveEntry(E e);

public void deleteEntry(Serializable id);

public void updateEntry(E e);

/*
* 根据一个或者一个以上的id,返回一个Set<E>
*/
public Set<E> getEntrysByIDS(Serializable[] ids);

public Set<E> getEntrysByIDS(String ids);

public E getEntryByCondition(final String entityName,final String... objects);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package cn.itcast.shoa.dao.base.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.metadata.ClassMetadata;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.itcast.shoa.dao.base.BaseDao;

public class BaseDaoImpl<E> implements BaseDao<E>{

private Class classt;
/*
* 获取到E代表的持久化类的数据字典-->当前实体bean的标识符名称
*/
private ClassMetadata classMetadata;

public BaseDaoImpl() {
/**
* 1.this可以代表子类或本类
* 2.不能把BaseDaoImpl在spring容器中实例化,因为如果在Spring容器中实例化,就不是泛型了
* 3.所以根据以上两点可以得出this应该代表子类
* 4.this.getClass().getGenericSuperclass()代表当前类:就是泛型
* 5.注意:不能把BaseDaoImpl让spring容器实例化
*/
// TODO Auto-generated constructor stub
ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
//因为将来E代表的是某一个持久化类,而该类型是class
this.classt = (Class)type.getActualTypeArguments()[0];
}

/*
* 当调用完构造方法后,立即执行该init方法
*/
@PostConstruct
private void init() {
this.classMetadata= this.hibernatetemplate.getSessionFactory().getClassMetadata(this.classt);
}

@Resource(name="hibernatetemplate")
public HibernateTemplate hibernatetemplate;

@Override
public Collection<E> getAllEntry() {
// TODO Auto-generated method stub
return this.hibernatetemplate.find( "from "+ this.classt.getName() );
}
@Override
public E getEntryById(Serializable id) {
// TODO Auto-generated method stub
/**
* classMetadata是持久化类的数据字典
*/
return (E)this.hibernatetemplate.
find("from "+this.classt.getName()
+
" where "
+classMetadata.getIdentifierPropertyName()
+"=?",
id).get(0);
}
@Override
public void saveEntry(E e) {
// TODO Auto-generated method stub
this.hibernatetemplate.save(e);
}
@Override
public void deleteEntry(Serializable id) {
// TODO Auto-generated method stub
E e = this.getEntryById(id);
this.hibernatetemplate.delete(e);
}
@Override
public void updateEntry(E e) {
// TODO Auto-generated method stub
this.hibernatetemplate.update(e);
}
@Override
public Set<E> getEntrysByIDS(Serializable[] ids) {
/**
* 1、如果数组只有一个元素
* 2、如果数组中有两个或者两个以上的元素
* from Person where pid in(1,2,3,4)
*/
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("from "+this.classt.getName());//this.classt.getName()持久化类的名字
stringBuffer.append(" where "+this.classMetadata.getIdentifierPropertyName());
stringBuffer.append(" in (");
for(int i=0;i<ids.length;i++){
if(i==ids.length-1){
stringBuffer.append(ids[i]);
}else{
stringBuffer.append(ids[i]+",");
}
}
stringBuffer.append(")");
List<E> list = this.hibernatetemplate.find(stringBuffer.toString());
return new HashSet<E>(list);
}

@Override
public Set<E> getEntrysByIDS(String ids) {
// TODO Auto-generated method stub
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("from "+this.classt.getName());
stringBuffer.append(" where "+this.classMetadata.getIdentifierPropertyName());
stringBuffer.append(" in (");
stringBuffer.append(ids);
stringBuffer.append(")");
List<E> list = this.hibernatetemplate.find(stringBuffer.toString());
return new HashSet<E>(list);
}

@Override
public E getEntryByCondition(final String hql,final String... objects) {
// TODO Auto-generated method stub
return this.hibernatetemplate.execute(new HibernateCallback<E>() {

@Override
public E doInHibernate(Session session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
Query query = session.createQuery(hql);
int index = 0;
for (String s : objects) {
query.setParameter(index, s);
index++;
}
return (E)query.uniqueResult();
}

});
}

}

role-action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cn.itcast.shoa.struts.action;

import java.util.Collection;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.shoa.domain.system.Role;
import cn.itcast.shoa.domain.system.User;
import cn.itcast.shoa.service.RoleService;
import cn.itcast.shoa.service.UserService;
import cn.itcast.shoa.struts.action.base.BaseAction;

@Controller("roleAction")
@Scope("prototype")
public class RoleAction extends BaseAction<Role>{

private Long uid;
private String checkedStr;


public Long getUid() {
return uid;
}

public void setUid(Long uid) {
this.uid = uid;
}

public String getCheckedStr() {
return checkedStr;
}

public void setCheckedStr(String checkedStr) {
this.checkedStr = checkedStr;
}

@Resource(name="roleService")
private RoleService roleService;

@Resource(name="userService")
private UserService userService;

public String showAllRole(){
Collection<Role> roleList = this.roleService.getAllRole();
ActionContext.getContext().put("roleList",roleList);
return listAction;
}

public String addUI(){
return addUI;
}

public String add(){
Role role = new Role();
BeanUtils.copyProperties(this.getModel(), role);
this.roleService.saveRole(role);
return action2action;
}

public String update(){
Role role = this.roleService.getRoleById(this.getModel().getRid());
BeanUtils.copyProperties(this.getModel(), role);
this.roleService.updateRole(role);
return action2action;
}

public String updateUI(){
Role role = this.roleService.getRoleById(this.getModel().getRid());
ActionContext.getContext().getValueStack().push(role);
return updateUI;
}

public String delete(){
this.roleService.deleteRole(this.getModel().getRid());
return action2action;
}

/**
* 加载角色树
* @throws Exception
*/
public String showRoleTree() throws Exception{
Collection<Role> roles = this.roleService.getRolesByUid(uid);
ActionContext.getContext().getValueStack().push(roles);
return SUCCESS;
}

/*
* * 建立用户和角色之间的关系
*/
public String saveRole(){
User user = this.userService.getUserById(this.uid);
Set<Role> roles = this.roleService.getRolesByIds(this.checkedStr);
user.setRoles(roles);
this.userService.updateUser(user);
return SUCCESS;
}
}

role-service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.itcast.shoa.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

import cn.itcast.shoa.domain.system.Role;

public interface RoleService {

public Collection<Role> getAllRole();

public Role getRoleById(Serializable id);

public void saveRole(Role role);

public void deleteRole(Serializable id);

public void updateRole(Role role);

public Set<Role> getRolesByIds(String ids);

public Collection<Role> getRolesByUid(Long uid);

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.itcast.shoa.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.shoa.dao.RoleDao;
import cn.itcast.shoa.domain.system.Role;
import cn.itcast.shoa.service.RoleService;

@Service("roleService")
public class RoleServiceImpl implements RoleService{

@Resource(name="roleDao")
private RoleDao roleDao;

@Override
public Collection<Role> getAllRole() {
// TODO Auto-generated method stub
//int i = 1/0;
//throw new RuntimeException("abc");
return this.roleDao.getAllEntry();
}

@Override
public Role getRoleById(Serializable id) {
// TODO Auto-generated method stub
return this.roleDao.getEntryById(id);
}

@Transactional(readOnly=false)
public void saveRole(Role role) {
// TODO Auto-generated method stub
this.roleDao.saveEntry(role);
}

@Transactional(readOnly=false)
public void deleteRole(Serializable id) {
// TODO Auto-generated method stub
this.roleDao.deleteEntry(id);
}

@Transactional(readOnly=false)
public void updateRole(Role role) {
// TODO Auto-generated method stub
this.roleDao.updateEntry(role);
}

@Override
public Set<Role> getRolesByIds(String ids) {
// TODO Auto-generated method stub
return this.roleDao.getEntrysByIDS(ids);
}

@Override
public Collection<Role> getRolesByUid(Long uid) {
// TODO Auto-generated method stub
return this.roleDao.getRoles(uid);
}

}

role-dao

1
2
3
4
5
6
7
8
9
10
11
package cn.itcast.shoa.dao;

import java.util.Collection;

import cn.itcast.shoa.dao.base.BaseDao;
import cn.itcast.shoa.domain.system.Role;

public interface RoleDao extends BaseDao<Role>{
public Collection<Role> getRoles(Long uid);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.itcast.shoa.dao.impl;

import java.util.Collection;

import org.springframework.stereotype.Repository;

import cn.itcast.shoa.dao.RoleDao;
import cn.itcast.shoa.dao.base.impl.BaseDaoImpl;
import cn.itcast.shoa.domain.system.Role;

@Repository("roleDao")
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{

@Override
public Collection<Role> getRoles(Long uid) {
// TODO Auto-generated method stub
/*
* 1.把所有角色提取出来
* 2.把用户能够访问到的角色提取出来
* 3.遍历所有的角色,遍历用户能够访问的角色,如果正在遍历的所有角色中的某一个正好是用户可以访问的,将checked设为true
*/
Collection<Role> allRoles = this.getAllEntry();
Collection<Role> userRoles = this.hibernatetemplate.find("from Role r inner join fetch r.users u where u.uid=?",uid);
for (Role role : allRoles) {//遍历所有角色
for (Role role2 : userRoles) {//遍历用户可以访问的角色
if(role.getRid().longValue() == role2.getRid().longValue()) {
role.setChecked(true);
break;
}
}
}
return allRoles;
}

}

user-role-js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var role = {
//专门用来存放数据的
data: {
user: {
username: '',
uid: ''
}
},
init: {
//初始化数据
initData: function(){
/**
* 要求this为设置角色的超级链接
*/
role.data.user.username = $(this).parent().siblings("td:first").text();
role.data.user.uid = $(this).parent().siblings("input[type='hidden']").val();
},
//初始化事件
initEvent: function(){
/**
* 给设置角色声明click事件
*/
$("a").each(function(){
if ($(this).text() == "设置角色") {
$(this).unbind("click");
$(this).bind("click", function(){
/**
* 1、显示div
*/
role.option.divOpt.showDiv();
/**
* 2、给user中的username和uid赋值
*/
role.init.initData.call(this);//相当于this调用initData方法
/**
* 3、显示用户的名称
*/
role.option.userOpt.showUsername();

/**
* 经过分析可以得出当角色树没有加载出来的情况下,全选复选框是不能点击的,
* 只有当角色树加载出来的情况下,全选复选框才能点击
*/
/**
* 设置全选复选框的初始化状态值为不可用
*/
role.option.roleTree.changeCheckBoxStatus("disabled");
/**
* 显示loading,隐藏roleTree
*/
role.option.roleTree.changeLoadingAndRoleTree({
roleTree:false
});
/**
* 4、加载角色树
*/
role.option.roleTree.loadRoleTree();
return false;
});
}
});

/**
* 给全选复选框增加change事件
*/
$("#allchecked").unbind("change");
$("#allchecked").bind("change", function(){
role.option.roleTree.allChecked.call(this);
});

/**
* 给保存按钮增加click事件
*/
$("#InputDetailBar img").unbind("click");
$("#InputDetailBar img").bind("click", function(){
role.option.roleTree.saveRole();
});
}
},
//页面的控制操作
option: {
//涉及到div的操作
divOpt: {
showDiv: function(){
/**
* 把所有的隐藏的div显示出来
*/
$("div:hidden").show();
}
},
//涉及到的用户的操作
userOpt: {
/**
* 显示用户名称
*/
showUsername: function(){
$("#userImage").text("用户:" + role.data.user.username);
}
},
//涉及到角色树的操作
roleTree: {
zTreePlugin:'',//接受zTree函数的返回值
setting: {
isSimpleData: true,
treeNodeKey: "rid",
treeNodeParentKey: "pid",
showLine: true,
root: {
isRoot: true,
nodes: []
},
/**
* 显示树上的复选框
*/
checkable:true,
/**
* 给角色树添加事件
*/
callback:{
change:function(){
/**
* 调用setAllChecked方法设置全选复选框的状态
*/
role.option.roleTree.setAllChecked();
}
}
},
/**
* 加载角色树
* 加载角色树的时候,应该对角色树进行回显
* 如果角色树的某一个节点的复选框被选中,实际上就是该节点的属性checked为true
*/
loadRoleTree: function(){
$.post("roleJSONAction_showRoleTree.action", {
uid:role.data.user.uid
}, function(data){
role.option.roleTree.zTreePlugin = $("#roleTree").zTree(role.option.roleTree.setting,data);
/**
* 必须等角色树加载出来以后,才能使全选复选框恢复可用状态
*/
role.option.roleTree.changeCheckBoxStatus("");
/**
* 当整个角色树被加载出来以后,显示角色树,隐藏loading
*/
role.option.roleTree.changeLoadingAndRoleTree({
roleTree:true
});
/**
* 设置全选复选框初始化状态的值
*/
role.option.roleTree.setAllChecked();
});
},
/**
* 变换全选复选框的选中状态
*/
changeCheckBoxStatus:function(status){
$("#allchecked").attr("disabled",status);
},
/**
* 从loading.gif到roleTree的转化
*/
changeLoadingAndRoleTree:function(json){
if(json.roleTree){
$("#roleTree").show();
$("#loading").hide();
}else{
$("#roleTree").hide();
$("#loading").show();
}
},
/**
* 全选复选框的功能
*/
allChecked:function(){
if($(this).attr("checked")){
role.option.roleTree.zTreePlugin.checkAllNodes(true);
}else{
role.option.roleTree.zTreePlugin.checkAllNodes(false);
}
},
/**
* 设置全选复选框被选中的状态
*/
setAllChecked:function(){
var uncheckedNodes = role.option.roleTree.zTreePlugin.getCheckedNodes(false);
if(uncheckedNodes.length==0){//说明全部被选中
$("#allchecked").attr("checked",true);
}else{//没有被全部选中
$("#allchecked").attr("checked",false);
}
},
/**
* 保存
* 建立用户和角色之间的关系
*/
saveRole:function(){
/**
* 1、获取uid和被选中的所有的角色的rid
* rid选中了1,2,3 "1,2,3"
* 2、后台只要对用户进行update操作
*/
//被选中的所有的角色
var checkedNodes = role.option.roleTree.zTreePlugin.getCheckedNodes(true);
var checkedStr = "";
for(var i=0;i<checkedNodes.length;i++){
if(i==checkedNodes.length-1){//最后一个
checkedStr = checkedStr + checkedNodes[i].rid;
}else{
checkedStr = checkedStr + checkedNodes[i].rid+",";
}
}
/**
* ajax端向后台传递的参数
*/
var parameter = {
uid:role.data.user.uid,
checkedStr:checkedStr
};
$.post("roleAction_saveRole.action",parameter,function(data){
alert("操作成功");
});
}
}
}
};

$().ready(function(){
//初始化页面上的事件
role.init.initEvent();
//使所有的隐藏的div显示
// $("div:hidden").show();
});

user下的list.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp"%>
<script language="javascript" src="${pageContext.request.contextPath}/js/user_role.js"></script>
<html>
<head>
<title>用户列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/css/images/title_arrow.gif"/> 用户管理
</div>
<div id="Title_End"></div>
</div>
</div>

<div id="MainArea">
<table cellspacing="0" cellpadding="0" class="TableStyle">

<!-- 表头-->
<thead>
<tr align=center valign=middle id=TableTitle>
<td width="100">姓名</td>
<td width="100">所属部门</td>
<td>所属岗位</td>
<td>相关操作</td>
</tr>
</thead>

<!--显示数据列表-->
<tbody id="TableData" class="dataContainer" datakey="userList">
<s:iterator value="#userList">
<tr class="TableDetail1 template">
<td><s:property value="username"/></td>
<s:hidden name="uid"></s:hidden>
<td><s:property value="department.name"/></td>
<td>
<s:iterator value="roles">
<s:property value="name"/>
</s:iterator>
</td>
<td><a onClick="return delConfirm()" href="list.html">删除</a>
<s:a action="userAction_updateUI.action?uid=%{uid}">修改</s:a>
<s:a>设置角色</s:a>
</td>
</tr>
</s:iterator>
</tbody>
</table>

<div id="TableTail">
<div id="TableTail_inside">
<a href="userAction_addUI.action"><img src="${pageContext.request.contextPath}/css/images/createNew.png" /></a>
</div>
</div>

<div class="ItemBlock_Title1" id="userTitle" style="display: none;"><!-- 信息说明 --><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif"/>
<s:label id="userImage"></s:label>
</div>
<div class="ItemBlock_Title1" id="privilegeTitle" style="display: none;"><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif" />选择角色</div>
</div>

<!-- 表单内容显示 -->
<div class="ItemBlockBorder" style="display: none;" id="privilegeContent">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<!--表头-->
<thead>
<tr align="LEFT" valign="MIDDLE" id="TableTitle">
<td width="300px" style="padding-left: 7px;">
<!-- 如果把全选元素的id指定为selectAll,并且有函数selectAll(),就会有错。因为有一种用法:可以直接用id引用元素 -->
<input type="checkbox" id="allchecked"/>
<label for="cbSelectAll">全选</label>
</td>
</tr>
</thead>

<!--显示数据列表-->
<tbody id="TableData">
<tr class="TableDetail1">
<!-- 显示权限树 -->
<td>
<ul id='roleTree' class="tree"></ul>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<image id="saveRole" src="${pageContext.request.contextPath}/css/images/save.png"/>
</div>
</div>

</body>
</html>

common.jsp添加zTree的引用

1
<%@ taglib prefix="s" uri="/struts-tags" %><link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/blue/pageCommon.css"/><link rel="stylesheet" href="zTreeStyle/zTreeStyle.css" type="text/css"><script language="javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.2.js"></script><script language="javascript" src="${pageContext.request.contextPath}/js/jquery-ztree-2.5.js"></script><script language="javascript" src="${pageContext.request.contextPath}/js/jQuery-plugin-delete.js"></script>

domain

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cn.itcast.shoa.domain.system;

import java.io.Serializable;
import java.util.Set;

public class Role implements Serializable{
private Long rid;
private String name;
private Long pid;//父节点的id
private String description;
private Set<User> users;
private Set<Privilege> privileges;
private Boolean checked;

public Boolean getChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
public Set<Privilege> getPrivileges() {
return privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
this.privileges = privileges;
}
public Long getRid() {
return rid;
}
public void setRid(Long rid) {
this.rid = rid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}



}

struts-role.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- 继承异常处理 -->
<package name="role" namespace="/" extends="struts-global">
<action name="roleAction_*" method="{1}" class="roleAction">
<result name="listAction">/WEB-INF/jsp/system/role/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/system/role/add.jsp</result>
<result name="updateUI">/WEB-INF/jsp/system/role/update.jsp</result>
<result name="action2action" type="redirectAction">roleAction_showAllRole.action</result>
</action>
</package>

<package name="rolejson" namespace="/" extends="json-default">
<action name="roleJSONAction_*" method="{1}" class="roleAction">
<result type="json"></result>
</action>
</package>
</struts>

role-hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.shoa.domain.system.Role">
<id name="rid" length="5">
<generator class="increment"></generator>
</id>
<property name="pid" length="5"></property>
<property name="name" length="20"></property>
<property name="checked"></property>
<property name="description" length="20"></property>

<set name="users" table="user_role">
<key>
<column name="rid"></column>
</key>
<many-to-many class="cn.itcast.shoa.domain.system.User" column="uid"></many-to-many>
</set>
<set name="privileges" table="role_privilege">
<key>
<column name="rid"></column>
</key>
<many-to-many class="cn.itcast.shoa.domain.system.Privilege" column="id"></many-to-many>
</set>
</class>
</hibernate-mapping>

角色权限

privilege.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cn.itcast.shoa.domain.system;

import java.io.Serializable;
import java.util.Set;

import org.apache.struts2.json.annotations.JSON;

public class Privilege implements Serializable{
private Long id;//主键
private Long pid;//父节点ID
private String name;//节点的名称
private String flag;//"1"为菜单 "2"为功能

private Boolean checked;

private String url;//跳转到的链接
private String target;//跳转到的frame的名字

private Boolean isParent;
private String icon;//图片的路径
private Set<Role> roles;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getTarget() {
return target;
}

public void setTarget(String target) {
this.target = target;
}

public Boolean getChecked() {
return checked;
}

public void setChecked(Boolean checked) {
this.checked = checked;
}


public Boolean getIsParent() {
return isParent;
}

public void setIsParent(Boolean isParent) {
this.isParent = isParent;
}


public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}


public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getPid() {
return pid;
}

public void setPid(Long pid) {
this.pid = pid;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getFlag() {
return flag;
}

public void setFlag(String flag) {
this.flag = flag;
}

/**
* 该注解的作用就是为了让该对象不传递到前台
* @return
*/
@JSON(serialize=false)
public Set<Role> getRoles() {
return roles;
}

public void setRoles(Set<Role> roles) {
this.roles = roles;
}

}

privilege.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.shoa.domain.system.Privilege">
<id name="id" length="5">
<generator class="assigned"></generator>
</id>
<property name="pid" length="5"></property>

<property name="isParent"></property>

<property name="url" length="100"></property>

<property name="target" length="20"></property>

<property name="icon" length="100"></property>
<property name="name" length="20"></property>

<property name="flag" length="1"></property>

<property name="checked"></property>


<set name="roles" table="role_privilege">
<key>
<column name="id"></column>
</key>
<many-to-many class="cn.itcast.shoa.domain.system.Role" column="rid"></many-to-many>
</set>
</class>
</hibernate-mapping>

struts-privilege.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="privilegeAction" namespace="/" extends="json-default">
<action name="privilegeAction_*" method="{1}" class="privilegeAction">
<result type="json"></result>
</action>
</package>
</struts>

privilegeAction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package cn.itcast.shoa.struts.action;

import java.util.Collection;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.shoa.domain.system.Privilege;
import cn.itcast.shoa.domain.system.Role;
import cn.itcast.shoa.domain.system.User;
import cn.itcast.shoa.service.PrivilegeService;
import cn.itcast.shoa.service.RoleService;
import cn.itcast.shoa.service.UserService;
import cn.itcast.shoa.struts.action.base.BaseAction;
import cn.itcast.shoa.util.OAUtils;

@Controller("privilegeAction")
@Scope("prototype")
public class PrivilegeAction extends BaseAction<Privilege>{

private Long rid;
private String checkedStr;

public String getCheckedStr() {
return checkedStr;
}

public void setCheckedStr(String checkedStr) {
this.checkedStr = checkedStr;
}

public Long getRid() {
return rid;
}

public void setRid(Long rid) {
this.rid = rid;
}


@Resource(name="privilegeService")
private PrivilegeService privilegeService;

@Resource(name="roleService")
private RoleService roleService;


public String showPrivilegeByRid(){
Collection<Privilege> privileges = this.privilegeService.getPrivilegesByRid(rid);
ActionContext.getContext().getValueStack().push(privileges);
return SUCCESS;
}

public String showMenuitemTreeByUid(){
User user = OAUtils.getUserFromSession();
Collection<Privilege> privileges = this.privilegeService.getPrivilegesByUid(user.getUid(),user.getUsername());

ActionContext.getContext().getValueStack().push(privileges);
return SUCCESS;
}

public String savePrivilege(){
this.privilegeService.savePrivilege(rid, checkedStr);
return SUCCESS;
}

}

privilegeService

1
2
3
4
5
6
7
8
9
10
11
12
13
package cn.itcast.shoa.service;

import java.util.Collection;

import cn.itcast.shoa.domain.system.Privilege;

public interface PrivilegeService {
public Collection<Privilege> getPrivilegesByRid(Long rid);

public void savePrivilege(Long rid,String checkedStr);

public Collection<Privilege> getPrivilegesByUid(Long uid,String username);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cn.itcast.shoa.service.impl;

import java.util.Collection;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.shoa.dao.RoleDao;
import cn.itcast.shoa.dao.privilegeDao;
import cn.itcast.shoa.domain.system.Privilege;
import cn.itcast.shoa.domain.system.Role;
import cn.itcast.shoa.service.PrivilegeService;

@Service("privilegeService")
public class PrivilegeServiceImpl implements PrivilegeService{

@Resource(name="privilegeDao")
private privilegeDao privilegeDao;

@Resource(name="roleDao")
private RoleDao roleDao;
@Override
public Collection<Privilege> getPrivilegesByRid(Long rid) {
// TODO Auto-generated method stub
return this.privilegeDao.getPrivilegesByRid(rid);
}

@Transactional(readOnly=false)
public void savePrivilege(Long rid, String checkedStr) {
// TODO Auto-generated method stub
Role role = this.roleDao.getEntryById(rid);
Set<Privilege> privileges = this.privilegeDao.getEntrysByIDS(checkedStr);
//建立角色和权限之间的关系
role.setPrivileges(privileges);
//更新角色,hibernate内部自动维护角色和权限之间的关系
this.roleDao.updateEntry(role);
}

@Override
public Collection<Privilege> getPrivilegesByUid(Long uid,String username) {
// TODO Auto-generated method stub
return this.privilegeDao.getMenuitemsByUid(uid,username);
}

}

privilegeDao

1
2
3
4
5
6
7
8
9
10
11
12
package cn.itcast.shoa.dao;

import java.util.Collection;

import cn.itcast.shoa.dao.base.BaseDao;
import cn.itcast.shoa.domain.system.Privilege;

public interface PrivilegeDao extends BaseDao<Privilege>{
public Collection<Privilege> getPrivilegesByRid(Long rid);

public Collection<Privilege> getMenuitemsByUid(Long uid,String username);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cn.itcast.shoa.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Repository;

import cn.itcast.shoa.dao.PrivilegeDao;
import cn.itcast.shoa.dao.base.impl.BaseDaoImpl;
import cn.itcast.shoa.domain.system.Privilege;

@Repository("privilegeDao")
public class PrivilegeDaoImpl extends BaseDaoImpl<Privilege> implements PrivilegeDao{

@Override
public Collection<Privilege> getPrivilegesByRid(Long rid) {
Collection<Privilege> allPrivilege = this.getAllEntry();
Collection<Privilege> rolePrivilege = this.hibernateTemplate.find("from Privilege p inner join fetch p.roles r where r.rid=?",rid);
for(Privilege privilege:allPrivilege){
for(Privilege privilege2:rolePrivilege){
if(privilege.getId().longValue()==privilege2.getId().longValue()){
privilege.setChecked(true);
break;
}
}
}
return allPrivilege;
}
//列表去重
@Override
public Collection<Privilege> getMenuitemsByUid(Long uid,String username) {
List<Privilege> privileges = null;
if("admin".equals(username)){
privileges = this.hibernateTemplate.find("from Privilege where flag='1'");
}else{
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("from Privilege p inner join fetch p.roles r inner join fetch r.users u");
stringBuffer.append(" where u.uid=?");
stringBuffer.append(" and flag='1'");
privileges = this.hibernateTemplate.find(stringBuffer.toString(),uid);
}
return new HashSet<Privilege>(privileges);
}
}

role下的list.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp"%>
<script language="javascript" src="${pageContext.request.contextPath}/js/role_privilege.js"></script>
<html>
<head>
<title>岗位列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/css/images/title_arrow.gif"/> 岗位管理
</div>
<div id="Title_End"></div>
</div>
</div>

<div id="MainArea">
<table cellspacing="0" cellpadding="0" class="TableStyle">

<!-- 表头-->
<thead>
<tr align="CENTER" valign="MIDDLE" id="TableTitle">
<td width="200px">岗位名称</td>
<td width="300px">岗位说明</td>
<td>相关操作</td>
</tr>
</thead>

<!--显示数据列表-->
<tbody id="TableData" class="dataContainer" datakey="roleList">
<s:iterator value="#roleList">
<tr class="TableDetail1 template">
<td><s:property value="name"/></td>
<s:hidden name="rid"></s:hidden>
<td><s:property value="description"/></td>
<td>
<s:a action="roleAction_delete.action?rid=%{rid}">删除</s:a>
<s:a action="roleAction_updateUI.action?rid=%{rid}">修改</s:a>
<s:a>设置权限</s:a>
</td>
</tr>
</s:iterator>
</tbody>
</table>

<!-- 其他功能超链接 -->
<div id="TableTail">
<div id="TableTail_inside">
<a href="roleAction_addUI.action"><img src="${pageContext.request.contextPath}/css/images/createNew.png" /></a>
</div>
</div>

<div class="ItemBlock_Title1" id="userTitle" style="display: none;"><!-- 信息说明 --><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif"/>
<s:label id="roleImage"></s:label>
</div>
<div class="ItemBlock_Title1" id="privilegeTitle" style="display: none;"><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif" />选择权限</div>
</div>

<!-- 表单内容显示 -->
<div class="ItemBlockBorder" style="display: none;" id="privilegeContent">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<!--表头-->
<thead>
<tr align="LEFT" valign="MIDDLE" id="TableTitle">
<td width="300px" style="padding-left: 7px;">
<!-- 如果把全选元素的id指定为selectAll,并且有函数selectAll(),就会有错。因为有一种用法:可以直接用id引用元素 -->
<input type="checkbox" id="allchecked"/>
<label for="cbSelectAll">全选</label>
</td>
</tr>
</thead>

<!--显示数据列表-->
<tbody id="TableData">
<tr class="TableDetail1">
<!-- 显示权限树 -->
<td>
<ul id='privilegeTree' class="tree"></ul>

</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<image id="savePrivilege" src="${pageContext.request.contextPath}/css/images/save.png"/>
</div>
</div>
</body>
</html>

role-privilege.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var privilege = {
data:{
role:{
rid:'',
name:''
}
},
init:{
/**
* 初始化数据
*/
initData:function(){
privilege.data.role.name = $(this).parent().siblings("td:first").text();
privilege.data.role.rid = $(this).parent().siblings("input[type='hidden']").val();
},
/**
* 初始化事件
*/
initEvent:function(){
/**
* 给设置权限添加click事件
*/
$("a").each(function(){
if($(this).text()=="设置权限"){
$(this).unbind("click");
$(this).bind("click",function(){
//1、显示div
privilege.option.divOpt.showDiv();
//2、初始化数据
privilege.init.initData.call(this);
//3、动态显示角色的名称
privilege.option.roleOpt.showRoleName();
//设置全选复选框初始化的状态值
privilege.option.privilegeTree.isAllChecked(false);
//4、加载权限树
privilege.option.privilegeTree.loadPrivilegeTree();
return false;
});
}
});

/**
* 给全选复选框添加change事件
*/
$("#allchecked").unbind("change");
$("#allchecked").bind("change",function(){
privilege.option.privilegeTree.allChecked.call(this);
});

/**
* 给保存(建立角色和权限之间的关系)添加click事件
*/
$("#savePrivilege").unbind("click");
$("#savePrivilege").bind("click",function(){
privilege.option.privilegeTree.savePrivilege();
});
}
},
option:{
roleOpt:{
showRoleName:function(){
$("#roleImage").text("角色:"+privilege.data.role.name);
}
},
divOpt:{
showDiv:function(){
$("div:hidden").show();
}
},
privilegeTree:{
zTreeplugin:'',
setting: {
isSimpleData: true,
treeNodeKey: "id",
treeNodeParentKey: "pid",
showLine: true,
root: {
isRoot: true,
nodes: []
},
checkable:true
},
loadPrivilegeTree:function(){
$.post("privilegeAction_showPrivilegeByRid.action",{
rid:privilege.data.role.rid
},function(data){
privilege.option.privilegeTree.zTreeplugin = $("#privilegeTree").zTree(privilege.option.privilegeTree.setting,data);
//设置全选复选框的初始化的默认值
privilege.option.privilegeTree.setAllCheckedValue();
});
},
/**
* 设置全选复选框的状态
*/
isAllChecked:function(checked){
$("#allchecked").attr("disabled",checked);
},
/**
*设置全选复选框的默认值
*/
setAllCheckedValue:function(){
var uncheckedNodes = privilege.option.privilegeTree.zTreeplugin.getCheckedNodes(false);
if (uncheckedNodes.length == 0) {//全部被选中
$("#allchecked").attr("checked", true);
}else{
$("#allchecked").attr("checked", false);
}
},
/**
* 全选复选框的功能
*/
allChecked:function(){
if($(this).attr("checked")){//全选复选框被选中
privilege.option.privilegeTree.zTreeplugin.checkAllNodes(true);
}else{
privilege.option.privilegeTree.zTreeplugin.checkAllNodes(false);
}
},
/**
* 保存
*/
savePrivilege:function(){
var checkedNodes = privilege.option.privilegeTree.zTreeplugin.getCheckedNodes(true);
var checkedStr = "";
for(var i=0;i<checkedNodes.length;i++){
if(i==checkedNodes.length-1){
checkedStr = checkedStr + checkedNodes[i].id;
}else{
checkedStr = checkedStr + checkedNodes[i].id+",";
}
}
var parameter = {
rid:privilege.data.role.rid,
checkedStr:checkedStr
};
$.post("privilegeAction_savePrivilege.action",parameter,function(data){
alert("操作成功");
});
}
}
}
};

$().ready(function(){
//初始化事件
privilege.init.initEvent();
});