Въведение в Spring DispatcherServlet

1. Въведение

Просто казано, в модела на дизайна на предния контролер , един контролер е отговорен за насочване на входящите HttpRequests към всички други контролери и манипулатори на приложението .

Spring's DispatcherServlet изпълнява този модел и следователно е отговорен за правилната координация на HttpRequests към техните десни манипулатори.

В тази статия ще разгледаме работния процес за обработка на заявките на Spring DispatcherServlet и как да внедрим няколко от интерфейсите, които участват в този работен поток.

2. Обработка на заявка DispatcherServlet

По същество DispatcherServlet обработва входяща HttpRequest , делегира заявката и обработва заявката съгласно конфигурираните интерфейси HandlerAdapter , които са внедрени в приложението Spring, заедно със съпътстващи анотации, посочващи манипулатори, крайни точки на контролера и обекти за отговор.

Нека да разберем по-задълбочено как DispatcherServlet обработва компонент:

  • на WebApplicationContext свързано с DispatcherServlet под ключ DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE се търси и е на разположение на всички елементи на процеса
  • В DispatcherServlet намира всички реализации на HandlerAdapter интерфейс конфигурирано за вашия диспечер използване getHandler () - всеки открити нарушения и конфигурирани дръжки за изпълнение на искането посредством дръжка () през останалата част от процеса
  • на LocaleResolver избор е свързан с искане да се даде възможност на елементи на процеса за разрешаване на локал
  • на ThemeResolver избор е свързан с искане да споделите с елементи като възгледи, определи коя тема да употреба
  • ако е посочен MultipartResolver , заявката се проверява за MultipartFile s - всички намерени се увиват в MultipartHttpServletRequest за по-нататъшна обработка
  • Реализациите на HandlerExceptionResolver, декларирани в WebApplicationContext, взимат изключения, които се появяват по време на обработката на заявката

Можете да научите повече за всички начини за регистрация и настройка на DispatcherServlet тук.

3. Интерфейси на HandlerAdapter

Интерфейсът HandlerAdapter улеснява използването на контролери, сървлети, HttpRequests и HTTP пътища през няколко специфични интерфейса. По този начин интерфейсът HandlerAdapter играе съществена роля през многото етапи на работния процес на обработка на заявките DispatcherServlet .

Първо, всяка реализация на HandlerAdapter се поставя в HandlerExecutionChain от метода getHandler () на вашия диспечер . След това, всяко от тези изпълнения дръжка () на HttpServletRequest обект като протича изпълнение верига.

В следващите раздели ще разгледаме по-подробно няколко от най-важните и често използвани HandlerAdapters .

3.1. Съпоставяне

За да разберем картографирането, първо трябва да разгледаме как да анотираме контролерите, тъй като контролерите са толкова важни за интерфейса HandlerMapping .

В SimpleControllerHandlerAdapter дава възможност за прилагане на контролер изрично без @Controller анотация.

На RequestMappingHandlerAdapter методи опори анотирани с @RequestMapping анотацията .

Тук ще се съсредоточим върху анотацията @Controller , но е наличен и полезен ресурс с няколко примера, използващи SimpleControllerHandlerAdapter .

В @RequestMapping анотацията се задава конкретното крайната точка, при която манипулатор ще бъде на разположение в рамките на WebApplicationContext свързани с нея.

Нека да видим пример за контролер, който излага и обработва крайната точка „/ потребител / пример“ :

