Searching...
Wednesday, 10 July 2013

Spring MVC Interceptor Example

08:21

Spring MVC Interceptor

HandlerInterceptor

Workflow interface that allows for customized handler execution chains. Applications can register any  number of existing or custom interceptors for certain groups of handlers, to add common preprocessing behavior without needing to modify each handler implementation.

See More about interceptor

Software Used

1. Eclipse Juno

2. Tomcat 7

3. Spring 3.0.3 

Example

Create one Web application and follow below steps

1. Create an interceptor class which implements HandlerInterceptor

package com.pretech; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; 
public class HelloIntercepter implements HandlerInterceptor  {
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        System.out.println("Executing preHandle method");
        return true;
    } 
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        System.out.println("Executing postHandle method");
    }
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("Executing afterCompletion method");
    } 
} 

2. Create a Controller class

package com.pretech; 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; 
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        return new ModelAndView("hello", "message", "Hello World.. Spring 3");
    } 
} 

3. Create a JSP page 

<html>
<head>
<title>Pretech</title>
</head>
<body>
    <h1>Helloworld Example</h1>
<hr/>
    <form action="hello">
         <input type="submit" value="Click here to Say Helloworld">
    </form>
${message}
</body>
</html>


4. Create spring config file (spring-context.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" />
        <property name="cacheSeconds" value="3000" />
    </bean>
    <context:component-scan base-package="com.pretech" />
    <mvc:annotation-driven />
<mvc:interceptors>
      <bean class="com.pretech.HelloIntercepter" />
    </mvc:interceptors> 
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean> 
</beans> 

5. Add Spring Dispatcher servlet details in to web.xml

<context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/spring-context.xml</param-value>
   </context-param>
   <servlet>
       <servlet-name>springDispatcher</servlet-name>
       <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
       </servlet-class>
     <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/spring-context.xml</param-value>
       </init-param>        
       <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
       <servlet-name>springDispatcher</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>

5. Run the application on Tomcat Server


http://localhost:8080/Spring3HelloWorld/hello 




image 


Download this example Spring Interceptor Example

0 comments: