我持续得到这个错误,我在stackoverflow中看到过类似于这个的早期帖子,但是我无法弄清楚如何将commandName映射到我的控制器类。 commandName的真正用途是什么。
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'student' available as request attribute
Register.jsp
<form:form class="form-horizontal" style="margin-top: 45px"
action="registeration.do" method="post" commandName="student">
<div class="form-group row">
<div class="col-9 col-sm-offset-2 col-lg-8">
<form:input type="text" class="form-control" id="email"
placeholder="Enter email" path="email"/>
<form:errors path="email" cssClass="error" />
</div>
</div>
<div class="form-group row">
<div class="col-9 col-sm-offset-2 col-lg-8">
<form:input type="password" class="form-control" id="password"
placeholder="Enter password" path="password" />
<form:errors path="password" cssClass="error" />
</div>
</div>
<div class="form-group row">
<div class="col-9 col-sm-offset-2 col-lg-8">
<form:input type="text" class="form-control" id="name"
placeholder="Enter name" path="name"/>
<form:errors path="name" cssClass="error" />
</div>
</div>
StudentController.java
@Controller
public class StudentController {
@RequestMapping(value="registeration.do" ,method=RequestMethod.POST)
public ModelAndView submitAdmissionForm(@Valid @ModelAttribute("student") Student student,BindingResult result,ModelMap model)
{
System.out.println("Implementing the Validations");
Map <String,Object> modelMap=new HashMap<String,Object>();
modelMap.put("student", student);
if(result.hasErrors())
{
System.out.println("error is there");
return new ModelAndView("Registeration",modelMap);
}
else{
return new ModelAndView("AdmissionSuccess",modelMap);
}
}
}
}
答案 0 :(得分:0)
每当您想要将POJO对象与HTML表单链接时,您必须在返回ModelAndView对象时传递一个名称,并以HTML格式提供相同的名称(因为您使用了commandName =“student”)。
在你的情况下,你没有在返回ModelAndView对象时传递它,但是你在HTML表单中使用它,这就是它给你错误的原因。
你应该使用ModelMap来放置像“Registeration”这样的对象,并将返回的对象设为:
ModelAndView("jspname","student", new StudentBean());
和
new ModelAndView("AdmissionSuccess","student", new StudentBean());
请参考ModelAndView类的构造函数