codestory

Die Anleitung zu Spring MVC Interceptor

  1. Was ist Spring Interceptor?
  2. Maven Project erstellen
  3. Maven und web.xml erklären
  4. Spring MVC konfigurieren
  5. Interceptor classes
  6. Controllers
  7. Die Applikation laufen

1. Was ist Spring Interceptor?

Wenn Sie in einen Unternehmen kommen und den Direktor treffen möchten, müssen Sie durch die Interceptor durchgehen. Die Inceptor hier kann die Beschützer und die Rezeption ... sein

Im Spring wenn eine Anforderung (request) zum Kontroller geschickt wird, wird sie durch die Interceptor (0 oder viel) durchgehen bevor sie vom Controller behandelt wird.
Spring Interceptor ist ein Begriffe, der ziemlich gleich wie Servlet Filter.

Spring Interceptor wendet nur daran, wenn die Anforderungen zu einem Controller geschickt werden.
Sie können Interceptor zum Aufgabedurchführung benutzen, z.B Log aufschreiben,die Konfiguration einzufügen oder aktuellisieren bevor die Anforderung durch Controller behandelt wird ...
Eine der Spring MVC Applikation, die Interceptor benutzt, ist "Die Multisprache-Applikationswebsite". Sie können die Applikation hier sehen
Die Klasse Interceptor soll die Interface org.springframework.web.servlet.HandlerInterceptor durchführen oder aus die Klasse org.springframework.web.servlet.handler.HandlerInterceptorAdapter verlängern.
Sie sollen 3 Abstrakt-Methode implemenitieren
  • public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
  • public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView)
  • public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
Beachten Sie: Die Methode preHandle gibt true oder false rück. Wenn es true ist, d.h die Anforderung geht weiter zu ihrem Zweck (ein Controller) .
Eine Anforderung kann durch viele Interceptor durchgehen. Das folgende Beispiel bezeichnet das.

2. Maven Project erstellen

  • File/New/Other..
  • Group Id: org.o7planning
  • Artifact Id: SpringMVCInterceptor
  • Package: org.o7planning.springmvcinterceptor
Das Projekt wurde erstellt.
Stellen Sie sicher, Sie benutzen Java >=6.
Project Properties:

3. Maven und web.xml erklären

Servlet API >= 3 benutzen.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
   
    <display-name>SpringMVCInterceptor</display-name>
   
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">


<modelVersion>4.0.0</modelVersion>
<groupId>org.o7planning</groupId>
<artifactId>SpringMVCInterceptor</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCInterceptor Maven Webapp</name>
<url>http://maven.apache.org</url>


  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
      </dependency>


      <!-- Servlet API -->
      <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
      </dependency>


      <!-- Jstl for jsp page -->
      <!-- http://mvnrepository.com/artifact/javax.servlet/jstl -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>


      <!-- JSP API -->
      <!-- http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
      <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.2</version>
          <scope>provided</scope>
      </dependency>



      <!-- Spring dependencies -->
      <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.3.3.RELEASE</version>
      </dependency>

      <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.3.3.RELEASE</version>
      </dependency>

      <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.3.3.RELEASE</version>
      </dependency>


  </dependencies>
 
 
  <build>
      <finalName>SpringMVCInterceptor</finalName>
      <plugins>

          <!-- Config: Maven Tomcat Plugin -->
          <!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
          <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.2</version>
            
              <!-- Config: contextPath and Port (Default - /SpringMVCInterceptor : 8080) -->
            
              <!--
              <configuration>
                  <path>/</path>
                  <port>8899</port>
              </configuration>
              -->
          </plugin>
      </plugins>
  </build>
 
</project>

4. Spring MVC konfigurieren

SpringWebAppInitializer.java
package org.o7planning.springmvcinterceptor.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class SpringWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ApplicationContextConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher",
                new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}
ApplicationContextConfig.java
package org.o7planning.springmvcinterceptor.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration

@ComponentScan("org.o7planning.springmvcinterceptor.*")
public class ApplicationContextConfig {

    @Bean(name = "viewResolver")
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }
 
}
Die Interceptor werden mit der Applikation durch die Klasse WebMvcConfig registrieren.
WebMvcConfig.java
package org.o7planning.springmvcinterceptor.config;

