Spring MVC异常处理 和 重定向传递数据

1.异常处理介绍

  Spring在web项目中,如果在请求处理时出现异常,那输出会是Servlet响应。这时异常需要以某种方式转换为响应。

  Spring将异常转换为响应的方式:

    a.特定的Spring异常将自动映射为指定的HTTP状态码;

    b.异常上添加@ResponseStatus注解,从而将其映射为某一个HTTP状态码;

    c.方法上添加@ExceptionHandler注解,使其处理异常。

 

2.异常处理代码

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 }

  正常情况下,当id为error时,会报错,这时是500错误,我们可以通过@ResponseStatus注解,映射到404状态码上,进行简单的异常处理。

 

  第二种方法是报相应的异常,直接跳转到错误页面。

 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("出现异常");
14         return "index";
15     }
16 }

 

3.重定向传值

 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              * 重定向传参数:
11              *         相当于把参数写到session中,
12              *             如果重定向到controller的时候,用@ModelAttribute接收
13              *             如果重定向到页面,可以直接接收
14              */
15             model2.addFlashAttribute("flash", "flash");
16             /**
17              * 通过url模板进行重定向
18              */
19             return "redirect:/{id}";
20         }else if("index".equals(id)){
21 //            model.addAttribute("id", id);
22             /**
23              * 转发不可以使用模板
24              */
25 //            return "/{id}";
26             model2.addFlashAttribute("flash", "flash");
27             /**
28              * 通过url模板进行重定向
29              */
30             return "index";
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();
    }

 

 重定向传参数:
    相当于把参数写到session中,
      如果重定向到controller的时候,用@ModelAttribute接收
      如果重定向到页面,可以直接接收
posted @ 2017-04-12 14:02  港城人民  阅读(1551)  评论(0编辑  收藏  举报