【问题标题】:Redirect on controller advice expectionhandler with model attribute spring MVC使用模型属性spring MVC重定向控制器建议expectionhandler
【发布时间】:2016-05-27 12:32:19
【问题描述】:

所以我尝试从具有模型属性的 ExceptionHandler 重定向到导致它的站点。

例如,我有一个控制器方法可以列出当前项目:

@AvailableInMenu
@RequestMapping(method = RequestMethod.GET)
public String list(Model model) {
    model.addAttribute("Companys", iCompanyService.findAll().getData());
    return "masters/crud/company/list";
}

这是方法调用的模板:

    <div th:if="${exception != null}" class="alert alert-warning alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Warning!</strong><div th:text="${exception}" th:remove="tag" ></div>
    </div>

    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <table id="table" class="table table-striped table-bordered nowrap">
                <thead>
                    <tr>
                      <th th:text="#{app.maintanance.installversion}"></th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="company : ${Companys}">
                        <td th:text="${company.installversion}"></td>
                        <td>
                            <a class="delete-button" th:attr="data-redir=@{/company/delete/{id}(id = ${company.id})}">
                                <i class="fa fa-pencil"></i>
                            </a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

此 thymeleaf 代码生成一个包含数据和删除链接的表。用户使用的实体具有外键,如果用户想要删除它,那么我将使用外键约束将其包装到错误包装​​器中。

这是删除链接的控制器方法:

@RequestMapping(value = {"/delete/{id}"}, method = RequestMethod.GET)
public String deleteCompany(Model model, @PathVariable("id") Long id) {
    ErrorWrapper<Boolean> result = iCompanyService.deleteCompany(id);
    if(result.hasError()){
        if(result.getConstraint()){
            throw new GlobalModelException(601L, "redirect:/company");
        }
    }
    return "redirect:/company";
} 

您可以看到我是否抛出了我创建的一般异常,以及一个带有 ExpectonHandler 的 ControllerAdvice 是否可以捕获我的期望类。我想将异常添加到模型并重定向回列表站点,并在站点上显示引导警报。 这是来自 ControllerAdvice 的异常处理程序

@ExceptionHandler(GlobalModelException.class)
public ModelAndView handlingException(GlobalModelException exception) {
    ModelAndView model = new ModelAndView();
    model.setViewName(exception.getUrl());
    model.addObject("exception", exception);
    return model;
}

ModelAndView 将我重定向到正确的位置,但重定向后异常为 null。我如何使用 ControllerADvice 调用我的列表站点,但模型中存在异常。我有很多其他的异常需要处理,还有很多其他需要处理的控制器。

有没有办法做到这一点?

【问题讨论】:

  • 重定向不能有模型。您将不得不使用 flash 属性。您还确定您想使用 GET 请求进行删除操作吗?请改用 DELETE 或 POST...
  • 因为在 spring 中使用 get 删除基于角色的访问控制非常安全,并且在 @ExceptionHandler 中你不能在控制器中使用 flashattributa 之类的。这是我想到的第一件事
  • 我会说它不是,但是我会使用 DELETE 或 POST(因为这也是数据修改的好习惯)。然后将这些东西放在会话中而不是模型中。另外我认为使用异常来控制流程是你不应该首先做的事情,但恕我直言。
  • 好吧,我该怎么办?我认为处理键重复、外键约束、登录错误和其他一些问题的最简单方法是抛出一个错误,使用一些和处理异常处理程序。你必须抓住这个错误,一旦它被抓住,为什么不使用是?
  • 我没有说过你不应该处理这些异常,但是使用另一个异常是你不应该做的事情,这就是我所说的流控制。

标签: spring spring-mvc thymeleaf


【解决方案1】:

【讨论】:

  • ModelAndView 没有 RedirectAttributes/FlashAttribute,并且由于 @ExceptionHandler 不是您所看到的控制器,@ExceptionHandler(GlobalModelException.class) public ModelAndView handlingException(GlobalModelException exception, final RedirectAttributes redirectAttributes ) {.. .} 也不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 2023-03-22
  • 2013-10-09
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多