Struts2拦截器权限验证(源码)!

 长话短说,抛砖引玉,举个很简单的例子,通过Session,验证用户是否已登陆。

环境:MyEclipse6.5+Mysql5+struts2.0.11.2

源码:拦截器类:

AuthInterceptor.java

 

ContractedBlock.gif Code

 

 

验证用户登陆后的Action类,负责向Session中插入值。

AdminAction.java

 

复制代码
package  com.sy.action;

import  java.util.List;
import  java.util.Map;

import  com.opensymphony.xwork2.ActionContext;
import  com.opensymphony.xwork2.ActionSupport;
import  com.sy.dao.AdminDao;
import  com.sy.dao.NewsDao;
import  com.sy.dao.impl.AdminDaoImpl;
import  com.sy.dao.impl.NewsDaoImpl;
import  com.sy.vo.Admin;
import  com.sy.vo.News;

public   class  AdminAction  extends  ActionSupport {

    
private   static   final   long  serialVersionUID  =   - 3699334709726488611L ;
    
int  i = 1 ; // 中间变量
     private   int  k; // 储存最大页面数
     private   int  pageNow = 1 // 页码数,初始为1
     private   int  pageSize  =   8  ;  // 页面行数 
     private   int  intRowCount; // 总行数
     private   int  intPageCount; // 总页数
     private  List < News >  Newss;
    
private  Admin admin;
    
public  List < News >  getNewss() {
        
return  Newss;
    }
    
public   void  setNewss(List < News >  newss) {
        Newss 
=  newss;
    }
    
public  Admin getAdmin() {
        
return  admin;
    }
    
public   void  setAdmin(Admin admin) {
        
this .admin  =  admin;
    }
    
public   int  getPageNow() {
        
return  pageNow;
    }
    
public   void  setPageNow( int  pageNow) {
        
this .pageNow  =  pageNow;
    }
    
public   int  getPageSize() {
        
return  pageSize;
    }
    
public   void  setPageSize( int  pageSize) {
        
this .pageSize  =  pageSize;
    }
    
public   int  getK() {
        
return  k;
    }
    
public   void  setK( int  k) {
        
this .k  =  k;
    }
    
public   int  getIntRowCount() {
        
return  intRowCount;
    }
    
public   void  setIntRowCount( int  intRowCount) {
        
this .intRowCount  =  intRowCount;
    }
    
public   int  getIntPageCount() {
        
return  intPageCount;
    }
    
public   void  setIntPageCount( int  intPageCount) {
        
this .intPageCount  =  intPageCount;
    }
    @SuppressWarnings(
" unchecked " )
    
public  String execute()  throws  Exception {
        
        AdminDao adi
= new  AdminDaoImpl();
        admin.getAname();
        admin.getApassword();
        
if (adi.isLogin(admin)){
            
            Map map
= ActionContext.getContext().getSession(); // 插入Session的值
            map.put( " user " , admin.getAname());
            
            NewsDao npage
= new  NewsDaoImpl();
            intRowCount
= npage.count();
            k
= (intRowCount  +  pageSize  -   1 /  pageSize;
            intPageCount 
=  (intRowCount  +  pageSize  -   1 /  pageSize; // 计算出总页数
             if (pageNow < 1 ){
                pageNow
= 1 ;
            }
            
            
if (pageNow  >  intPageCount)
                 pageNow
= intPageCount;
                 i 
=  (pageNow  - 1 ) * pageSize;
            NewsDao nlist
= new  NewsDaoImpl();
            
if ( null != nlist.queryByPage(i,pageSize)){
            Newss 
=  nlist.queryByPage(i,pageSize);
            
            
return  SUCCESS;
            }
else {
                
return   " failure " ;
            }
                }
else
                    
return   " failure " ;
        }
    }
复制代码

 

 struts.xml

 

复制代码
<? 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 = " News "   extends = " struts-default " >

    
< interceptors >

    
< interceptor - stack name = " myStack " >
                
< interceptor - ref name = " defaultStack " ></ interceptor - ref >
                
< interceptor - ref name = " auth " ></ interceptor - ref >
    
</ interceptor - stack >
    
    
< interceptor name = " auth "   class = " com.sy.interceptor.AuthInterceptor " />
    
</ interceptors >

    
< global - results >
        
< result name = " login "  type = " redirect " >/ admin / login.jsp </ result >
    
</ global - results >
<!--  验证管理员登陆  -->
        
< action name = " login "   class = " com.sy.action.AdminAction " >
            
< result name = " failure " >/ admin / failure.jsp </ result >
            
< result >/ admin / ManageNews.jsp </ result >
            
< result name = " input " >/ admin / login.jsp </ result >
        
</ action >
<!--  管理员列表  -->         
        
< action name = " alist "   class = " com.sy.action.ListAction "  method = " adminList " >
            
< result >/ admin / deleteManager.jsp </ result >
            
< result name = " failure " >/ admin / Showfailure.jsp </ result >
            
< interceptor - ref name = " myStack " ></ interceptor - ref >
        
</ action >
    
</ package >
</ struts >
复制代码

 

 配置完成!!!


本文转自施杨博客园博客,原文链接:http://www.cnblogs.com/shiyangxt/archive/2008/10/24/1318673.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值