首页 > 解决方案 > 调用 RequestMapping “两次”

问题描述

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

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

标签: javaspringmaventhymeleaf

解决方案


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

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

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


推荐阅读