调用RequestMapping为“两次”

时间:2018-11-08 17:24:27

标签: java spring maven thymeleaf

我无法弄清楚标题的内容,但是我有以下代码:

@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";    
            }

1 个答案:

答案 0 :(得分:2)

您需要使用p批注才能将属性发送到控制器中的另一种方法。另外,您需要在返回的网址中添加“ redirect:/”。

@RedirectedAttributes

@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中,我不确定您是要说的是什么意思,所以我在两个地方都添加了它们,只是为了确保。