SpringMVC Exception Handling and springmvc Exception Handling

Source: Internet
Author: User

SpringMVC Exception Handling and springmvc Exception Handling
I. Exception Handling

Spring provides multiple methods to convert exceptions to responses:

  • A specific Spring exception is automatically mapped to the specified HTTP status code.

By default, Spring automatically converts some of its exceptions into appropriate status codes, so as to feedback them to the client. In fact, if no ing exception occurs, the response will carry a 500 status code. The ing table is as follows:

  • You can add the @ ResponseStatus annotation to a custom exception to map it to an HTTP status code.

Although these built-in mappings are very useful, when RuntimeException occurs in our business system, if Spring cannot find the corresponding built-in ing, the default value is 500, what if we don't want a 500 status code? How can we map our custom Exception to the desired status code?

1/** 2 * value indicates the exception status code to be matched. 3 * cause of exception prompted by reson 4 */5 @ ResponseStatus (value = HttpStatus. NOT_FOUND, reason = "Own Exception") 6 public class OwnException extends RuntimeException {7}Custom exception class
  • You can add the @ ExceptionHandler annotation to the method to handle exceptions.

In many cases, we do not want to display ugly error pages to customers. A common practice is to build a friendly page, such as error. jsp. When an exception occurs, return this page to the client. However, if all kinds of processing methods are done in each place, our program will be slightly bloated ...... spring provides a Controller notification (@ ControllerAdvice), that is, when an exception occurs in the @ Controller class, it is handled by this class.

@ ControllerAdvicepublic class ExceptionHandle {/*** when an exception occurs, the error page is returned. Of course, you can write several more ExceptionHandler methods, refine your Exception handling * @ return */@ ExceptionHandler (value = Exception. class) public String handleException () {return "error ";}}View Code 2. Cross-redirect request data transmission

In the String view name returned by the Controller method, if it starts with "redirect:", this String is not used to find the view, but to guide the browser to redirect the path. Sometimes we want the browser to be redirected and some data can be retained, which sounds incredible, but SpringMVC provides us with two solutions:

  • Use the URL template to pass data in the form of PATH variables or query parameters

In this way, parameters are transmitted in PATH variables, but there is a drawback that they cannot pass complex objects...

@ RequestMapping (value = "/home", method = RequestMethod. GET) public String getHome (Model model) {model. addAttribute ("userName", "userName"); model. addAttribute ("id", 123); return "redirect:/home/{userName }";}View Code

In this way, if the last path is parsed/Home/userName? Id = 123

  • Send Data Using flash Properties

What should I do if I want to pass some objects? One way is to exist in the session before the redirection. After the redirection, the session is retrieved from the session and cleared. In fact, this method is feasible and worthy of recommendation. The flash attribute is based on this principle.

/*** @ Param model RedirectAttributes before redirection to ensure that the object survive the redirection process * @ return */@ RequestMapping (value = "/list", method = RequestMethod. GET) public String getList (RedirectAttributes model) {model. addAttribute ("show", "show"); List <String> list = new ArrayList <String> (); list. add ("str"); model. addFlashAttribute ("list", list); // flashAttribute return "redirect: {show }";} /*** after redirection * @ param model * @ return */@ RequestMapping (value = "/show", method = RequestMethod. GET) public String showList (Model model) {System. out. println (model. containsAttribute ("list"); // true return "home ";}View Code

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.