I know that this question has been asked and answer on different ways, but with Spring everything is different.
I am new with Spring 4 - MVS and just want to create my first MVC - HelloWorld implementation and got the error
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.get
My web.xml looks like this:
Archetype Created Web Application HelloWorld org.springframework.web.servlet.DispatcherServlet 1 HelloWorld / contextConfigLocation /WEB-INF/HelloWorld-servlet.xml org.springframework.web.context.ContextLoaderListener My HelloWorld-servlet.xml like this:<context:component-scan base-package="com.programcreek.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
My student.jsp looks like this:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>
<h2>2 Student Information 2</h2>
<form:form method="POST" action="/HelloWorld/addStudent" >
<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>
and my StudentController.java as follow:
package com.programcreek.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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;
@Controller
@RequestMapping("/")
public class StudentController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command2", new Student());
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("HelloWorld")Student student,
ModelMap model) {
System.out.println("Controller2");
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "result";
}
}
I always read a lot of example and transfer sample codes to my eclipse to try out, but for this case of student which has been taken from an old example (Spring 2.4).
Thank you for helping me to figure out the problems. (I know about the spring-documentation)