gpt4 book ai didi

java - 在另一个 Controller 中获取模型和 View 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:35 25 4
gpt4 key购买 nike

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

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

代码:

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

我的家庭 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
}

有人知道如何在我的 Controller 中获取对象数据吗?

谢谢。

最佳答案

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

我们可以使用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:/";
}
}

另一方面,在 Controller 中使用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
}

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

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

谢谢你们。

关于java - 在另一个 Controller 中获取模型和 View 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31714671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com