繁体   English   中英

在另一个控制器中获取modelandview对象

[英]get modelandview object in another controller

我正在努力将数据从一个控制器传递到另一个控制器。 我有一个用@ControllerAdvice注释的类,用于处理所有应用程序异常。

我正在处理异常并将它们添加到自定义类然后在ModelAndView中我添加它并使用重定向传递给另一个控制器。 在那个控制器中我想要添加的对象,但我对它如何获得该对象并不太了解。 我尝试了一些技巧,但没有成功。

码:

ExceptionHandler类:

@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;
    }
}

我的家庭控制员:

@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       
    }

有谁知道如何在我的控制器中获取该对象数据?

谢谢。

经过大量的谷歌搜索和搜索各种论坛和文章后,我找到了一些解决方案。 我已经结合了我的要求实现的各种论坛的数据和代码。

我们可以使用FlashMap 只需获取请求的上下文并添加FlashMap并将其他数据添加到FlashMap中。

码:

@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:/";
    }
}

另一方面,在控制器中使用ModelAttribute来获取从异常处理程序方法发送的数据。

码:

@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       
}

毕竟宾果..完成我的工作。

如果有人对它有更好的想法,那么仍然欢迎..

多谢你们。

你可以像这样做一个解决方法:

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

然后在您的Controller方法中,您可以访问HttpServletRequest并获取属性(对象):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM