Java Reference
In-Depth Information
resolver to identify which view should be returned to the user. In this case, the BookController
returns a ModelAndView object named bookList . The fragment of the view resolver in
bookstore-servlet.xml (from Listing 5-41) is shown here:
17. <beans:bean
18. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
19. <beans:property name="prefix" value="/WEB-INF/views/" />
20. <beans:property name="suffix" value=".jsp" />
21. </beans:bean>
Based on the definition, the view resolver finds the file using the following mechanism:
Prefix + ModelAndView name + suffix, which translates to : /WEB-INF/jsp/bookList.jsp
ModelAndView.addObject("bookList", bookService.getBookList()) adds the book data returned by
getBookList() to the model named bookList , which is formatted and rendered by the view.
Finally, the servlet engine renders the response via the specified JSP illustrated in Listing 5-43.
Listing 5-43. View
1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
2. <%@ taglib prefix="c" uri=" http://java.sun.com/jsp/jstl/core " %>
3. <!DOCTYPE html>
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7. <title>Your Book store</title>
8. </head>
9. <body>
10. <h1>Books List</h1>
11. <table border="1">
12. <tr>
13. <th align="left">Author</th>
14. <th align="left">Book Title</th>
15. </tr>
16. <c:forEach items="${bookList}" var="book">
17. <tr>
18. <td>${book.author.authorName}</td>
19.
20. <td>${book.bookTitle}</td>
21. </tr>
22. </c:forEach>
23. </table>
24. </body>
25. </html>
Figure 5-20 illustrates the directory structure of the bookstore application.
 
Search WWH ::




Custom Search