ITPub博客

首页 > 应用开发 > Java > struts2.0拦截器(实现未登录的用户不能访问系统的页面)

struts2.0拦截器(实现未登录的用户不能访问系统的页面)

Java 作者:dawn009 时间:2014-06-12 16:38:09 0 删除 编辑

1.要在struts.xml文件中添加下面的代码:


    <!--自定义拦截器(没有登录的就返回到login)-->
       
           
           

       

       
            /sessionValid.jsp
       

还要在具体action的跳转配置中添加下面代码:


            /orderSearch.jsp
           
              
       

如果不加上面蓝色的部分,页面域中的值就不能带到下个页面,因为定义了自己的拦截器,系统传值的拦截器就失效了,所以加上这条系统默认的拦截器就生效了

2.下面是SessionNullInterceptor的拦截器具体代码:

package com.hoperun.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/** 
* session为空拦截器 
*/
public class SessionNullInterceptor implements Interceptor {
private static final long serialVersionUID = 1L;

public void destroy() {
}

public void init() {
}

public String intercept(ActionInvocation invocation) throws Exception {
   HttpServletRequest req = ServletActionContext.getRequest();
   if (req.getSession().getAttribute("username") == null) {
    return Action.LOGIN;
   } else {
    return invocation.invoke();
   }
}

}

3,对了还不能忘记在login.acton中添加如下代码:

if(result==true)
        {
        Map map = ActionContext.getContext().getSession();            
            map.put("username",username);         
            return SUCCESS;
        }

这样如果你没有充login.jsp登录进来而直接去访问系统的其他页面时,就不能查看你想看的页面,而是会自动跳转到sessionValid.jsp页面去

----->>转载于:http://hi.baidu.com/wei_yuhong/item/7dd7774221d04fd6c0a59225

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29119536/viewspace-1181527/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-08-15

  • 博文量
    595
  • 访问量
    4989247