Interceptors Configuration using Java or XML

In this section, you will learn about how to configure interceptors using Java or XML.

Interceptors Configuration using Java or XML

Interceptors Configuration using Java or XML

In this section, you will learn about how to configure interceptors using Java or XML.

In Spring MVC, you can configure HandlerInterceptors or WebRequestInterceptors , which can be applied to all incoming requests or confined to particular URL patterns.

Given below sample code for registering interceptors in Java :

@Configuration
@EnableWebMvc
public class MyAppWebConfig extends WebMvcConfigurerAdapter {
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new LocaleInterceptor());
		registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
		registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
	}

}

You can do the same in XML using the <mvc:interceptor> as follows :

<mvc:interceptors>
	<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
	<mvc:interceptor>
		<mapping path="/**"/>
		<exclude-mapping path="/admin/**"/>
		<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
	</mvc:interceptor>
	<mvc:interceptor>
		<mapping path="/secure/*"/>
		<bean class="org.example.SecurityInterceptor" />
	</mvc:interceptor>
</mvc:interceptors>

 

Tutorials

  1. Spring 3.2
  2. New Features of Spring Framework 3.2
  3. Spring MVC framework Introduction
  4. Special Bean Types of WebApplicationContext
  5. Spring MVC framework Features
  6. Processing Sequence of DispatcherServlet
  7. @Controller annotation for defining new controller
  8. @RequestMapping annotation for mapping requests
  9. Spring 3.2 Asynchronous Request Processing
  10. Defining handler methods using @RequestMapping annotation
  11. Requests Intercepting through a HandlerInterceptor
  12. View Resolving through ViewResolver interface
  13. Chaining of Multiple view resolvers
  14. Redirecting and forwarding to views
  15. ContentNegotiatingViewResolver
  16. Locales in Spring MVC
  17. Themes and Theme resolvers in Spring MVC
  18. Multipart support for file upload in Spring MVC
  19. @ExceptionHandler & @ResponseStatus annotation
  20. Customizing the Default Error Page
  21. Form Tag library configuration
  22. The form tag
  23. The input tag
  24. The password tag
  25. ControllerClassNameHandlerMapping class for handling convention mapping
  26. Modification in conventional ModelAndView
  27. Autogenerated logical view name through RequestToViewNameTranslator
  28. The checkbox tag
  29. Support for ETag
  30. Servlet container initialization through code
  31. MVC Java Config or the MVC XML Namespace
  32. Customizing the MVC Java config or XML Namespace
  33. Interceptors Configuration using Java or XML
  34. Content Negotiation Configuration
  35. Spring 3.2 MVC Hello World Example
  36. View resolvers
  37. View Controllers Configuration
  38. Static Resources Configuration
  39. mvc:default-servlet-handler
  40. The checkboxes tag