【问题标题】:Calling RequestMapping "twice"调用 RequestMapping “两次”
【发布时间】:2018-11-08 17:24:26
【问题描述】:

我不知道该给标题写什么,但我有以下代码:

@Controller
public class WorkdayAddController {
@Autowired
private WorkdayRepository workdayRepository; 

@Autowired
private VehicleRepository vehicleRepository;

@RequestMapping(value = "addworkday")
public String addWorkday(Model model){
    model.addAttribute("workdayaddform", new WorkdayAddForm());
    model.addAttribute("vehicles", vehicleRepository.findAll());
    return "addworkday";
}   

@RequestMapping(value = "saveworkday", method = RequestMethod.POST)
public String save(@Valid @ModelAttribute("workdayaddform") WorkdayAddForm workdayaddform, BindingResult bindingResult) {
    if (!bindingResult.hasErrors()) { // validation errors
        Date workdayBegin = workdayaddform.getBeginDate();
        Date workdayEnd = workdayaddform.getEndDate();
        if (!UtilityClass.dateIsAfterDate(workdayBegin, workdayEnd)) {
            bindingResult.rejectValue("beginDate", "err.beginDate", "Aloitusaika ei voi olla lopetusajan jälkeen.");
            return "addworkday";    
        }
        Workday workday = new Workday();
        Vehicle vehicle = new Vehicle();
        workdayRepository.save(workday);
    }
    else {
        return "addworkday";
    }
    return "redirect:/workdaylist";     
}    

}

在“dateIsAfterDate”检查之后,它应该再次将一个人引导到“addworkday”,它会这样做,但它不会添加“vehicles”模型。有没有解决的办法?我认为它会以某种方式将其定向到上面的 @RequestMapping(value="addworkday") 但似乎并非如此。

更新:

@RequestMapping(value = "addworkday")
public String addWorkday(Model model, RedirectAttributes redirectAttributes){
    System.out.println(redirectAttributes); // {}
    System.out.println(model);  // output in comment
    model.addAttribute("workdayaddform", new WorkdayAddForm()); //I guess I need to add the old workdayform here?
    model.addAttribute("vehicles", vehicleRepository.findAll());
    return "addworkday";
} 

 @RequestMapping(value = "saveworkday", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute("workdayaddform") WorkdayAddForm workdayaddform, 
                       BindingResult bindingResult,
                       final RedirectAttributes redirectAttributes) {
        if (!bindingResult.hasErrors()) { // validation errors
            Date workdayBegin = workdayaddform.getBeginDate();
            Date workdayEnd = workdayaddform.getEndDate();

            if (!UtilityClass.dateIsAfterDate(workdayBegin, workdayEnd)) {
                // Add the vehicle you want to send to the other method.
                redirectAttributes.addFlashAttribute("workdayaddform", workdayaddform);
                redirectAttributes.addFlashAttribute("vehicle", vehicleRepository.findAll());
                redirectAttributes.addFlashAttribute("binding", bindingResult);
                return "redirect:/addworkday";    
            }

【问题讨论】:

    标签: java spring maven thymeleaf


    【解决方案1】:

    您需要使用@RedirectedAttributes 注释来将属性发送到控制器中的另一个方法。此外,您需要在返回的网址中添加“redirect:/”。

     @RequestMapping(value = "saveworkday", method = RequestMethod.POST)
        public String save(@Valid @ModelAttribute("workdayaddform") WorkdayAddForm workdayaddform, 
                           BindingResult bindingResult,
                           final RedirectAttributes redirectAttributes) {
            if (!bindingResult.hasErrors()) { // validation errors
                Date workdayBegin = workdayaddform.getBeginDate();
                Date workdayEnd = workdayaddform.getEndDate();
    
                if (!UtilityClass.dateIsAfterDate(workdayBegin, workdayEnd)) {
                    // Add the vehicle you want to send to the other method.
                    redirectAttributes.addFlashAttribute("vehicle", vehicle);
               redirectAttributes.addFlashAttribute("binding", bindingResult);
                    return "redirect:/addworkday";    
                }
            // More code.
            else {
               redirectAttributes.addFlashAttribute("vehicle", new Vehicle());
               return "redirect:/addworkday"; 
            }
        }
    

    我不确定你的意思是在else 之后还是if 内部,所以我在这两个地方都添加了它们,以确保。

    【讨论】:

    • 谢谢,这可行,但不显示错误。所以我想我需要找到一种方法来将绑定发送到方法。
    • 这只是您需要添加的另一个属性。我刚刚编辑了我的答案。希望它有效。
    • 嗯。我可以从模型访问绑定结果,但 redirectAttributes 显示为空字典 {}。仅添加 BindingResult 作为参数并不像我想象的那么简单,所以我不得不放弃它。我在第一篇文章中更新了我目前的进度。
    • 那么你解决了吗?您始终可以将绑定结果拆分为较小的部分,例如错误消息并像字符串一样发送它们。您将拥有更多属性,但它肯定会起作用。
    • 我想我最终会修复它,无论如何你的回答回答了我原来的问题,所以我接受它。
    猜你喜欢
    • 2015-07-02
    • 1970-01-01
    • 2022-12-09
    • 2016-10-17
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多