struts2实现用户登录的简单权限控制文档格式.docx
《struts2实现用户登录的简单权限控制文档格式.docx》由会员分享,可在线阅读,更多相关《struts2实现用户登录的简单权限控制文档格式.docx(19页珍藏版)》请在冰豆网上搜索。
23.idvarchar2(20)primarykey,
24.ridvarchar2(20),
25.rnamevarchar2(20)
26.);
27.
28.createsequencesystem.roler_sequence
29.startwith1
30.incrementby1
31.nomaxvalue
32.cache20;
33.
34.createtriggersystem.roler_trigger
35.beforeinsert
36.onsystem.roler
37.foreachrow
38.when(new.idisnull)
39.begin
40.selectsystem.roler_sequence.nextvalinto:
new.idfromdual;
41.end;
--创建权限表
createtablesystem.purview
(
pvaluenumber(3)primarykey,
pnamevarchar2(20),
pmodulevarchar2(20)
insertintosystem.purviewvalues(1,'
insertintosystem.purviewvalues(2,'
insertintosystem.purviewvalues(3,'
insertintosystem.purviewvalues(4,'
insertintosystem.purviewvalues(5,'
insertintosystem.purviewvalues(6,'
insertintosystem.purviewvalues(7,'
insertintosystem.purviewvalues(8,'
--创建角色表
createtablesystem.roler
idvarchar2(20)primarykey,
ridvarchar2(20),
rnamevarchar2(20)
createsequencesystem.roler_sequence
startwith1
incrementby1
nomaxvalue
cache20;
createtriggersystem.roler_trigger
beforeinsert
onsystem.roler
foreachrow
when(new.idisnull)
begin
selectsystem.roler_sequence.nextvalinto:
end;
1)roler表
2)purview表
二:
添加权限
1)添加角色jsp
1.<
%@pagelanguage="
java"
import="
java.util.*"
pageEncoding="
GBK"
%>
2.<
%@pageisELIgnored="
false"
%>
3.<
%@tagliburi="
/struts-tags"
prefix="
s"
4.
5.<
!
DOCTYPEHTMLPUBLIC"
-//W3C//DTDHTML4.01Transitional//EN"
>
6.<
html>
7.<
head>
9.<
/head>
10.
11.<
body>
12.
13.<
formaction="
rolerInsertDo.action"
method="
post"
14.角色名<
inputtype="
text"
name="
rname"
/>
15.<
BR>
<
/BR>
权限
16.<
tableborder="
1"
17.<
tr>
td>
新闻<
/td>
td>
18.<
19.<
20.<
s:
iteratorvalue="
purviewList"
var="
pur"
21.
22.<
iftest="
${pur.pmodule=='
}"
23.<
checkbox"
purview"
value="
${pur.pvalue}"
${pur.pname}<
24.<
/s:
if>
25.
26.<
iterator>
27.<
/tr>
28.<
/table>
29.
30.<
31.
32.<
公告<
33.<
34.<
35.<
36.
37.<
38.<
39.<
40.
41.<
42.<
43.<
44.
45.<
46.
47.<
48.<
submit"
value="
提交"
49.<
/form>
50.<
/body>
51.
52.<
/html>
<
角色名<
权限
2)添加角色action
1.publicStringinsertDo(){
2.//获得角色名称和权限值
3.intp[]=roler.getPurview();
4.//调用service层
5.rolerService=newInsertRolerService();
6.intflag=rolerService.insert(roler);
7.returnSUCCESS;
8.}
publicStringinsertDo(){
//获得角色名称和权限值
intp[]=roler.getPurview();
//调用service层
rolerService=newInsertRolerService();
intflag=rolerService.insert(roler);
returnSUCCESS;
}
3)添加角色service
1.publicintinsert(InsertRolerBeanroler){
2.
3.//开始连接数据库
4.dbcon=newDBConnection();
5.sta=dbcon.getSta();
6.//取出所有选中的权限值,生成最终的权限值
7.longsum=0;
8.for(inti=0;
i<
roler.getPurview().length;
i++){
9.
10.sum+=Math.pow(2,roler.getPurview()[i]);
//getPurview是事先声明好的get和set方法,此数组用来接收前台所选的权限,Math.pow此发放为权限算法
11.}
12.sql="
insertintoroler(rid,rname)values("
+sum+"
"
+roler.getRname()+"
'
)"
;
14.try{
15.flag=sta.executeUpdate(sql);
16.
17.}catch(SQLExceptione){
18.//TODOAuto-generatedcatchblock
19.e.printStackTrace();
20.}
21.returnflag;
22.}
publicintinsert(InsertRolerBeanroler){
//开始连接数据库
dbcon=newDBConnection();
sta=dbcon.getSta();
//取出所有选中的权限值,生成最终的权限值
longsum=0;
for(inti=0;
i++){
sum+=Math.pow(2,roler.getPurview()[i]);
}
sql="
try{
flag=sta.executeUpdate(sql);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnflag;
三:
根据用户查看此用户所具有的权限
1)查看用户所具有的权限页面
12.<
checkRoler"
13.用户名:
14.<
登录"
17.
用户名:
<
2)查询用户所具有的权限action
1.publicStringgetByName(){
2.Stringname=roler.getRname();
3.//System.out.println("
name-------------->
+name);
4.purviewService=newRolerService();
5.roler=purviewService.getByRname(name);
6.longrid=roler.getRid();
7.System.out.println("
rid------------->
+rid);
10.purviewList=purviewService.getAll();
11.
12.lis=purviewService.getRoler(purviewList,rid);
14.
15.lis=purviewService.getQX(lis);
17.return"
success"
18.}
publicStringgetByName(){
Stringname=roler.getRname();
//System.out.println("
purviewService=newRolerService();
roler=purviewService.getByRname(name);
longrid=roler.getRid();
System.out.println("
purviewList=purviewService.getAll();
lis=purviewService.getRoler(purviewList,rid);
lis=purviewService.getQX(lis);
return"
3)查询用户所具有的权限service
a).getByRname方法,用来根据所登用户的名称得到rid字段
1.publicInsertRolerBeangetByRname(Stringrname){
2.dbcon=newDBConnection();
3.sta=dbcon.getSta();
5.sql="
selectid,rid,rnamefromrolertwheret.rname='
+rname+"
6.
7.try{
8.rs=sta.executeQuery(sql);
9.while(rs.next()){
10.rb=newInsertRolerBean();
11.rb.setId(rs.getInt("
id"
));
12.rb.setRid(rs.getLong("
rid"
13.rb.setRname(rs.getString("
15.
16.}
22.returnrb;
23.
24.}
publicInsertRolerBeangetByRname(Stringrname){
rs=sta.executeQuery(sql);
while(rs.next()){
rb=newInsertRolerBean();
rb.setId(rs.getInt("
rb.setRid(rs.getLong("
rb.setRname(rs.getString("
}
returnrb;
b).getAll()方法用来得到所有权限名称
1.publicListgetAll(){
2.purviewList=newArrayList();
6.sql="
selectpvalue,pname,pmodulefrompurview"
7.
8.try{
9.rs=sta.executeQuery(sql);
10.//循环打包
11.while(rs.next()){
12.RolerBeanpur=newRolerBean();
13.pur.setPvalue(rs.getInt("
pvalue"
14.pur.setPname(rs.getString("
pname"
15.pur.setPmodule(rs.getString("
pmodule"
16.purviewList.add(pur);
17.}
18.}catch(SQLExceptione){
19.//TODOAuto-generatedcatchblock
20.e.printStackTrace();
21.}finally{
22.dbcon.close();
25.returnpurviewList;
26.}
publicListgetAll(){
purviewList=newArrayList();