Followers

springMVC : Multiple Interceptor

index.jsp
======

<a href="hello.htm">Click here for greeting message</a>

web.xml
======

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>



dispatcher-servlet.xml
==============

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

   
    <context:component-scan base-package="myspring" />
    <bean id="loggerInterceptor" class="myspring.LoggerInterceptor" ></bean>
    <bean id="secondloggerInterceptor" class="myspring.SecondLoggerInterceptor" ></bean>
     
   
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
        <property name="interceptors" >
            <list>
                <ref bean="loggerInterceptor"></ref>
                <ref bean="secondloggerInterceptor"></ref>
               
            </list>
        </property>
    </bean>
   
</beans>


MyController.java
===========

package myspring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {
    @RequestMapping("/hello.htm")
    public ModelAndView sayGreetings(){
        String msg = "Hi, This is my second Interceptor Spring MVC application";
        System.out.println("I am in MyController.");
    return new ModelAndView("/WEB-INF/jsp/hello.jsp","message",msg);
    }
   
}


LoggerInterceptor.java
==============

package myspring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class LoggerInterceptor extends HandlerInterceptorAdapter{
    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handle) throws Exception{
   
        System.out.println("In first preHandle Method....");
    return true;
    }
    @Override
    public void postHandle(HttpServletRequest request,HttpServletResponse response,Object handle,ModelAndView modelAndView) throws Exception{
   
       
        System.out.println("In first post Handle method...");
    }
    @Override
    public void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handle,Exception ex) throws Exception{
        System.out.println("In first afterCompletion method...");
   
}
}

SecondLoggerInterceptor.java
===================

package myspring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class SecondLoggerInterceptor implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest hsr, HttpServletResponse hsr1, Object o) throws Exception {
        System.out.println("I am in second preHandle");
return true;       
    }

    @Override
    public void postHandle(HttpServletRequest hsr, HttpServletResponse hsr1, Object o, ModelAndView mav) throws Exception {
        System.out.println("I am in second postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest hsr, HttpServletResponse hsr1, Object o, Exception excptn) throws Exception {
       System.out.println("I am in second afterCompletion");
    }
}



hello.jsp
======

${message}

<%

System.out.println("In View....");

%>

No comments:

Post a Comment