Spring MVC Exception Handling and data redirection, mvc Exception Handling

Source: Internet
Author: User

Spring MVC Exception Handling and data redirection, mvc Exception Handling

1.Exception HandlingIntroduction

In Spring's web project, if an exception occurs during request processing, the output will be a Servlet response. In this case, the exception must be converted to a response in some way.

Spring converts exceptions to responses:

A. a specific Spring exception will be automatically mapped to the specified HTTP status code;

B. Add the @ ResponseStatus annotation to the exception to map it to an HTTP status code;

C. Add the @ ExceptionHandler annotation to the method to handle exceptions.

 

2. Exception Handling Code

1     @RequestMapping("getPathVariable/{id}")2     public String getPathVariable(3             @PathVariable("id") String id){4         if("error".equals(id)){5             throw new SpittleException();6         }7         return "index";8     }

 

 1 package com.taozhiye.controller; 2  3 import org.springframework.http.HttpStatus; 4 import org.springframework.web.bind.annotation.ResponseStatus; 5  6 @ResponseStatus( 7         // 404 8             value = HttpStatus.NOT_FOUND, 9             reason = "Spittle not found"10         )11 public class SpittleException extends RuntimeException {12 13 }

Under normal circumstances, when the id is error, an error is reported. In this case, it is a 500 error. We can map it to the 404 status code through the @ ResponseStatus annotation for simple exception handling.

 

The second method is to report an exception and directly jump to the error page.

1 package com. taozhiye. controller; 2 3 import org. springframework. web. bind. annotation. controllerAdvice; 4 import org. springframework. web. bind. annotation. exceptionHandler; 5 6 7 8 @ ControllerAdvice 9 public class AppExcepitonHandler {10 11 @ ExceptionHandler (Exception. class) 12 public String deal () {13 System. out. println ("exception"); 14 return "index"; 15} 16}

 

3. Redirect value transfer

1 @ RequestMapping ("getPathVariable/{id}") 2 public String getPathVariable (3 @ PathVariable ("id") String id, 4 Model model, 5 RedirectAttributes model2) {6 if ("error ". equals (id) {7 throw new SpittleException (); 8} else if ("findAll ". equals (id) {9/** 10 * redirect parameter passing: 11 * is equivalent to writing the parameter to the session. If 12 * is redirected to the controller, use @ ModelAttribute to receive 13 * If redirected to the page, you can directly receive 14 */15 model2.addFlashAttribute ("flash", "flash "); 16/** 17 * redirect 18 */19 return "redirect:/{id}"; 20} else if ("index ". equals (id) {21 // model. addAttribute ("id", id); 22/** 23 * The template 24 */25 // return "/{id}" cannot be used for forwarding }"; 26 model2.addFlashAttribute ("flash", "flash"); 27/** 28 * redirect 29 */30 return "index" through the url template "; 31} else {32 return "ajax1"; 33} 34}

 

 

@RequestMapping("/findAll")    @ResponseBody    public List<User> findAll(@ModelAttribute("flash") String flash){        System.out.println("flash:"+flash);        return userService.findUserAll();    }@RequestMapping("/index")    @ResponseBody    public List<User> index(Map<String, Object> map,@ModelAttribute("flash")String flash){        System.out.println("flash:"+flash);        return userService.findUserAll();    }

 

Redirect parameters:It is equivalent to writing the parameter to the session. If the parameter is redirected to the controller, @ ModelAttribute is used to receive the parameter. If the parameter is redirected to the page, you can directly receive the parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.