@Controller @RequestMapping("/user") @ResponseBody public class UserController { @GetMapping("/example") public User fetchUserExample() { // ... } }

Пътищата, посочени от анотацията @RequestMapping, се управляват вътрешно чрез интерфейса HandlerMapping .

Структурата на URL адресите е естествено по отношение на самия DispatcherServlet - и се определя от картографирането на сървлета.

По този начин, ако DispatcherServlet е картографиран на '/', тогава всички картографирания ще бъдат обхванати от това картографиране.

Ако обаче съответствието на сървлета е ' / диспечер ', тогава всички анотации @ RequestMapping ще бъдат относителни към този корен URL.

Не забравяйте, че '/' не е същото като '/ *' за сървиране на сървлети! '/' е картографирането по подразбиране и излага всички URL адреси в зоната на отговорност на диспечера.

‘/*' is confusing to a lot of newer Spring developers. It does not specify that all paths with the same URL context are under the dispatcher's area of responsibility. Instead, it overrides and ignores the other dispatcher mappings. So, ‘/example' will come up as a 404!

For that reason, ‘/*' shouldn't be used except in very limited circumstances (like configuring a filter).

3.2. HTTP Request Handling

The core responsibility of a DispatcherServlet is to dispatch incoming HttpRequests to the correct handlers specified with the @Controller or @RestController annotations.

As a side note, the main difference between @Controller and @RestController is how the response is generated – the @RestController also defines @ResponseBody by default.

A writeup where we go into much greater depth regarding Spring's controllers can be found here.

3.3. The ViewResolver Interface

A ViewResolver is attached to a DispatcherServlet as a configuration setting on an ApplicationContext object.

A ViewResolver determines both what kind of views are served by the dispatcher and from where they are served.

Here's an example configuration which we'll place into our AppConfig for rendering JSP pages:

@Configuration @EnableWebMvc @ComponentScan("com.baeldung.springdispatcherservlet") public class AppConfig implements WebMvcConfigurer { @Bean public UrlBasedViewResolver viewResolver() { UrlBasedViewResolver resolver = new UrlBasedViewResolver(); resolver.setPrefix("/WEB-INF/view/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); return resolver; } }

Very straight-forward! There are three main parts to this:

  1. setting the prefix, which sets the default URL path to find the set views within
  2. the default view type which is set via the suffix
  3. setting a view class on the resolver which allows technologies like JSTL or Tiles to be associated with the rendered views

One common question involves how precisely a dispatcher's ViewResolverand the overall project directory structure are related. Let's take a look at the basics.

Here's an example path configuration for an InternalViewResolver using Spring's XML configuration:

For the sake of our example, we'll assume that our application is being hosted on:

//localhost:8080/

This is the default address and port for a locally hosted Apache Tomcat server.

Assuming that our application is called dispatcherexample-1.0.0, our JSP views will be accessible from:

//localhost:8080/dispatcherexample-1.0.0/jsp/

The path for these views within an ordinary Spring project with Maven is this:

src -| main -| java resources webapp -| jsp WEB-INF

The default location for views is within WEB-INF. The path specified for our InternalViewResolver in the snippet above determines the subdirectory of ‘src/main/webapp' in which your views will be available.

3.4. The LocaleResolver Interface

The primary way to customize session, request, or cookie information for our dispatcher is through the LocaleResolver interface.

CookieLocaleResolver is an implementation allowing the configuration of stateless application properties using cookies. Let's add it to AppConfig.

@Bean public CookieLocaleResolver cookieLocaleResolverExample() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setDefaultLocale(Locale.ENGLISH); localeResolver.setCookieName("locale-cookie-resolver-example"); localeResolver.setCookieMaxAge(3600); return localeResolver; } @Bean public LocaleResolver sessionLocaleResolver() { SessionLocaleResolver localeResolver = new SessionLocaleResolver(); localeResolver.setDefaultLocale(Locale.US); localResolver.setDefaultTimeZone(TimeZone.getTimeZone("UTC")); return localeResolver; } 

SessionLocaleResolver allows for session-specific configuration in a stateful application.

The setDefaultLocale() method represents a geographical, political, or cultural region, whereas setDefaultTimeZone( ) determines the relevant TimeZone object for the application Bean in question.

Both methods are available on each of the above implementations of LocaleResolver.

3.5. The ThemeResolver Interface

Spring provides stylistic theming for our views.

Let's take a look at how to configure our dispatcher to handle themes.

First, let's set up all the configuration necessary to find and use our static theme files. We need to set a static resource location for our ThemeSource to configure the actual Themes themselves (Theme objects contain all of the configuration information stipulated in those files). Add this to AppConfig:

@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/", "/resources/") .setCachePeriod(3600) .resourceChain(true) .addResolver(new PathResourceResolver()); } @Bean public ResourceBundleThemeSource themeSource() { ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource(); themeSource.setDefaultEncoding("UTF-8"); themeSource.setBasenamePrefix("themes."); return themeSource; } 

Requests managed by the DispatcherServlet can modify the theme through a specified parameter passed into setParamName() available on the ThemeChangeInterceptor object. Add to AppConfig:

@Bean public CookieThemeResolver themeResolver() { CookieThemeResolver resolver = new CookieThemeResolver(); resolver.setDefaultThemeName("example"); resolver.setCookieName("example-theme-cookie"); return resolver; } @Bean public ThemeChangeInterceptor themeChangeInterceptor() { ThemeChangeInterceptor interceptor = new ThemeChangeInterceptor(); interceptor.setParamName("theme"); return interceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(themeChangeInterceptor()); } 

The following JSP tag is added to our view to make the correct styling appear:


     

The following URL request renders the example theme using the ‘theme' parameter passed into our configured ThemeChangeIntercepter:

//localhost:8080/dispatcherexample-1.0.0/?theme=example

3.6. The MultipartResolver Interface

A MultipartResolver implementation inspects a request for multiparts and wraps them in a MultipartHttpServletRequest for further processing by other elements in the process if at least one multipart is found. Add to AppConfig:

@Bean public CommonsMultipartResolver multipartResolver() throws IOException { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize(10000000); return resolver; } 

Now that we've configured our MultipartResolver bean, let's set up a controller to process MultipartFile requests:

@Controller public class MultipartController { @Autowired ServletContext context; @PostMapping("/upload") public ModelAndView FileuploadController( @RequestParam("file") MultipartFile file) throws IOException { ModelAndView modelAndView = new ModelAndView("index"); InputStream in = file.getInputStream(); String path = new File(".").getAbsolutePath(); FileOutputStream f = new FileOutputStream( path.substring(0, path.length()-1) + "/uploads/" + file.getOriginalFilename()); int ch; while ((ch = in.read()) != -1) { f.write(ch); } f.flush(); f.close(); in.close(); modelAndView.getModel() .put("message", "File uploaded successfully!"); return modelAndView; } }

We can use a normal form to submit a file to the specified endpoint. Uploaded files will be available in ‘CATALINA_HOME/bin/uploads'.

3.7. The HandlerExceptionResolver Interface

Spring's HandlerExceptionResolver provides uniform error handling for an entire web application, a single controller, or a set of controllers.

To provide application-wide custom exception handling, create a class annotated with @ControllerAdvice:

@ControllerAdvice public class ExampleGlobalExceptionHandler { @ExceptionHandler @ResponseBody public String handleExampleException(Exception e) { // ... } }

Any methods within that class annotated with @ExceptionHandler will be available on every controller within dispatcher's area of responsibility.

Implementations of the HandlerExceptionResolver interface in the DispatcherServlet's ApplicationContext are available to intercept a specific controller under that dispatcher's area of responsibility whenever @ExceptionHandler is used as an annotation, and the correct class is passed in as a parameter:

@Controller public class FooController{ @ExceptionHandler({ CustomException1.class, CustomException2.class }) public void handleException() { // ... } // ... }

The handleException() method will now serve as an exception handler for FooController in our example above if either exception CustomException1 or CustomException2 occurs.

Here's an article that goes more in-depth about exception handling in a Spring web application.

4. Conclusion

In this tutorial, we've reviewed Spring's DispatcherServlet and several ways to configure it.

As always, the source code used in this tutorial is available over on Github.