Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在另一个控制器中获取modelandview对象_Java_Spring_Redirect_Modelandview - Fatal编程技术网

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

Java 在另一个控制器中获取modelandview对象,java,spring,redirect,modelandview,Java,Spring,Redirect,Modelandview,我正在努力将数据从一个控制器传递到另一个控制器。 我有一个用@ControllerAdvice注释的类,用于处理应用程序的所有异常 我正在处理异常并将它们添加到自定义类中,然后在ModelAndView中添加该异常并使用重定向将其传递给另一个控制器。 在那个控制器中,我想要那个添加的对象,但我不知道如何得到那个对象。我试过一些把戏,但没有成功 代码: 异常处理程序类: @ControllerAdvice public class DefaultExceptionHandler { @A

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

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

代码:

异常处理程序类:

@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       
    }
我的家庭控制器:

@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       
    }
有人知道如何在我的控制器中获取对象数据吗


谢谢。

您可以这样做:

public ModelAndView handleException(Exception ex, HttpServletRequest req) {
//...
ModelAndView modelAndView = new ModelAndView("forward:/");
//...
req.setAttribute("processingException", response);
然后在控制器方法中,您可以访问HttpServletRequest并获取属性(对象):


在谷歌搜索了很多论坛和文章之后,我找到了一些解决方案。我结合了各种论坛的数据和代码,使我的要求得到满足

我们可以用它。只需获取请求的上下文,添加FlashMap,并向FlashMap添加其他数据

代码:

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

代码:

毕竟是宾果游戏。。完成了我的工作

如果有人对此有更好的想法,欢迎


谢谢各位。

您必须了解重定向会导致新的请求/响应周期。原始请求中的请求属性在第二个请求中不再存在。使用重定向属性。@SotiriosDelimanolis:我已经发布了我自己问题的答案,你能告诉我这是不是一个更好的方法吗。谢谢
@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       
}