Neither BindingResult nor plain target object … Exception

前端 未结 1 630
死守一世寂寞
死守一世寂寞 2021-01-25 12:04

Yes, I read it is pretty common problem, but reading those post did not really help me.

The short story is that I wanna submit a form on showAllComments

1条回答
  •  有刺的猬
    2021-01-25 12:40

    You need to add a your form bean class ie Comment, as an attribute to your model when you show the showAllComments.jsp in the logincontroller.

    @RequestMapping(value = "/log_in", method = RequestMethod.POST)
    public ModelAndView tryToLogin(@RequestParam("uName") String uName, @RequestParam("pW") String pW,      HttpServletResponse response, HttpServletRequest request) {
        ModelAndView ret = new ModelAndView("login", "command", new User());
        User user = userService.existingUser(uName, pW);
        loggedInUser = new User();
        model.addAttribute("command", new Comment());
        if (user != null) {
            Map model = new HashMap();
                model.put("COMMENTS", allComments);
                model.put("LOGGED_IN_USER", loggedInUser);
            ret = ModelAndView("showAllComments", model);
        }
        return ret;
    }
    

    This should work fine.

    UPDATE

    And it is not a good practise to use 'command' as the command object name. For the class comment you can use a 'comment' or something like that. If your doing that, Update your form with the following code.

    
        
    COMMENT

    Do the same change at all other places, viz

    model.addAttribute("comment", new Comment());
    

    and

    @ModelAttribute("comment")
    

    UPDATE 2

        @RequestMapping(value="userRegistration", method = RequestMethod.GET)
    public ModelAndView showUserRegistrationForm(Model model){
        model.addAttribute("user", new AccountDetailsForm());
        return new ModelAndView("userRegistration");
    }
    

    0 讨论(0)
提交回复
热议问题