判断页面是否登陆或过期 [转]

public abstract class MyBasePage extends BasePage implements
  PageValidateListener {

/*
  * 判断session是否清空,如果为空则跳到提示页面
  * @see org.apache.tapestry.event.PageValidateListener#pageValidate(org.apache.tapestry.event.PageEvent)
  */
public void pageValidate(PageEvent event) {
  Visit visit = (Visit) getVisit();

  if (visit != null)
   return;
  throw new PageRedirectException("SessionTimeOut";
}
}

然后自己所写的类继承MyBasePage,而不是BasePage,不过记住注销方法里面需要设置visit=null


import javax.servlet.http.HttpSession;

import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.engine.BaseEngine;

public class MyEngine extends BaseEngine {
private static org.apache.log4j.Logger logger = org.apache.log4j.LogManager
.getLogger(MyEngine.class.getName());

private static final long serialVersionUID = -628778836971125385L;

private transient boolean killSession; //是否要进行session注销操作,默认为false

// public String getStaleSessionPageName() {
// return "Home";
// }

/** 下面的方法用于注销用户 */

protected void cleanupAfterRequest(IRequestCycle cycle) {

super.cleanupAfterRequest(cycle);
if (killSession) {
try {
HttpSession session = cycle.getRequestContext().getSession();
if (session != null) {
session.invalidate();
logger.debug("注销用户";
}
} catch (IllegalStateException ex) {

}
}
}

public void logout() {
killSession = true;
Visit visit = (Visit) this.getVisit();
if (visit != null) {
visit.setAppKey(null);
visit.setBnetid(null);
visit.setCalledUserIdList(null);
visit.setIsadmin(false);
visit.setCalledUserIdList(null);
visit.setIsSet(false);
visit.setRole(0);
visit.setRulePriority(null);
visit.setSeluserid(null);
visit.setTupttosave(null);
visit.setUserEmail(null);
visit.setUserid(null);
visit.setUsername(null);
}

}

}

你可能感兴趣的:(apache,log4j,servlet,tapestry)