问题描述
拦截器:拦截没有session的访问publicStringintercept(ActionInvocationinvocation)throwsException{HttpServletRequestrequest=(HttpServletRequest)ServletActionContext.getRequest();HttpSessionsession=(HttpSession)request.getSession();//Mapsession=invocation.getInvocationContext().getSession();System.out.println("开始判定");Stringpath=request.getServletPath();System.out.println("managerINFO是否为空"+(session.getAttribute("managerInfo")!=null));/**通过session中是否有managerInfo来拦截访问**/if(session.getAttribute("managerInfo")!=null){returninvocation.invoke();}/***对没有session值的,对URI进行拦截*/return(list!=null&&list.contains(path))?invocation.invoke():Action.LOGIN;}
action如下:如果有帐号密码,则还回SUCCESSpublicStringlogin(){if(StringUtils.isBlank(manager.getManagername())||StringUtils.isBlank(manager.getPassword())){returnLOGIN;}//查找登录名字,如果数据库有记录,则设置一个session值if(managerService.login(manager)){request.getSession().setAttribute("managerInfo",manager);System.out.println("Action中的managerINFO是否为空"+(request.getSession().getAttribute("managerInfo")!=null));returnSUCCESS;}returnLOGIN;}
下面是struts2的配置<!--管理员操作--><packagename="admin"namespace="/admin"extends="struts-default"><!--拦截器--><interceptors><interceptorname="adminLogin"class="adminLoginInterceptor"/><interceptor-stackname="admindefault"><interceptor-refname="adminLogin"/><interceptor-refname="defaultStack"/></interceptor-stack></interceptors><default-interceptor-refname="admindefault"/><actionname="admin_*"class="managerAction"method="{1}"><resultname="login"type="redirect">index.jsp</result><resultname="success">/WEB-INF/page/admin/index.jsp</result><resultname="top">/WEB-INF/page/admin/top.jsp</result><resultname="left">/WEB-INF/page/admin/left.jsp</result><resultname="list">/WEB-INF/page/admin/memberlist.jsp</result></action></package>
下面是结果:问题:拦截器中得不到session
解决方案
解决方案二:
顶起来,查遍网络。google没法用,找不到答案,只好来这求答案了
解决方案三:
没看出来,你用的什么拦截器
解决方案四:
Struts2拦截session用Filter例:packagecom.utils;importjava.io.IOException;importjava.util.Map;importjavax.annotation.Resource;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importorg.apache.log4j.Logger;importorg.apache.struts2.ServletActionContext;importorg.springframework.web.context.WebApplicationContext;importorg.springframework.web.context.support.WebApplicationContextUtils;importcom.cheater.init.service.InitService;importcom.cheater.init.service.InitServiceImpl;importcom.cheater.model.User;/****@Date:2013-12-13下午4:59:27*@Author:Gyh*@TODO:系统权限过滤器(未登录用户不允许访问某些功能)*/publicclassPrivilegeFilterimplementsFilter{WebApplicationContextcontext;privatestaticLoggerlogger=Logger.getLogger(LogAspect.class);@Overridepublicvoiddestroy(){}@OverridepublicvoiddoFilter(ServletRequestarg0,ServletResponsearg1,FilterChainarg2)throwsIOException,ServletException{doBeforeFilter(arg0);//判断是否保存有登录信息try{HttpServletRequestrequest=(HttpServletRequest)arg0;HttpServletResponseresponse=(HttpServletResponse)arg1;Useruser=(User)request.getSession().getAttribute(Constants.USER);Stringaction=request.getServletPath();//action名System.out.println("调用的action:"+action);if(null!=request.getQueryString())System.out.println("传递的参数:"+request.getQueryString());//Stringparams=request.getQueryString();//后面的参数Map<String,String>acceptedAction=Constants.ACCEPTED_ACTION;if(null==user&&acceptedAction.containsKey(action)){response.sendRedirect(request.getContextPath()+"/init_login.action");}else{arg2.doFilter(request,response);}}catch(Exceptione){e.printStackTrace();}}privatevoiddoBeforeFilter(ServletRequestrequest){HttpSessionsession=((HttpServletRequest)request).getSession(true);//首先检查session,若已经登陆则直接忽略以下代码if(session.getAttribute(Constants.USER)!=null){return;}Cookie[]cookies=((HttpServletRequest)request).getCookies();Stringemail=null;Stringpassword=null;if(cookies!=null){for(Cookiec:cookies){if(c.getName().equals("email")){email=c.getValue();}if(c.getName().equals("pass")){password=c.getValue();}}context=WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());//获取springbeanInitServiceinitService=(InitService)context.getBean("initService");Useruser=initService.getUser(email,password);if(user!=null){logger.info(user.getEmail()+"通过COOKIE登录..");session=((HttpServletRequest)request).getSession(true);session.setAttribute(Constants.USER,user);}}}@Overridepublicvoidinit(FilterConfigarg0)throwsServletException{}}
解决方案五:
我用Filter写了一个登录过滤器,session还是获取不了。然后我又检查了其他的session使用,都可以,唯独在ACTION里面的session设置后,在其他任何地方都无法获取session。
解决方案六:
一个过滤写这么麻烦直接用javax.servlet.Filter;这个接口
解决方案七:
我放弃了struts2了