View RSS Feed

My Java Tips

MVC - View

Rate this Entry
by , 11-19-2011 at 05:00 PM (1209 Views)
This post will briefly explain MVC view. I assume that you have some basic knowledge about MVC.

There are many ways in which data can be accepted from the user and presented to the user in an application where data has to be accepted from the user, has to be updated/modified and has to presented back in a proper way to the user again.

The component View consists of a presentation logic. It presents the data contained in the Model to the user. Further, it also allows the user to interact with the system and notifies Controller about the users actions.

As the component Model is different from the component View, there can be multiple Views for the same Model (data). With component View you can get separation of presentation from the content.

In order to understand the Model 2 MVC architecture, let's see a simple example of online website for shopping books.

I will use an example for better understanding.

EShop.jsp is the main View, or presentation logic for our online website for shopping books. There is no processing logic within the JSP page itself. Please note that the page deals only with presenting the main user interface of the application to the client, and performs no processing whatsoever. It is just responsible for retrieving any objects or beans that may have been previously created by the servlet, and extracting the dynamic content from that servlet for insertion within static templates. Please also note that the JSP page Cart.jsp is included within EShop.jsp. Cart.jsp is used for the the presentation of the session based shopping cart. If the cart is empty or not yet created it shows nothing.

Java Code:
<%@ page session="true" %>

 Shop Books Online
<hr />

<form name="shoppingForm"></form>   action="/examples/servlet/ShoppingServlet"
<select name="BOOK">  <option>The Complete Reference Java | Herbert Schildz | 45.00</option>  <option>Programming in C | Dennis Ritchie | 55.00</option>  <option>SCJP | Khalid Moghul | 35.00</option>  <option>.NET | Peter Norton | 65.00</option>  <option>Datastructures| Tenenenbaum | 35.00</option> </select>

 <strong>Quantity: </strong>
<input name="qty" size="3" value="1" type="text" />
<input name="action" value="ADD" type="hidden" />
<input name="Submit" value="Add to Cart" type="submit" />


 <jsp:include page="Cart.jsp" flush="true">


Java Code:
&lt;%@ page session="true" import="java.util.*, shopping.BOOK" %&gt;
 Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
 if (buylist != null &amp;&amp; (buylist.size() &gt; 0)) {

  for (int index=0; index &lt; buylist.size();index++) {
   BOOK anOrder = (BOOK) buylist.elementAt(index);

    &lt;% } %&gt;
<table width="100%" border="0" cellpadding="0">
<td><strong>&lt;%= anOrder.getBook() %&gt;</strong></td>
<td><strong>&lt;%= anOrder.getAuthor() %&gt;</strong></td>
<td><strong>&lt;%= anOrder.getPrice() %&gt;</strong></td>
<td><strong>&lt;%= anOrder.getQuantity() %&gt;</strong></td>
<td><form name="deleteForm"></form>    action="/examples/servlet/ShoppingServlet"
<input value="Delete" type="submit" />
<input name="delindex" value="&lt;%= index %&gt;" type="hidden" />
<input name="action" value="DELETE" type="hidden" /></td>
<form name="checkoutForm"></form>    action="/examples/servlet/ShoppingServlet"
<input name="action" value="CHECKOUT" type="hidden" />
<input name="Checkout" value="Checkout" type="submit" />

&lt;% } %&gt;

Submit "MVC - View" to Facebook Submit "MVC - View" to Digg Submit "MVC - View" to Submit "MVC - View" to StumbleUpon Submit "MVC - View" to Google