Customize the configuration of DispatcherServlet

Spring MVC through AbstractAnnotationConfigDispatcherServletInitializer can realize through the Java configuration, there are three methods is written must be carried out by: GetServletConfigClasses getServletConfigClasses getServletMappings (); getServletConfigClasses getServletMappings ();

  1. Once the customizeRegistration DispatcherServlet is registered with the Servlet container, the customizeRegistration method is invoked, And will register after ServletRegistration. Dynamic object passed, through the object can be carried out on the DispatcherServlet some customization, such as: SetLoadOnStartup, setMultipartConfig, setInitParameter, etc

Add filters, servlets, and listeners

Inheritance AbstractAnnotationConfigDispatcherServletInitializer creates DispatcherServlet and ContextLoaderListener, if you want to register other components, Simply implement WebApplicationInitializer interface.

Data in multipart format

Configure the multipart parser

Spring 3.1 starts with two multipart parsers built in:

  1. CommonsMultipartResolver, resolving multipart requests using Jakarta Commons FileUpload
  2. StandardServletMultipartResolver, relies on the Servlet 3.0 support for multipart

In between, StandardServletMultipartResolver is preferred. Declare the parser as a bean to enable it;

You must also specify the relevant configurations, which need to be specified in either web.xml or Java configuration classes, with the details of Multipart as part of the DispatcherServlet configuration.

Process multipart data

The most common way to do this is to use the @RequestPart annotation in a controller method parameter, which can be configured as follows:

  1. @RequestPart(“file”) byte[] file
  2. @RequestPart(“file”) MultipartFile file (org.springframework.web.multipart.MultipartFile)
  3. @RequestPart(“file”) Part file (javax.servlet.http.Part)In this case, you do not need to configure the MultipartResolver bean.

Exception handling

Spring provides several ways to turn exceptions into responses:

  1. Specific Spring exceptions are automatically mapped to the specified HTTP state
  2. An @responseStatus annotation can be added to the exception

  1. Use the method to handle exceptions in the current controller by submitting the @ExceptionHandler annotation on the method
  2. Controller addition generally handles global exceptions in a unified manner

Passing data across redirected requests

Return “redirect: /spittle/{username}? With id=12”, the request is redirected. In the Servlet system, the original request ends, a new GET request is made, and the model data in the original request dies.

  1. Passing the data through the URL template,
. model.addAttribute("username"."Tom"); // Put model.addattribute ("age", 23); // Put it in the query parametersreturn "redirect:/spittle/{username}";
Copy the code

The resulting URL from the above code: /spittle/Tom? Age =23 This is a very straightforward approach, but it has the obvious disadvantage of not passing complex objects.

  1. Using Flash properties

How it works: Data is placed in the session before a redirect occurs, and retrieved from the session and cleaned up after the redirect is complete. Spring’s implementation is slightly different, using the RedirectAttributes object, which the Flash attributes in the session carry until the next request, and then disappear.

All attributes added via RedirectAttributes#addFlashAttribute are copied to the session before the redirect is executed. After the redirect, flash properties existing in the session are fetched and moved from the session to the model. The method that handles the redirection can access the Spittle object from the model.