- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道该在标题中添加什么内容,但我有以下代码:
@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/
如何在Spring中忽略类级别@RequestMapping("/home")并直接调用方法级别@RequestMapping("/users")? @Controller @RequestMappi
让我们假设以下 Controller : @RestController public class MyController { @RequestMapping(method = GET, pat
我在使用 Spring Boot 时遇到了一些问题。 当我使用 @RequestMapping(value = "/home") 时,一切正常。但是当我使用 @RequestMapping(name
我正在尝试创建一个restful API(使用Spring Boot v2.0.0.Release),我希望有一个端点,但我希望有两种可能的用途: GET /time - outputs curren
我已经被这个错误困扰了几个小时。。我的pom.xml。应用程序未启动。所有的Spring框架依赖于相同的版本,但仍然得到相同的错误。。更新。MVN依赖的结果:树。看起来这里一切都很好。
我有以下三个 REST API 方法: @RequestMapping(value = "/{name1}", method = RequestMethod.GET) public Objec
我正在尝试为 spring @RequestMapping 注释创建值属性以像这样映射 url /educationDistrict/308/action/resetAddressesForYear/
如何定义 @RequestMapping 方法来显式允许匿名(未经授权)访问? 以下方法不起作用,总是收到 401 Unauthorized: @RequestMapping("/test") @Se
想象一下我有一个像这样的 Spring MVC Controller : @Controller @RequestMapping("/base-url") public class MyControl
如何处理除资源映射文件夹之外的所有 URL 请求? 在这个例子中,我处理了所有路径(这就是我想要的),但现在即使我设置也无法访问资源文件夹 里面dispatcher-servlet.xml @Req
我需要有一个/jsp/login 和/jsp/login?error 的映射 请注意,我无法使用/jsp/login?error=someval 然后检查参数是否具有该值或默认值。在这种情况下,不允许
让 Tomcat 启动并自动显示 index.jsp 文件的内容没有任何问题。然而,当我尝试使用 @Controller 和 @RequestMapping 时,我遇到了错误。这是我的 Control
我有以下文件夹结构: WEB-INF/ -- 页数/ ----home.jsp ----admin.jsp ----admin2.jsp ----admin3.jsp 我有以下 Controller
我正在使用 Spring Boot,并尝试显示 get 请求。我正在使用 2 个类:Greeting.java 和 GreetingController: package greeting;
我正在尝试使用 @RequestMapping与 consumes -元素。阅读API-document它适用于 Content-Type - 请求头。但是,使用 @RequestMapping(co
我已经为此打开了一个问题 here.但我也想问一下 stackoverflow 的人。 @Controller @RequestMapping("/xxx") public class MyContr
我想在我的 Spring Boot 应用程序中创建一个自定义注释,它总是向我的类级别 RequestMapping 路径 添加一个前缀。 我的 Controller : import com.sage
鉴于下面的@Controller,即使我向 webApp 发送 Get 请求, Controller 也会运行 homePage 方法。 @RestController @RequestMapping
我正在尝试设置 Controller ,但不幸的是无法查看输出...一切都正确渲染。当我访问 http://localhost:8080/CMT/content/edit 时,我得到一个 404 页面
在我的 Spring Controller 中,我创建了 3 个方法。方法 1 和方法 2 工作正常,但方法 3 给我带来问题 问题: org.springframework.web.servlet.
我是一名优秀的程序员,十分优秀!