I am trying to bind the user input using @ModelAttribute
annotation when the user wants to add a new book in the database (book table).
Book class has four properties(int bookId, String title, Author author, Publisher publisher
). User just enters the title and then select the author and publisher from drop-down lists. When the form is submitted, just title value comes to the model object in the controller and the author and publisher are null.
I present my form and request handler method in below. Can anybody say where is the problem in my form or maybe in my controller class?
addBook.jsp
<form action="addBookExecution" method="post">
<table>
<tr>
<td>Enter Book's Name:</td>
<td><input type="text" name="title"/></td>
</tr>
<tr>
<td>Select Author: </td>
<td><select name="author">
<option>****select author****</option>
<c:forEach var="author" items="${authorList}">
<option value="${author}">${author.authorName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>Select Publisher: </td>
<td><select name="publisher">
<option>****select publisher****</option>
<c:forEach var="publisher" items="${publisherList}">
<option value="${publisher}">${publisher.publisherName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>Click here to submit the form:</td>
<td><input type="submit" value="submit"/></td>
</tr>
Controller:
@RequestMapping(value = "/addBookExecution", method = RequestMethod.POST)
protected ModelAndView addBookExecution(@ModelAttribute Book book)
throws ClassNotFoundException, SQLException {
bookService.addBook(book);
ModelAndView model = new ModelAndView("adminFunctionsPage");
model.addObject("Msg","Your request has been processed successfully.");
return model;
}