When you use jsp spring tag <form:form>
<form:form method="POST" action="../App/addCar"> <table> <tr> <td><form:label path="brand">Name</form:label></td> <td><form:input path="brand" /></td> </tr> <tr> <td><form:label path="year">Age</form:label></td> <td><form:input path="year" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit" /> </td> </tr> </table> </form:form>
you should write:
@RequestMapping(value = "/car", method = RequestMethod.GET) public ModelAndView car() { return new ModelAndView("car", "command", new Car()); }
Because the spring framework expects an object named "command". The default command name used to bind command objects is "command". This name is used when binding the created command class to the request.
http://static.springsource.org/spring/docs/1.2.9/api/org/springframework/web/servlet/mvc/BaseCommandController.html
But when you use the html form <form> , you can write:
@RequestMapping(value = "/car", method = RequestMethod.GET) public ModelAndView car() { return new ModelAndView("car", "YOUR_MODEL_NAME", new Car()); }
But on your page
<form method="POST" action="../App/addCar"> <table> <tr> <td><form:label path="YOUR_MODEL_NAME.brand">Name</form:label></td> <td><form:input path="YOUR_MODEL_NAME.brand" /></td> </tr> <tr> <td><form:label path="YOUR_MODEL_NAME.year">Age</form:label></td> <td><form:input path="YOUR_MODEL_NAME.year" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit" /> </td> </tr> </table> </form>
Dimus source share