I have a controller that goes to the DB and get a list of books. I like to use @modelAttribute to display individual books in separate form so I can do quick edits on each item.
Controller
@RequestMapping("/")
public String pagIndex(Model model){
System.out.println("loading index page");
// Get list of books
List<Book> books = bookDao.getBookList();
// List of object to Model
model.addAttribute("books", books);
return "index";
}
View
<h3>Book Inventory</h3>
<table>
<tr>
<td>ID</td>
<td>Book Name</td>
<td>ISPN</td>
<td>Price</td>
<td></br><td>
<td>Object</td>
</tr>
<!-- Each book in separate form for easy update -->
<c:forEach var="book" items="${books}">
<tr>
<sf:form class="editeForm" action="${pageContext.request.contextPath}/edititem" modelAttribute="book" method="POST">
<td>${book.id} <input type="hidden" path="id" name="id"></td>
<td><sf:input type="text" path="name" name="name" /></td>
<td><sf:input type="text" path="ispn" name="ispn" /></td>
<td><sf:input type="text" path="price" name="price" /></td>
<td></br><td>
<td>${book}</td>
<td><input type="submit" value="save edite"><input type="submit" name="delete" value="delete"></td>
</sf:form>
</tr>
</c:forEach>
</table>
When i set modelAttribute="book" stack trace is
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'book' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
modelAttribute="${book}" stack trace is
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'Book bean [id=1, name=Java: A Beginner's Guide, ispn=978-0071809252, price=18' available as request attribute
Update: If I use plain JSTL it works but anyway to use modelAttriburte?
<h3>Book Inventory</h3>
<table>
<tr>
<td>ID</td>
<td>Book Name</td>
<td>ISPN</td>
<td>Price</td>
<td></br><td>
<td>Object</td>
</tr>
<!-- Each book in separate form for easy update -->
<c:forEach var="book" items="${books}">
<tr>
<form class="editeForm" action="${pageContext.request.contextPath}/edititem" method="POST">
<td>${book.id} <input type="hidden" name="id" value="${book.id}"/></td>
<td><input type="text" name="name" value="${book.name} /></td>
<td><input type="text" name="ispn" value="${book.ispn} /></td>
<td><input type="text" name="price" value="${book.price} /></td>
<td></br><td>
<td>${book}</td>
<td><input type="submit" value="save edite"><input type="submit" name="delete" value="delete"></td>
</form>
</tr>
</c:forEach>
</table>
POSTmethod for your index?