A dot eyeball.

Through @ ControllerAdvice. We can place the global configuration of the controller in the same place. Methods annotated with @ControllerAdvice can be annotated with @ExceptionHandler, @initBinder, @ModelAttribute. This is valid for all methods in the controller annotated @requestMapping.

@ExceptionHandler: Used to handle exceptions in a controller globally. InitBinder: Set the WebDataBinder that automatically binds foreground request parameters to the Model. @modelAttribute: @modelAttribute is used to bind key/value pairs to Model, so that @requestMapping can obtain the key/value pairs set here.

The following uses @ExceptionHandler to handle global exceptions and output exception information to users more humanized.

Example 2.

1. Custom @ ControllerAdvice

package org.light4j.springMvc4.advice; import org.springframework.ui.Model; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.ModelAndView; Public class ExceptionHandlerAdvice {@ExceptionHandler(value = exception.class)// public ModelAndView exception(Exception exception, WebRequest request) { ModelAndView modelAndView = new ModelAndView("error"); // Error page modelAndView.addobject ("errorMessage", exception.getMessage()); return modelAndView; } @modelattribute //③ public void addAttributes(Model Model) {model.addattributes (" MSG ", "extra info "); @ InitBinder / / / / (4)} (4) public void InitBinder (WebDataBinder WebDataBinder) {WebDataBinder. SetDisallowedFields (" id "); / / 5.}}Copy the code

Code explanation:

① @controlleradvice declares a ControllerAdvice. @controlleradvice combines the @component annotation, so it automatically registers as a Spring Bean. ② @ExceptionHandler defines the global processing here. The conditions for intercepting the @ExceptionHandler value attribute can be filtered. ③ Use the @modelAttribute annotation to add the key-value pair globally. This key-value pair is available to all @requestMapping methods annotated. ④ Customize WebDataBinder with the @initBinder annotation.

Ignoring the ID of the request parameter is shown below. For more information on WebDataBinder configuration, refer to the WebDataBinder API documentation.

2. Demonstrate the controller

package org.light4j.springMvc4.web; import org.light4j.springMvc4.domain.DemoObj; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class AdviceController { @RequestMapping("/advice") public String getSomething(@ModelAttribute("msg") String MSG,DemoObj obj){//① throw new IllegalArgumentException(" I'm sorry, the argument is wrong /"+" from @modelAttribute :"+ MSG); }}Copy the code

3. The abnormal page is displayed

SRC /main/resources/views create a new error.jsp with the following contents:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page isELIgnored="false" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta  http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>@ControllerAdvice Demo</title> </head> <body> ${errorMessage} </body> </html>Copy the code

4. Run

accesshttp://localhost/springMvc4.x-advice/advice?id=1&name=xxx. The debug viewDemoObj.idIt’s filtered out,

As shown below:



And won the@ModelAttributethemsgInformation,

As shown below:



The page effect is as follows:

Note: when I was testing, I could not get the value of the el expression in error.jsp, and could not get the value passed from the background. If the web.xml header version is 2.3, the default JSP does not enable EL parsing, so you need to enable EL parsing. Add the following configuration to the error.

<%@ page isELIgnored="false" %>Copy the code

Add the above configuration and the problem is solved.

Three. Source code examples:

Github Address: Click to view code cloud address: click to view

exceptional
Welcome to follow the life designer’s wechat public account



longjiazuoA