package com.highcom.literature.util.interceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.highcom.literature.LTBContextNotExistException;
import com.highcom.literature.LTBackContext;
import javax.servlet.http.*;
/**
* Title: SignonInterceptor.java
*
* Description:
*
* Copyright: Copyright (c) 2005
*
* Company: HighCom
*
* @author Xinbo Li
*
* @version 1.0
*
* 创建时间:2005-6-29 11:10:16
*/
public class SignonInterceptor extends HandlerInterceptorAdapter {
/**
* 构造函数
*/
public SignonInterceptor() {
}
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws
Exception {
try {
LTBackContext ctx = LTBackContext.getLTBackContext(request);
}
catch (LTBContextNotExistException ex) {
String url = request.getServletPath();
String query = request.getQueryString();
ModelAndView modelAndView = new ModelAndView("/sys_manage/need_login");
if (query != null) {
modelAndView.addObject("signonForwardAction", url + "?" + query);
}
else {
modelAndView.addObject("signonForwardAction", url);
}
throw new ModelAndViewDefiningException(modelAndView);
}
return true;
}
}
对应的xml配置
<bean id="sysHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref local="signonInterceptor"/>
</list>
</property>
<property name="urlMap">
<map>
<entry key="/index_sys.do">
<ref local="sysWorkAction" />
</entry>
</map>
</property>
</bean>
<bean id="signonInterceptor"
class="com.highcom.literature.util.interceptor.SignonInterceptor">
<description>登陆拦截器</description>
</bean>
boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
这个方法在自己的应用方法执行之前,返回true的时候继续执行操作,如果false,说明没有通过,那么自己的方法也就不会执行。
void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception;
这个方法在自己的应用方法执行之后,在DispatcherServlet类导向到view之前执行。可以往view中添加额外的model对象。
void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception;
一个Callback方法,请求过程(request processing)完成之后执行。可以用来清理资源。