javaspringhttp-redirectmodelandview

get modelandview object in another controller


I am working to pass data from one controller to another. I have one class that is annotated with @ControllerAdvice that is used to handle all exception of application.

I am processing exception and adding them to custom class then in ModelAndView I am adding that and passing to another controller using redirect. And in that controller I want that added object but I don't have much idea about it how to get that object. I have tried some trick but did not get success.

Code:

ExceptionHandler class:

@ControllerAdvice
public class DefaultExceptionHandler {

    @Autowired
    private CPro cPro;

    private static final Logger LOG = LoggerFactory.getLogger(DefaultExceptionHandler.class);

    @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ModelAndView handleException(Exception ex) {

        ModelAndView modelAndView = new ModelAndView("redirect:/");
        String exceptionType = ex.getClass().getSimpleName();
        DefaultExceptionHandler.LOG.error("Internal Server Exception", ex);
        ErrorResponse response = new ErrorResponse();
        if (ex.getCause() != null) {
            response.addSimpleError(exceptionType, ex.getCause().getMessage(), cPro.getProName());
        } else {
            response.addSimpleError(exceptionType, ex.getMessage(), cPro.getProName());
        }
        modelAndView.addObject("processingException", response);

        return modelAndView;
    }
}

my home controller:

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String getHomePage(@ModelAttribute("processingException") ErrorResponse errorResponse, Model model) {                

        // I want to get object data of processingException added in exception handler using ModelAndView
        model.addAttribute("processingException", errorResponse.getError() == null ? null : errorResponse);
        return "upscale"; //here upscale.html redirection       
    }

Does anyone have idea that how to get that object data in my controller ?

Thanks.


Solution

  • After a lot googling and searching various forums and article, I found some solution. I have combined data and code of various forums I have made my requirement fulfill.

    We can use FlashMap for that. Just get context of request and add FlashMap and add other data to FlashMap as well.

    Code:

    @ControllerAdvice
    public class DefaultExceptionHandler {
    
        @Autowired
        private CPro cPro;
    
        private static final Logger LOG = LoggerFactory.getLogger(DefaultExceptionHandler.class);
    
        @ExceptionHandler(Exception.class)
        public String handleException(Exception ex, HttpServletRequest request) throws IOException {
    
            DefaultExceptionHandler.LOG.error("Internal Server Exception", ex);
    
            String exceptionType = ex.getClass().getSimpleName();
    
            ErrorResponse response = new ErrorResponse();
    
            if (ex.getCause() != null) {
                response.addError(exceptionType, ex.getCause().getMessage(), cPro.getProName());
            } else {
                response.addError(exceptionType, ex.getMessage(), cPro.getProName());
            }
    
            FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(request);
            if (outputFlashMap != null) {
                outputFlashMap.put("processingException", response);
            }
    
            return "redirect:/";
        }
    }
    

    and other hand, in controller use ModelAttribute to get data that is sent from exception handler method.

    code:

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getHomePage(Model model, @ModelAttribute("processingException") Object processingException) {                
    
        if (processingException instanceof ErrorResponse) {
            model.addAttribute("processingException", ((ErrorResponse) processingException).getError());
        } else {
            model.addAttribute("processingException", null);
        }
        return "upscale"; //here upscale.html redirection       
    }
    

    After all bingo.. Done my work.

    If anyone have still better idea on it then still welcome..

    Thanks guys.