• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Request redirection and forwarding

After processing a request, a processor can jump to another resource in two ways: request forwarding and redirection. Depending on the type of resource to jump to, there are two types: jump to page and jump to other processors.

Note that for a page requesting a forward, it can be a web-INF page; The redirected page cannot be a web-INF page. Because a redirect is equivalent to the user making another request, the user cannot directly access the web-INF resource.

The SpringMVC framework encapsulates the request forwarding and redirection operations in the original Servlet. Forwarding and redirection can now be implemented in a simple way.

Forward: forward, to achieve the request. GetRequestDispatcher (” xx. JSP “). The forward ()

Redirect: Response.sendreDirect (“xxx.jsp”)

Forward requests

When the handler method returns ModelAndView, add forward: to the view specified by setViewName(), and the view no longer works with the view parser, so that views at different locations can be specified when the parser is configured. The view page must write out the path relative to the project root. The forward operation does not require a view parser.

Processor method Mandatory String, adding forward to the view path: full view path.

Request redirection

You can redirect by adding redirect: to the view string returned by the processor method.

Processor method definitions:

Exception handling

A common way the SpringMVC framework handles exceptions is by using the @ExceptionHandler annotation.

@ ExceptionHandler annotations

The @ExceptionHandler annotation is used to specify a method as an exception handling method. The annotation has only one optional attribute, value, which is a Class<? > array that specifies the exception class to be handled by the annotation’s method, that is, the exception to be matched.

The return value of an annotated method can be ModelAndView, String, or void, and the method name can be arbitrary. The method parameters can be Exception and its subclasses, HttpServletRequest, HttpServletResponse, and so on. The system automatically assigns values to these method parameters.

For the use of exception handling annotations, it is also possible to annotate exception handling methods directly into the Controller.

(1) Custom exception class

Define three exception classes: NameException, AgeException, and MyUserException. MyUserException is the parent of the other two exceptions.

(2) Modify Controller to throw an exception

(3) Define the abnormal response page

Define three exception response pages.

However, this is not generally used. Instead, the exception handling methods are specifically defined in a class as a global exception handling class.

You need to use the @controllerAdvice annotation, which literally means’ controller enhancement ‘, to enhance controller objects. A class decorated with @ControllerAdvice can use @ExceptionHandler.

When an exception is thrown by a method decorated with the @RequestMapping annotation, the exception handling method in the @ControllerAdvice modified class is executed.

@ControllerAdvice is decorated with the @Component annotation. You can create an object by scanning the @ControllerAdvice classpath (package name) in context: Component-scan.

(4) Define global exception handling classes

(5) Define the Spring configuration file