What causes “java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute”?

前端 未结 5 916
独厮守ぢ
独厮守ぢ 2020-11-22 03:06

This is meant to be an extensive canonical question & answer post for these types of questions.


I\'m trying to write a Spring MVC web applicati

5条回答
  •  猫巷女王i
    2020-11-22 03:57

    I had this error on a screen with multiple forms that do a search. Each form posts to its own controller method with results shown on same screen.

    Problem: I missed adding the other two forms as model attributes in each controller method causing that error when screen renders with results.

    Form1 -> bound to Bean1 (bean1) -> Posting to /action1
    Form2 -> bound to Bean2 (bean2) -> Posting to /action2
    Form3 -> bound to Bean3 (bean2) -> Posting to /action3
    
    @PostMapping
    public String blah(@ModelAttribute("bean1") Bean1 bean, Model model){
    // do something with bean object
    
    // do not miss adding other 2 beans as model attributes like below. 
    model.addAttribute("bean2", new Bean2()); 
    model.addAttribute("bean3", new Bean3());
    return "screen";
    }
    
    @PostMapping
    public String blahBlah(@ModelAttribute("bean2") Bean2 bean, Model model){
    // do something with bean object
    // do not miss adding other 2 beans as model attributes like below. 
    model.addAttribute("bean1", new Bean1()); 
    model.addAttribute("bean3", new Bean3());
    return "screen";
    }
    
    @PostMapping
    public String blahBlahBlah(@ModelAttribute("bean3") Bean3 bean, Model model){
    // do something with bean object
    // do not miss adding other 2 beans as model attributes like below. 
    model.addAttribute("bean1", new Bean1()); 
    model.addAttribute("bean2", new Bean2());
    return "screen";
    }
    

提交回复
热议问题