使用Spring Boot在@GetMapping处向控制器窗体添加属性

使用Spring Boot在@GetMapping处向控制器窗体添加属性,spring,spring-boot,thymeleaf,Spring,Spring Boot,Thymeleaf,在Validator类的实现中验证日期间隔的重叠。我从Posgresql数据库中得到一个列表,我想在错误消息旁边显示它 我尝试用这行代码插入它,但没有成功: model.addAttribute("dateOverlaps", pricesValidator.getDBIntervals()); 这是完整的代码: @Controller public class PricesController { @Autowired private RateRepository rateReposit

在Validator类的实现中验证日期间隔的重叠。我从Posgresql数据库中得到一个列表,我想在错误消息旁边显示它

我尝试用这行代码插入它,但没有成功:

model.addAttribute("dateOverlaps", pricesValidator.getDBIntervals());
这是完整的代码:

@Controller 
public class PricesController {

@Autowired
private RateRepository rateRepository;

@Autowired
private PricesValidator pricesValidator;

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.addValidators(pricesValidator);
}


//Enter new price form
@GetMapping("/admin/rates/priceform")
public String priceForm(Model model){
    model.addAttribute("rate", new Rate());
    model.addAttribute("dateOverlaps", pricesValidator.getDBIntervals());

    return "/admin/rates/priceform";
}

@PostMapping("/admin/rates/priceform")
public String priceSubmit(@ModelAttribute @Valid Rate price, BindingResult bindingResult){

    if(bindingResult.hasErrors()){

        return "/admin/rates/priceform";
    }
    rateRepository.addRate(price);

    return "redirect:/admin/rates/prices";
}

} 
我使用Thymeleaf,但我想当然地认为问题不在于查看器

这是html视图:

 <!--Global validation results-->
 <div th:if="${#fields.hasErrors('global')}">
     <div class="alert alert-danger" role="alert" 
          th:each="err : ${#fields.errors('global')}"> 
          <div th:switch="${err}">
          <p th:case="error.fromAfterTo" th:text="#error.fromAfterTo}"></p>
          <p th:case="error.overlaps" th:text="#{error.overlaps}"></p>
            <ul>
               <li th:text="#{from} + ' - ' + #{to}"></li>
               <li th:each="interval : ${dateOverlaps}"
                   th:text="${#temporals.format(interval.datefrom, 'dd/MM/yyyy')} + '-' + 
                   ${#temporals.format(interval.dateto, 'dd/MM/yyyy')}">Intervals</li>
            </ul>
          </div>
     </div>
 </div><!--Global validation results-->

  • interval

提前感谢您的帮助。

问题在于@GetMapping上的pricesValidator.getdbinterval()是:

model.addAttribute("dateOverlaps", pricesValidator.getDBIntervals());
正在调用我要显示的列表,并显示错误消息,该消息在提交表单之前为空。因为它的值是通过验证过程从数据库中提取的

这提出了一个新问题:

如何在@PostMapping部分设置此模型属性?

这个问题的答案很简单,在@PostMapping部分,您可以设置模型,也可以在@GetMapping部分设置模型。就我而言,这就是解决方案:

@PostMapping("/admin/rates/priceform")
public String priceSubmit(@ModelAttribute @Valid Rate price, BindingResult bindingResult, Model model){

    if(bindingResult.hasErrors()){
        model.addAttribute("dateOverlaps", pricesValidator.getDBIntervals());
        return "/admin/rates/priceform";
    }
    rateRepository.addRate(price);

    return "redirect:/admin/rates/prices";
}

如何尝试在视图中获取值?那么,调用端点时得到的确切结果是什么呢?我已经添加了视图@DanielOlszewskiI获取带有#{error.overlaps}文本的错误消息,但没有间隔列表的符号。验证器可以工作@你在做什么,你期望会发生什么,会发生什么?你调试代码了吗?打印了Validator.GetDbInterval()返回的价格。查看生成的HTML?我对thymeleaf一无所知,但ul在一个开关内,但不在任何情况下。这正常吗?第一个李在展示吗?