import org.o7planning.springmvcinterceptor.interceptor.AdminInterceptor;
import org.o7planning.springmvcinterceptor.interceptor.LogInterceptor;
import org.o7planning.springmvcinterceptor.interceptor.OldLoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    // Static Resource Config
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // LogInterceptor apply to all URLs.
        registry.addInterceptor(new LogInterceptor());

        // This URL is old, It no longer use.
        // Using OldURLInterceptor to redirecto to new URL.
        registry.addInterceptor(new OldLoginInterceptor())//
                .addPathPatterns("/admin/oldLogin");

        // This interceptor apply to URL like /admin/*
        // Exclude /admin/oldLogin
 
        registry.addInterceptor(new AdminInterceptor())//
                .addPathPatterns("/admin/*")//
                .excludePathPatterns("/admin/oldLogin");
    }

}

5. Interceptor classes

LogInterceptor wird für alle Anforderungen, die zu einem Controller gehen. angewendet. (Sehen Sie die Konfiguration in WebMvcConfig).
LogInterceptor.java
package org.o7planning.springmvcinterceptor.interceptor;

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 LogInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        long startTime = System.currentTimeMillis();
        System.out.println("\n-------- LogInterception.preHandle --- ");
        System.out.println("Request URL: " + request.getRequestURL());
        System.out.println("Start Time: " + System.currentTimeMillis());

        request.setAttribute("startTime", startTime);
         
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("\n-------- LogInterception.postHandle --- ");
        System.out.println("Request URL: " + request.getRequestURL());

        // You can add attributes in the modelAndView
        // and use that in the view page
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("\n-------- LogInterception.afterCompletion --- ");

        long startTime = (Long) request.getAttribute("startTime");
        long endTime = System.currentTimeMillis();
        System.out.println("Request URL: " + request.getRequestURL());
        System.out.println("End Time: " + endTime);

        System.out.println("Time Taken: " + (endTime - startTime));
    }

}
OldLoginInterceptor ist ein Interceptor. Wenn der Benutzer den Pfad /admin/oldLogin typt, leitet er zu dem neuen Pfad /admin/login um.
OldLoginInterceptor.java
package org.o7planning.springmvcinterceptor.interceptor;

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 OldLoginInterceptor extends HandlerInterceptorAdapter {

   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
           throws Exception {

       System.out.println("\n-------- OldLoginInterceptor.preHandle --- ");
       System.out.println("Request URL: " + request.getRequestURL());
       System.out.println("Sorry! This URL is no longer used, Redirect to /admin/login");
 
       response.sendRedirect(request.getContextPath()+"/admin/login");
       return false;
   }

   @Override
   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
           ModelAndView modelAndView) throws Exception {
     
       // This code will never be run.
       System.out.println("\n-------- OldLoginInterceptor.postHandle --- ");
   }

   @Override
   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
           throws Exception {
       // This code will never be run.
       System.out.println("\n-------- QueryStringInterceptor.afterCompletion --- ");
   }

}
AdminInterceptor.java
package org.o7planning.springmvcinterceptor.interceptor;

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 AdminInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        System.out.println("\n-------- AdminInterceptor.preHandle --- ");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("\n-------- AdminInterceptor.postHandle --- ");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("\n-------- AdminInterceptor.afterCompletion --- ");
    }

}

6. Controllers

MainController.java
package org.o7planning.springmvcinterceptor.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {

   @RequestMapping(value = { "/", "/test" })
   public String test(Model model) {

       System.out.println("\n-------- MainController.test --- ");

       System.out.println(" ** You are in Controller ** ");

       return "test";
   }

   // This path is no longer used.
   // It will be redirected by OldLoginInterceptor or
   @Deprecated
   @RequestMapping(value = { "/admin/oldLogin" })
   public String oldLogin(Model model) {
       // Code here never run.
       return "oldLogin";
   }

   @RequestMapping(value = { "/admin/login" })
   public String login(Model model) {

       System.out.println("\n-------- MainController.login --- ");

       System.out.println(" ** You are in Controller ** ");

       return "login";
   }

}

7. Die Applikation laufen

Den Pfad laufen
Und das ist Log auf dem Bildschirm Console:
URL laufen
Log sehen:
No ADS