我不知道要在标题中加上什么,但是我有以下代码:

@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”,但是它不会添加“车辆”模型。有没有解决的办法?我以为它将以某种方式将其定向到上面的@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";
            }

最佳答案

您需要使用@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内部,所以我在两个位置都添加了它们,只是为了确保。

09-30 13:55