Creating the View

The view resolver, configured in the WEB-INF/jboss-as-spring-mvc-context.xml web application context, resolves View from the ModelAndView object, and the response is rendered with a catalog.jsp view template. Create catalog.jsp in the WEB-INF/views directory. To create a JSP, select File | New | Other, and in New, select Web | JSP File and click on Next. In the New JSP File wizard, select the WEB-INF/views folder, specify File name as catalog.jsp, and click on Next, as shown in the following screenshot. Subsequently click on Finish.

Creating the View

The catalog.jsp class and the other classes added, which include the controller class, the DAO classes, and the model class, are shown in Project Explorer in the following screenshot:

Creating the View

The request initiates from the catalog.jsp view template, and the response is rendered with catalog.jsp. To send a request to DispatcherServlet, the <form/> element has the action attribute set to catalog, which gets mapped to the /catalog path URI specified using the @RequestMapping annotation in the CatalogController request handler class. The createAndDisplayCatalog handler method gets invoked when the form is posted. In catalog.jsp, instantiate a bean from the org.jboss.springmvc.data.CatalogDao interface with the scope request. Add <form/> with <input/> elements for input fields to create a new Catalog object. To render the response from a org.jboss.springmvc.data.CatalogDao bean, invoke the getAllCatalogs() method to obtain List of Catalog objects. Iterate over List to output Catalog instance properties.

The catalog.jsp is listed in the following code:

<%@page contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="org.jboss.springmvc.model.Catalog,org.jboss.springmvc.data.CatalogDao,org.jboss.springmvc.controller.CatalogController"%>
<jsp:useBean id="catalogDao" type="org.jboss.springmvc.data.CatalogDao" scope="request" />
<!--scope should be request for the bean to be found  -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Spring MVC-JPA Catalog</title>
<head>
    <meta name="generator" content="HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />
    <title>
      Spring MVC-JPA Catalog
    </title>
  </head><!-- action should be same as the mapping in the controller -->
  <body>
    <h2>
      Catalog form
    </h2>
    <form method="post" action="catalog">
      Journal: <input type="text" name="journal" />Publisher: <input type="text" name="publisher" />Edition: <input type="text" name="edition" />Title: <input type="text" name="title" />Author: <input type="text" name="author" />
      <table>
        <tr>
          <td></td>
        </tr>
        <tr>
          <td></td>
        </tr>
        <tr>
          <td></td>
        </tr>
        <tr>
          <td></td>
        </tr>
        <tr>
          <td></td>
        </tr>
      </table>
      <p>
        <input type="submit" value="Add" />
      </p>
    </form>
    <table>
      <%
        for (Catalog catalog : catalogDao.getAllCatalogs()) {
      %>
      <tr>
        <td>
          <%=catalog.getId()%>
        </td>
        <td>
          <%=catalog.getJournal()%>
        </td>
        <td>
          <%=catalog.getPublisher()%>
        </td>
        <td>
          <%=catalog.getEdition()%>
        </td>
        <td>
          <%=catalog.getTitle()%>
        </td>
        <td>
          <%=catalog.getAuthor()%>
        </td>
      </tr><%
        }
      %>
    </table>
  </body>
</html>
..................Content has been hidden....................

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