java - 调用 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";
}
解决方案
您需要使用@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
,所以我在两个地方都添加了它们,只是为了确保。
推荐阅读
- jwt - 通过 SIGnalR Core 中的用户名向特定用户发送消息
- r - 当因子在 R 中采用特定值时如何从 data.frame 中删除行
- c# - C# 将 JSON 序列化为简单的字典
- javascript - 带有输入文本的单选按钮
- javascript - XMLHttpRequest.send(data) 不向 Django 视图函数传递数据
- linux - Linux MS Teams 预览版 + XMonad:可能会有通知浮动吗?
- reactjs - 选择并按住并拖动形状应水平或垂直滚动
- python - python:如何添加一个搜索路径
- python - 如何使用 Django 中的列表创建正确的 get 查询集以获取值
- javascript - 如何使用 For 循环动态更改 Vue Js 中的变量?