我的一开始的源码是:
public class BankInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String username = (String)request.getSession().getAttribute("adminusername"); if(username == null){ response.sendRedirect(request.getServletContext().getContextPath()+"/admin/login"); return false; }else return true; }
下面是报错:
方法一:
getServletContext().getContextPath() 没有这个方法当然报错了。
你可以用request.getContextPath();
public class BankInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String username = (String)request.getSession().getAttribute("adminusername"); if(username == null){ response.sendRedirect(request.getServletContext().getContextPath()+"/admin/login"); return false; }else return true; }
方法二:
在一开始的基础上加上.getSession()
public class BankInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String username = (String)request.getSession().getAttribute("adminusername"); if(username == null){ response.sendRedirect(request.getSession().getServletContext().getContextPath()+"/admin/login"); return false; }else return true; }