Get a modelandview object in another controller

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

I handle the exception and add them to the custom class and then to the ModelAndView. I add this and pass it to another controller using redirection. And in this controller I want this added object, but I don't have a big idea on how to get this object. I tried some trick but no 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 an idea how to get the object data in my controller?

Thank.

+3


source to share


2 answers


After a lot of searching and searching various forums and articles, I found some solution. I pooled data and code from various forums where I fulfilled my requirement.

We can use FlashMap . Just get the request context and add FlashMap and add other data to FlashMap.

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 on the other hand, in the controller, use ModelAttribute to get the data that is sent from the 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 the bingo .. Did my job.

If anyone has an even better idea, then welcome anyway.

Thanks guys.

+1


source


You can do a workaround like this:

public ModelAndView handleException(Exception ex, HttpServletRequest req) {
//...
ModelAndView modelAndView = new ModelAndView("forward:/");
//...
req.setAttribute("processingException", response);

      



Then, in your controller method, you will access the HttpServletRequest and get the attribute (object):

public String getHomePage(@ModelAttribute("processingException", HttpServletRequest req)
{
//....
req.getAttribute("processingException");

      

0


source







All Articles