小编君主不*_*不是你的帖子

为什么 Spring 异常处理程序没有按预期工作

当我@ExceptionHandler在控制器中使用时,它没有按预期工作。

这是我的代码:

@Controller
public class PageController {

    @RequestMapping("/page")
    public ModelAndView index(ModelAndView modelAndView){
        String mess = null;
        mess.split(",");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @ExceptionHandler({Exception.class})
    @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Bad Request")
    public ModelAndView handleException(Exception ex, HttpServletRequest request,     HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", ex.getMessage());
        modelAndView.addObject("url", request.getRequestURL());
        modelAndView.addObject("code", response.getStatus());
        modelAndView.setViewName("exception");
        return modelAndView;
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序以调试模式启动后,我访问http://localhost:8080/page,并且handleException正在运行,但视图低于预期excepction视图。为什么?

在此输入图像描述

spring-mvc

0
推荐指数
1
解决办法
4457
查看次数

标签 统计

spring-mvc ×1