View RSS Feed

My Java Tips

MVC Model

Rate this Entry
by , 11-19-2011 at 06:18 PM (1012 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, 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 representing the selection, and goes about updating the shopping cart object before placing it back within the session.
Java Code:
package shopping;
public class BOOK {
  String book;
  String author;
  float price;
  int quantity;

  public BOOK() {
  public void setBook(String title) {
  public String getBook() {
    return book;
  public void setAuthor(String a) {
  public String getAuthor() {
    return author;
 public void setPrice(float p) {
  public float getPrice() {
    return price;
  public void setQuantity(int 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.

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

Java Code:
  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);

<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><strong>$&lt;%= amount %&gt;</strong></td>
<a href="/examples/jsp/shopping/EShop.jsp">Shop some more!</a>
Error page
Java Code:
  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 Submit "MVC Model" to StumbleUpon Submit "MVC Model" to Google