Java Reference
In-Depth Information
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>
22. </beans:beans>
23.
As soon as the user requests a list of books using http://localhost:8080/bookstore , the request
hits the servlet engine, which routes the call to the bookstore web app, which is deployed in the
servlet container. The web.xml file shown in Listing 5-40 provides the welcome file that should serve
the request.
21. <welcome-file-list>
22. <welcome-file>/list_book.html</welcome-file>
23. </welcome-file-list>
The URL in the welcome file matches the URL pattern that has been registered for DispatcherServlet ,
and the request is routed to it. Based on the configuration available in bookstore-servlet.xml , the
request is routed to a specific controller, illustrated in line 12 of Listing 5-41. Here the list_book.html
file is declared as a bean and mapped to the BookController class. This means if a URL with
/list_book.html is requested, it will ask the BookController to handle the request. Listing 5-42
illustrates the interface-based BookController . Later you will see how to replace this interface-based
controller with an annotated controller.
Listing 5-42. Interface-Based Controller for the Bookstore Application
1. package com.apress.bookstore.controller;
2.
3. import javax.servlet.http.HttpServletRequest;
4. import javax.servlet.http.HttpServletResponse;
5.
6. import org.springframework.web.servlet.ModelAndView;
7. import org.springframework.web.servlet.mvc.Controller;
8.
9. import com.apress.bookstore.service.BookService;
10.
11. public class BookController implements Controller{
12.
13. @Override
14. public ModelAndView handleRequest(HttpServletRequest arg0,
15. HttpServletResponse arg1) throws Exception {
16. BookService bookservice = new BookService();
17. ModelAndView modelAndView = new ModelAndView("bookList");
18. modelAndView.addObject("bookList", bookservice.getBookList());
19. return modelAndView;
20. }
21. }
The controller instantiates the BookService that is responsible for returning the required book data.
ModelAndView("booklist") calls the view named bookList by passing bookList to Spring's view
 
Search WWH ::




Custom Search