标签:拦截器
首先在SpringMvc.xml中加入自己定义的拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/manager/**"/>//拦截改路径的url请求
<bean id="mLoginInterceptor" class="com.usermanage.util.MLoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
2.任何访问我定义拦截的url都会返回登录页面
package com.usermanage.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.usermanage.entity.Tmanager;
public class MLoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURL().toString();
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
if (request.getSession().getAttribute("mloginUser") == null){
System.out.println(request.getSession().getAttribute("loginUser"));
response.sendRedirect(basePath+"index.jsp");
return false;
}else{
System.out.println("sdsjkd");
return true;
}
//request.getRequestDispatcher("redirect:toIndexHome").forward(request, response);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
super.postHandle(request, response, handler, modelAndView);
}
}
标签:拦截器
原文地址:http://hatch.blog.51cto.com/9349645/1678324