JSP和Struts解决用户退出问题
时间:2007-10-22 来源:不详 作者:林子
response.setHeader(Cache-Control,no-store);
//Directs caches not to store the page under any circumstance
response.setDateHeader(Expires, 0); //Causes the proxy cachetosee the page as stale
response.setHeader(Pragma,no-cache); //HTTP 1.0backwardcompatibility
if (!this.userIsLoggedIn(request))
ActionErrors errors = new ActionErrors();
errors.add(error, new ActionError(logon.sessionEnded));
this.saveErrors(request, errors);
return mapping.findForward(sessionEnded);
return executeAction(mapping, form, request,response); protectedabstract ActionForwardexecuteAction(ActionMapping mapping,ActionForm form,HttpServletRequest request, HttpServletResponseresponse) throwsIOException, ServletException; privatebooleanuserIsLoggedIn(HttpServletRequest request)
if (request.getSession().getAttribute(User) == null)
return false;
return true;
清单6中的代码与清单4中的很相像,仅仅只是用ActionMappingfindForward替代了RequestDispatcherforward。清单6中,如果在HttpSession中未找到username字符串,ActionMapping对象将找到名为sessionEnded的forward元素并跳转到对应的path。如果找到了,子类将执行其实现了executeAction()方法的业务逻辑。因此,在配置文件struts-web.xml中为所有子类声明个一名为sessionEnded的forward元素是必须的。清单7以secure1action阐明了这样一个声明:
copyright dedecms
清单7
继承自BaseAction类的子类Secure1Action实现了executeAction()方法而不是覆盖它。Secure1Action类不执行任何退出代码,如清单8:publicclassSecure1Action extends BaseAction publicActionForwardexecuteAction(ActionMapping mapping,ActionFormform,HttpServletRequest request,HttpServletResponseresponse) throws IOException,ServletException
HttpSession session = request.getSession();
return (mapping.findForward(success));
只需要定义一个基类而不需要额外的代码工作,上述解决方案是优雅而有效的。不管怎样,将通用的行为方法写成一个继承StrutsAction的基类是许多Struts项目的共同经验,值得推荐。
上一篇:JSP与XML的结合 下一篇:Taglib 原理和实现之嵌套和属性读取
文章评论
共有位Admini5网友发表了评论 查看完整内容