使用拦截器来拦截请求handlerInterceptor

来源:互联网 发布:linux添加管理员权限 编辑:程序博客网 时间:2024/04/20 14:51
这个其实蛮简单的。。
就是自己写的拦截器继承
HandlerInterceptorAdapter
 这个类是实现了HandlerInterceptor接口的
有三个方法preHandle(..)
postHandle(..)执行时
afterCompletion(..)
如下例子::::【定义自己的类】
package samples;public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {    private int openingTime;    private int closingTime;    public void setOpeningTime(int openingTime) {        this.openingTime = openingTime;    }    public void setClosingTime(int closingTime) {        this.closingTime = closingTime;    }    public boolean preHandle(            HttpServletRequest request,            HttpServletResponse response,            Object handler) throws Exception {        Calendar cal = Calendar.getInstance();        int hour = cal.get(HOUR_OF_DAY);        if (openingTime <= hour && hour < closingTime) {            return true;        } else {            response.sendRedirect("http://host.com/outsideOfficeHours.html");            return false;        }    }}

配置xml bean:
<beans>    <bean id="handlerMapping"          class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">        <property name="interceptors">            <list>                <ref bean="officeHoursInterceptor"/>            </list>        </property>    </bean>    <bean id="officeHoursInterceptor"          class="samples.TimeBasedAccessInterceptor">        <property name="openingTime" value="9"/>        <property name="closingTime" value="18"/>    </bean><beans>

----------------但是我不知道为什么一直不知道拦截之后怎么处理?
原创粉丝点击