Case 1 - Reading request parameters

Let's start with reading the request parameters with the help of the following steps:

  1. Create ReadMyBooks as a dynamic web application, and add all the required jars to it, as we did earlier.
  2. Each application has a home page. So, let's add index.jsp as a home page from the earlier application. You can copy and paste it directly.
  3. Copy the images folder from the earlier application.
  4. Add one more link to search the book based on the author's name, as follows:
<a href="searchByAuthor.jsp">Search Book By Author</a> 
  1. Let's add the searchByAuthor.jsp page facilitating the user to request for a list of books by entering the author's name as follows:
<body> 
  <center> 
    <img alt="bookshelf"src="images/img1.png"height="180" 
     width="350"> 
    <br> 
     <h3>Add the Author name to search the book List</h3> 
     <form action="/searchBooks.htm"> 
      Author Name:<input type="text"name="author"> 
      <br> 
      <input type="submit"value="Search Books"> 
    </form> 
  </center> 
</body>
  1. Add the configuration for DispachetServlet as the Front Controller in web.xml, as we did earlier. Name the servlet as books.
  2. Create or copy books -servlet.xml to configure handler mapping and other web component mappings from the earlier application.
  3. Add the configuration to scan the controllers using the context namespace.
  4. We need the Book bean to handle data to and from the controller. So, before developing the controller code, add Book.java to the com.packt.ch06.beans package from our earlier applications having the data members, as shown in the following code:
public class Book { 
  private String bookName; 
  private long ISBN; 
  private String publication; 
  private int price; 
  private String description; 
  private String author; 
  //getters and setters 
} 
  1. Now create a SearchBookController class as a controller in the com.packt.ch06.controllers package, and annotate it by @Controller.
  2. To search the books, add a method named searchBookByAuthor(), and annotate it by @RequestMapping for the searchBooks.htm file for mapping the URL. We can either use the Servlet API or Spring API, but we will use the Spring APIs here.
  3. Let's now add the code to searchBookByAuthor() for the following:
    • Reading request parameters
    • Searching the list of books
  1. Create an instance of ModelAndView to bind the book list as model data, logical model name, and logical view name together.

The code is as follows:

@Controller 
public class SearchBookController { 
@RequestMapping(value = "searchBooks.htm",method=RequestMethod.GET ) 
public ModelAndView searchBookByAuthor( 
@RequestParam("author") String author_name)  
  { 
    // the elements to list generated below will be added by      
    business logic  
    List<Book> books = new ArrayList<Book>(); 
    books.add(new Book("Learning Modular Java Programming",  
      9781235, "packt pub publication", 800, 
    "Explore the Power of Modular Programming ",  
      "T.M.Jog")); 
    books.add(new Book("Learning Modular Java Programming",  
      9781235, "packt pub publication", 800, 
    "Explore the Power of Modular Programming ",   
      "T.M.Jog")); 
    mv.addObject("auth_name",author); 
    return new ModelAndView("display", "book_list", books); 
  } 
} 

The @RequestParam annotation is used to read a request parameter and bind it to the method argument. The value of the author attribute is bound to the author_name argument without exposing the Servlet APIs. We have also used method=RequestMethod.GET, which denotes that the method is mapped for the HTTP get method. However, Spring 4.3 added the method level, which composed the flavors of the RequestMapping annotation. We will discuss them immediately after this demo.

Here, we have added a dummy list. Later on, it can be replaced by the actual code to get data from the persistence layer.

  1. It's time to configure ViewResolver, and package scanning in books -servlet.xml, as we did in the earlier application. We can copy paste books-sevlet.xml in WEB-INF from the earlier application.
  2. Create the jsps folder under WebContent, which will contain the .jsp pages.
  3. Create display.jsp in the jsps folder to display the list of books using the jstl tag, as shown in the following code:
<%@ taglib prefix="jstl" 
uri="http://java.sun.com/jsp/jstl/core"%> 
<html> 
  <head> 
    <meta http-equiv="Content-Type"content="text/html; 
    charset=ISO-8859-1"> 
    <title>Book List</title> 
  </head> 
<body> 
<center> 
  <img alt="bookshelf"src="images/img1.png" 
  height="180" width="350"> 
  <br> 
</center> 
<jstl:if test="${not empty book_list }"> 
<h1 align="center"> 
  <font color="green"> 
    Book List of ${auth_name } 
  <font> 
</h1> 
<table align="center" border="1"> 
<tr> 
  <th>Book Name</th> 
  <th>ISBN</th> 
  <th>Publication</th> 
  <th>Price</th> 
  <th>Description</th> 
</tr> 
<jstl:forEach var="book_data" 
  items="${book_list}"varStatus="st"> 
  <tr> 
  <td> 
    <jstl:out value="${ book_data.bookName }"> 
    </jstl:out> 
  </td> 
  <td> 
    <jstl:out value="${ book_data.ISBN }"> 
    </jstl:out> 
  </td> 
  <td>  
    <jstl:out value="${ book_data.publication }"> 
    </jstl:out> 
  </td> 
  <td> 
    <jstl:out value="${ book_data.price }"> 
    </jstl:out>
</td> <td> <jstl:out value="${ book_data.description }"> </jstl:out> </td> </tr> </jstl:forEach> </table> </jstl:if> <jstl:if test="${empty book_list }"> <jstl:out value="No Book Found"></jstl:out> </jstl:if> </body>

If the list doesn't have elements, there is no point to display such a list. The jstl:if tag is used to take the decision whether to display the list or not, and jstl:forEach is used to display the book information by iterating over the list. Run the application, and click on the link on the home page to get the form to enter author's name. If the author's name exists on submission of the form, we will get a list of books, as shown in the following screenshot:

Here, we have used @RequestParam to bind the individual request parameters to the method arguments. However, if the name of the request parameters matches the name of the method arguments, there is no need to use the annotation. The updated code can be written as follows:

@RequestMapping(value = "searchBooks.htm") 
public ModelAndView searchBookByAuthor( String author) { 
  List<Book> books = new ArrayList<Book>(); 
  books.add(new Book("Learning Modular Java Programming",  
    9781235, "packt pub publication", 800, 
  "explore the power of modular Programming ", author)); 
  books.add(new Book("Learning Modular Java Programming",  
    9781235, "packt pub publication", 800,  
  "explore the power of modular Programming ", author)); 
  ModelAndView mv = new ModelAndView("display", "book_list", books); 
  mv.addObject("auth_name",author); 
  return mv; 
} 

In this demo, we used @RequestMapping to map the incoming URL to a specific method. However, if you remember, we mentioned about the flavors of @RequestMapping added by Spring 4.3. Before moving ahead, let's discuss them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.128.129