View RSS Feed

My Java Tips

MVC Model

Rate this Entry
by , 11-19-2011 at 06:18 PM (728 Views)
The Model is the one which is responsible for managing data storage as well as retrieval of the data from the data source. When data changes it notifies all its Views about change in data. A model holds part of the current state of the application. The model represents enterprise data and the business rules that govern access to and updates of this data. The Model knows everything about the data that need to be displayed. However, it does not know anything about the View, that is the manner in which the data is to be displayed, nor the View actions that are used to manipulate the data. The data is accessed and manipulated through methods that are independent of the View.


With MVC architecture there can be change in component Model as well as component View without affecting each other. This obviously increases maintainability as well as extensibility of the system.

The model is independent of the user interface. The component Model return Results, Verify Results of Processing, provides Semantic Processing, perform Function or Pass to Semantic Process or Storage Component. Model also represents data centric classes encapsulating problem domain objects. Each class represents roughly to the rows of a database table. Model objects can be constructed from a ResultSet of a database query, from user input, or from user request parameters.



In our example of online website for shopping books, every time the user adds a book within EShop.jsp, the request is posted to the controller servlet, ShoppingServlet.java. The servlet in turn determines the appropriate action, and then processes the request parameters for the item to be added. It then instantiates a new Book bean BOOK.java representing the selection, and goes about updating the shopping cart object before placing it back within the session.

BOOK.java
Java Code:
package shopping;
public class BOOK {
  String book;
  String author;
  float price;
  int quantity;

  public BOOK() {
    book="";
    author="";
    price=0;
    quantity=0;
  }
  public void setBook(String title) {
    book=title;
  }
  public String getBook() {
    return book;
  }
  public void setAuthor(String a) {
    author=a;
  }
  public String getAuthor() {
    return author;
  }
 public void setPrice(float p) {
    price=p;
  }
  public float getPrice() {
    return price;
  }
  public void setQuantity(int q) {
    quantity=q;
  }
  public int getQuantity() {
    return quantity;
  }
}
Checkout.jsp gets the shopping cart from the session and the total amount for the request, and then shows the selected items and their total cost.

Checkout.jsp
Java Code:
&lt;%@ page session="true" import="java.util.*, shopping.BOOK" %&gt;</pre>
Shop Books Online Checkout
<hr />

<center>
Java Code:
 &lt;%
  Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
  String amount = (String) request.getAttribute("amount");
  for (int i=0; i &lt; buylist.size();i++) {
   BOOK anOrder = (BOOK) buylist.elementAt(i);
 %&gt;

 &lt;%
  }
  session.invalidate();
 %&gt;
<table width="100%" border="0" cellpadding="0">
<tr>
<td><strong>BOOK</strong></td>
<td><strong>AUTHOR</strong></td>
<td><strong>PRICE</strong></td>
<td><strong>QUANTITY</strong></td>
<td>&nbsp;</td>
</tr>
<tr>
<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>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><strong>TOTAL</strong></td>
<td><strong>$&lt;%= amount %&gt;</strong></td>
<td>&nbsp;</td>
</tr>
</table>
<a href="/examples/jsp/shopping/EShop.jsp">Shop some more!</a>
 </center>
Error page
error.html
Java Code:
<h1>
  Sorry, there was an error! 
  Please try <a href="/examples/jsp/shopping/EShop.jsp">again</a>.</h1>

Submit "MVC Model" to Facebook Submit "MVC Model" to Digg Submit "MVC Model" to del.icio.us Submit "MVC Model" to StumbleUpon Submit "MVC Model" to Google

Comments