java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'category' available as request attribute

后端 未结 3 2025
醉梦人生
醉梦人生 2020-12-10 07:28

I looked almost all answers related this problem on the web but could not figure out the problem in my code.

Here is my JSP page.



        
3条回答
  •  悲&欢浪女
    2020-12-10 07:54

    If you're getting to index.jsp through something like http://localhost:8080/yourapp, I'll assume you have a for it.

    This means that the index.jsp generates the HTML without any pre-processing by Spring. You're trying to render this

    
         
          
    
    

    where is from Spring's tag library. First, note that you are using both commandName and modelAttribute. This is redundant. Use one or the other, not both. Second, when you specify either of these, the tag implementation looks for a HttpServletRequest attribute with the name specified. In your case, no such attribute was added to the HttpServletRequest attributes. This is because the Servlet container forwarded to your index.jsp directly.

    Instead of doing that, create a new @Controller handler method which will added an attribute to the model and forward to the index.jsp view.

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String welcomePage(Model model) {
        model.addAttribute("category", new Category()); // the Category object is used as a template to generate the form
        return "index";
    }
    

    You can get rid of this

    
    
    

    Also, move any mvc configuration from your applicationContext.xml file to your servlet-context.xml file. That's where it belongs. Here's why.

提交回复
热议问题