" $ {#fields.hasErrors('*')}"始终为假-重定向问题 [英] "${#fields.hasErrors('*')}" always false - Redirect Problem

查看:424
本文介绍了" $ {#fields.hasErrors('*')}"始终为假-重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器如下:

    @PostMapping("/event/{id}")
    public String save(@PathVariable("id") long id, @Valid Form form, BindingResult bindingResult ) {

        if (!bindingResult.hasErrors()) {
          //No errors
          //No return
        }

        return "redirect:/event/{id}";               
    }

我的@GetMapping是:

My @GetMapping is:

 @GetMapping("/event/{id}")
    public ModelAndView eventDetail(@PathVariable("id") long id) {

        ModelAndView model = new ModelAndView("event/details");
        Event event = eventoRepository.findById(id).get();
        model.addObject("event", evento);
        model.addObject("guests", event.getGuests());
        model.addObject("guest",new Guest());

        return model;

    }

我知道"$ {#fields.hasErrors('*')}"始终为假,因为重定向.(对吗?)

I know that "${#fields.hasErrors('*')}" is always false because redirect. (right?)

如何在没有重定向的情况下返回此路径/event/{id} ?

How return to this path /event/{id} without redirect?

推荐答案

我知道"$ {#fields.hasErrors('*')}"始终为假,因为重定向.(对吗?)

I know that "${#fields.hasErrors('*')}" is always false because redirect. (right?)

对.看来您总是重定向.因此,调用了一个用 @GetMapping("/event/{id}")注释的方法,该表单很可能被重置为新状态,并且不再有错误使表达式始终为错误.

Right. It looks like you always redirect. Because of that a method annotated with @GetMapping("/event/{id}") is called, the form most likely is reseted to fresh state and there are no more errors making expression always false.

如何在不重定向的情况下返回此路径/event/{id}?

How return to this path /event/{id} without redirect?

仅返回包含表单的视图(模板)的名称.很有可能与用 @GetMapping("/event/{id}")注释的方法返回的内容相同.

Simply return name of the view (template) containing the form. Most likely it's the same what's returned by method annotated with @GetMapping("/event/{id}").

您应该遵循本指南中的方法.如果表单包含错误,则返回无重定向,否则重定向.

You should follow an approach from this guide. Return without redirect if the form contains error, and redirect otherwise.

修改:您还应该为模型提供其他对象.代替在每个方法(获取,发布等)中填充模型,您可以将常见对象提取到以 @ModelAttribute 注释的方法.根据 javadoc 这样的方法可以接受与用 @RequestMapping 注释的方法类似的参数.

You should also provide additional objects to the model. Instead of populating model in each method (Get, Post etc.) you may extract common objects to a method annotated with @ModelAttribute. According to javadoc such a method can accept similar parameters as methods annotated with @RequestMapping.

对于您的情况,这样的操作应该可以正常工作:

For your case something like this should work fine:

    @ModelAttribute
    void supplyModel(@PathVariable("id") long id, Model model) {
        Event event = eventoRepository.findById(id).get();
        model.addAttribute("event", evento);
        model.addAttribute("guests", event.getGuests());
        model.addAttribute("guest",new Guest());
    }

    @GetMapping("/event/{id}")
    public String eventDetail(@PathVariable("id") long id) {
        return "event/details";

    }

    @PostMapping("/event/{id}")
    public String save(@PathVariable("id") long id, @Valid Form form, BindingResult bindingResult ) {

        if (bindingResult.hasErrors()) {
            // Has errors
            return "event/details";
        }

        // No errors
        return "redirect:/event/" + id;
    }

这篇关于" $ {#fields.hasErrors('*')}"始终为假-重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