Neither BindingResult nor a regular target for the bean name available as a request attribute - Spring MVC

I get this error

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

This is a stack trace.

org.apache.jasper.JasperException: java.lang.IllegalStateException: neither a BindingResult, nor a regular target for bean name 'command', available as a request attribute org.apache.jasper.servlet.JspServletWrapper.handleJspException (JspServletWrapper.java<34) org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:452) org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java data java: 333) javax.servlet.http.HttpServlet.service (HttpServlet.java:722) the main reason is java.lang.IllegalStateException: neither the BindingResult nor the regular target for the bean name 'command', available as an org.springframework request attribute. web.servlet.support.BindStatus. (BindStatus.java:141) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus (AbstractDataBoundFormElementTag.java:178) org.springframework.webFleTataBataTataBataTataBormTataBormTataFmentBataTformBormTamentTaFaTTBeTFmeTataBformTamentTaFaTBeTFentBamentTamentTaFaTBeTTBemeTataBformTaFaTBeFTaFaTBemeTataBeformateataform ) org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor (LabelTag.java:129) org.springframework.web.servlet.tags.form.LabelTag.resolveFor ​​(LabelTag.java:119) org.springframework .web.servlet.tags.form.LabelTag.writeTagContent (LabelTag.java:89) org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal (AbstractFormTag.java:102) org.springframework.web.servlet .RequestContextAwareTag.doStartTag (RequestContextAwareTag.java:79) org.apache.jsp.student_jsp._jspx_meth_form_005flabel_005f0 (student_jsp.java:182) org.apache.jsp.student_jspth_j_fjj_jsp_fx_jsp.jf.jsp_fx_jsp.jf.jsp.jf.jsp.jf.jsp_fx_j_xj_fj ._jspService (student_jsp.java:79) org.a pache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:70) javax.servlet.http.HttpServlet.service (HttpServlet.java:722) org.apache.jasper.servlet.JspServletWrapper.service (JspServlet org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.javahaps89) org.apache.jasper.servlet.JspServlet.service (JspServlet.javahaps33) javax.servlet.http.HttpServlet.service (HttpServlet. 722)

This is my web.xml file

 <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Form Handling</display-name> <welcome-file-list> <welcome-file>student.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/HelloWeb-Servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 

code>

This is my applicationContext.xml file

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.Prime" /> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/JSP/" /> <property name="suffix" value=".jsp" /> </bean> </beans> 

code>

This is my student.jsp file

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC Form Handling</title> </head> <body> <h2>Student Information</h2> <form:form modelAttribute="SpringWeb" method="POST" action="/HelloWeb/addStudent" commandName="SpringWeb"> <table> <tr> <td><form:label path="name">Name</form:label></td> <td><form:input path="name" /></td> </tr> <tr> <td><form:label path="age">Age</form:label></td> <td><form:input path="age" /></td> </tr> <tr> <td><form:label path="id">id</form:label></td> <td><form:input path="id" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit"/> </td> </tr> </table> </form:form> </body> </html> 

code>

And this is my StudentController.java file

 package com.Prime; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.ui.ModelMap; @Controller public class StudentController { @RequestMapping(value = "/student", method = RequestMethod.GET) public ModelAndView student() { return new ModelAndView("/student.jsp", "SpringWeb", new Student()); } @RequestMapping(value = "/addStudent", method = RequestMethod.POST) public String addStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model) { model.addAttribute("name", student.getName()); model.addAttribute("age", student.getAge()); model.addAttribute("id", student.getId()); return "result"; } } 

code>

Can someone help me with this error .... Thanks in advance

+6
source share
3 answers

I tried your configuration and I did not get your error. But I had to change something.

ViewResolver adds .jsp to your view. Therefore IMHO you have to change

  return new ModelAndView("/student.jsp", "SpringWeb", new Student()); 

to

  return new ModelAndView("student", "SpringWeb", new Student()); 

So, remove jsp and "/" in front of the student.

Then it worked for me. But, as I said, I did not get your mistake

+3
source

Edit:

It seems to me that your exceptions specify a "bean name " command . I think you have a paperwork problem because the code you posted here does not match the exception.


Remove the commandName attribute from the form tag, the modelAttribute attribute modelAttribute enough:

 <form:form modelAttribute="SpringWeb" method="POST" action="/HelloWeb/addStudent" > 

After reading this article, this suggests that this is not a solution to your problem, but in any case you do not need to specify them twice.

+3
source

The jsp location accessed by the /student call is

 /WEB-INF/JSP//student.jsp.jsp 

Change the configuration to match the corresponding URL.

0
source

All Articles