在另一个控制器中获取模型和视图对象
get modelandview object in another controller
我正在努力将数据从一个控制器传递到另一个控制器。
我有一个用@ControllerAdvice 注释的class,用于处理应用程序的所有异常。
我正在处理异常并将它们添加到自定义 class 然后在 ModelAndView 中添加它并使用重定向传递给另一个控制器。
在那个控制器中,我想要那个添加的对象,但我不太了解如何获取该对象。我尝试了一些技巧,但没有成功。
代码:
异常处理程序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;
}
}
我的家庭控制器:
@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 并获取属性(对象):
public String getHomePage(@ModelAttribute("processingException", HttpServletRequest req)
{
//....
req.getAttribute("processingException");
在谷歌搜索和搜索各种论坛和文章后,我找到了一些解决方案。我结合了各种论坛的数据和代码,满足了我的要求。
我们可以使用 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
}
毕竟宾果..完成我的工作。
如果有人对此有更好的想法,那么仍然欢迎..
谢谢大家。
我正在努力将数据从一个控制器传递到另一个控制器。 我有一个用@ControllerAdvice 注释的class,用于处理应用程序的所有异常。
我正在处理异常并将它们添加到自定义 class 然后在 ModelAndView 中添加它并使用重定向传递给另一个控制器。 在那个控制器中,我想要那个添加的对象,但我不太了解如何获取该对象。我尝试了一些技巧,但没有成功。
代码:
异常处理程序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;
}
}
我的家庭控制器:
@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 并获取属性(对象):
public String getHomePage(@ModelAttribute("processingException", HttpServletRequest req)
{
//....
req.getAttribute("processingException");
在谷歌搜索和搜索各种论坛和文章后,我找到了一些解决方案。我结合了各种论坛的数据和代码,满足了我的要求。
我们可以使用 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
}
毕竟宾果..完成我的工作。
如果有人对此有更好的想法,那么仍然欢迎..
谢谢大家。