gpt4 book ai didi

java - 调用RequestMapping "twice"

转载 作者:行者123 更新时间:2023-11-30 02:00:00 26 4
gpt4 key购买 nike

我不知道该在标题中添加什么内容,但我有以下代码:

@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注释才能将属性发送到 Controller 中的另一个方法。此外,您还需要将“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 内部,所以我将它们添加到两个位置,只是为了确定。

关于java - 调用RequestMapping "twice",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53213093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com