SpringMVC controller: how to stay on page if form validation error occurs

前端 未结 3 1838
野性不改
野性不改 2020-12-30 15:45

I have next working code in my SpringMVC controller:

@RequestMapping(value = \"/register\", method = RequestMethod.GET)
public void registerForm(Mod         


        
相关标签:
3条回答
  • 2020-12-30 16:18

    *I'm using Hibernate Validator APIs to validate my beans. To preserve form data along with displaying error messages, you need to do these 3 things:

    1. Annotate your bean (eg. @NotEmpty, @Pattern, @Length, @Email etc.)

    enter image description here

    1. Inside controller:

      @Controller public class RegistrationController {

      @Autowired
      private RegistrationService registrationService;
      
      @RequestMapping(value="register.htm", method=RequestMethod.GET, params="new")
      public String showRegistrationForm(Model model) {
          if (!model.containsAttribute("employee")) {
              model.addAttribute("employee", new Employee());
          }
          return "form/registration";
      }
      
      @RequestMapping(value="register.htm", method=RequestMethod.POST)
      public String register(@Valid @ModelAttribute("employee") Employee employee, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
          if (bindingResult.hasErrors()) {
              redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.employee", bindingResult);
              redirectAttributes.addFlashAttribute("employee", employee);
              return "redirect:register.htm?new";
          }
          registrationService.save(employee);
          return "workspace";
      }
      // ....
      

      }

    2. Update your view/jsp to hold error messages:

    enter image description here

    This article can surely be helpful.

    0 讨论(0)
  • 2020-12-30 16:24

    You can change your POST implementation to this:

    @RequestMapping(value = "/buy/{buyId}", method = RequestMethod.POST)
    public String buyPost(@PathVariable long buyId,
                              @Valid @ModelAttribute("buyForm") BuyForm buyForm,
                              BindingResult result) {
    
        buyForm.setId(buyId); // important to do this also in the error case, otherwise, 
                              // if the validation fails multiple times it will not work.
    
        if (result.hasErrors()) {
            byForm.setId(buyId);
            return "/buy/{buyId}";
        }
    
        buyService.buy(buyForm);
        return "redirect:/show/{buyId}";
    }
    

    Optionally, you can also annotate the method with @PostMapping("/buy/{buyId}") if you use Spring 4.3 or higher.

    0 讨论(0)
  • 2020-12-30 16:43

    I tried the solution metioned in this post at this weekend, but it doesn't work for BindingResult.

    The code below works but not perfect.

    @ModelAttribute("command")
    public PlaceOrderCommand command() {
        return new PlaceOrderCommand();
    }
    
    @RequestMapping(value = "/placeOrder", method = RequestMethod.GET)
    public String placeOrder(
            @ModelAttribute("command") PlaceOrderCommand command,
            ModelMap modelMap) {
        modelMap.put(BindingResult.MODEL_KEY_PREFIX + "command",
                modelMap.get("errors"));
        return "placeOrder";
    }
    
    @RequestMapping(value = "/placeOrder", method = RequestMethod.POST)
    public String placeOrder(
            @Valid @ModelAttribute("command") PlaceOrderCommand command,
            final BindingResult bindingResult, Model model,
            final RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            redirectAttributes.addFlashAttribute("errors", bindingResult);
    
            //it doesn't work when passing this          
            //redirectAttributes.addFlashAttribute(BindingResult.MODEL_KEY_PREFIX + "command", bindingResult);
    
            redirectAttributes.addFlashAttribute("command", command);
            return "redirect:/booking/placeOrder";
        }
        ......
    }
    
    0 讨论(0)
提交回复
热议问题