简体   繁体   English

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

[英]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. 我有一个用@ControllerAdvice注释的类,用于处理所有应用程序异常。

I am processing exception and adding them to custom class then in ModelAndView I am adding that and passing to another controller using redirect. 我正在处理异常并将它们添加到自定义类然后在ModelAndView中我添加它并使用重定向传递给另一个控制器。 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: 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;
    }
}

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. 谢谢。

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. 我们可以使用FlashMap Just get context of request and add FlashMap and add other data to FlashMap as well. 只需获取请求的上下文并添加FlashMap并将其他数据添加到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 other hand, in controller use ModelAttribute to get data that is sent from exception handler method. 另一方面,在控制器中使用ModelAttribute来获取从异常处理程序方法发送的数据。

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. 多谢你们。

You could make 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 have access to HttpServletRequest and get the Attribute (Object): 然后在您的Controller方法中,您可以访问HttpServletRequest并获取属性(对象):

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

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

相关问题 如何从添加到控制器内部modelAndView的arraylist中将对象放入jsp - how to get object into jsp from arraylist which is added into modelAndView inside controller 如何在java弹簧框架中的modelAndView.getModel()返回的spring控制器本身内部的modelMap对象中获取所有键值对 - How to get all the key value pairs in modelMap object , inside spring controller itself , returned by modelAndView.getModel() in java spring framework 从控制器测试的上下文中访问ModelAndView对象中包含的模型的属性 - Accessing the attributes of a model contained in a ModelAndView object from the context of a controller test ModelAndView返回方法对象 - ModelAndView returning method object ModelAndView对象未返回到jsp - ModelAndView object not returned to jsp 当您使用Spring Forward重定向到其他控制器时,如何从ModelAndView返回响应对象? - How do you return the response object from ModelAndView when you are using spring forward to redirect to a different controller? 同时返回带有列表对象的ModelAndView - Return a ModelAndView with a List Object Simultaneously JavaScript中的Spring MVC modelAndView对象 - Spring MVC modelAndView object in javascript 如何重定向到另一个 URI 并从以前的 modelAndView 访问 object - How do you redirect to another URI and access an object from the previous modelAndView 从先前的ModelAndView访问对象 - accessing object from previous ModelAndView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM