1、問題的出現:當用戶進入主頁的時候,我需要判斷是來着移動端還是pc端,於是我用了攔截器,攔截所有請求;
2、操作:1)mvc配置文件<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.thinkgem.jeesite.modules.sys.interceptor.MobileInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
2)攔截器中的三個方法:紅色注釋部分是錯誤代碼,錯誤原因
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
if (modelAndView != null){
// 如果是手機或平板訪問的話,則跳轉到手機視圖頁面。
/*if(IsFromPcOrPhone.JudgeIsMoblie(request)){//手機端
request.getRequestDispatcher("/h5qingnianbang/index.jsp").forward(request, response);
}else{//pc
request.getRequestDispatcher("/qingnianbang/index.jsp").forward(request, response);
}*/
if(UserAgentUtils.isMobileOrTablet(request) && !StringUtils.startsWithIgnoreCase(modelAndView.getViewName(), "redirect:")){
modelAndView.setViewName("h5qingnianbang/index" );
}else{
modelAndView.setViewName("qingnianbang/index" );
}
}
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
}
3、問題原因:
preHandle():這個方法在業務處理器處理請求之前被調用,在該方法中對用戶請求request進行處理。如果程序員決定該攔截器對請求進行攔截處理后還要調用其他的攔截器,或者是業務處理器去進行處理,則返回true;如果程序員決定不需要再調用其他的組件去處理請求,則返回false。
postHandle():這個方法在業務處理器處理完請求后,但是DispatcherServlet向客戶端返回請求前被調用,在該方法中對用戶請求request進行處理。
afterCompletion():這個方法在DispatcherServlet完全處理完請求后被調用,可以在該方法中進行一些資源清理的操作。
4、原因分析:紅色錯誤的代碼邏輯是訪問jsp頁面,這樣會被再一次攔截,進入攔截器,又再次的重定向,再被攔截,如此循環,但是沒有一種循環下去,不知道為何,所有導致錯誤的出現。