The MVC Design Pattern in Java EE (Eng Ver)

MVC - Java EE (Eng Ver)
Introduction

MVC is a design pattern (a general design solution for problem solving) widely used in software design. 
MVC Stands for Model Controller View. 
MVC is achieved through 3 components: 

  • the model contains the data and provides methods for accessing them;
  • The view displays the data in the model;
  • the controller receives the commands(usually through the view) and implement them by changing the status of the other two components

This pattern ensures the division between business logic(managed by the model) and user interface (run by and view controller). 

Wanting to apply this pattern to the Java EE, you can use the following Java technologies applied to various components of the pattern. 

Model: This component can be implemented through the entity bean and session bean 
Controller: It can be implemented by the servlet. 
View: The latter through jsp and / or jsf. 

The entity bean will be an abstraction and then to represent the data, the session bean can perform operations on the entity, 
the servlet will collect input from jsp (you) to make requests to the session bean and communicate results to jsp and so on. 

Example: 

You have to implement a web application that allows you to view / edit the data in a table "users" in a db. 

Table: Users 
The columns: id, name, first name 

Insert into table one data row in order to have at least one default user. 

Suppose you use a persistence provider, for example hibernate 
For this element(users table) you can create an entity bean. 
Make a session bean with entitymanager that realize the methods "edit" and "find." 

  • edit - Will carry out the update users compared to the database using the entitymanager method merge.
  • find - made an id, returns the entity bean that represents the data in the table that are in line with id indicated.
  • .. You can also implement the methods "create", "erase" to complete.

By implementing this portion of the project, it gets the "Model" 

We implement a servlet(the servlet user) that calls through resource injection, @EJB, the session bean. 
Then by invoking the servlet model and using the method find(1) you can get the equivalent entity containing the data with id = 1; 

If the servlet receives the GET or POST parameter "id", "name" and "surname" then perform the upgrade db, creating a new entity with the data passed through http parameters and calling the method of the edit session bean. 
(Note that when you leave the entity EJB components, which are used for example, web components, entering into a state-managed ..) 

The servlet in this case the controller realized. 

The servlet article in question sets the request attribute "user" setting the panel obtained from the user session bean and using a RequestDispatcher performs a forward to a jsp that takes care to show a html form that allows the reading and editing of data. 
The modification done by calling the servlet users start moving their appropriate values. 

This short article does not have the presumption to be exhaustive, as implied in various concepts and technologies .. may be the point of departure for an approach to MVC in Java EE. 

For a complete example click here

2 comments:

Mohamed Shaheen said...

please note that entity beans are no longer exist , thy have been replaced by Java persistence API

Giuseppe Morreale said...

See at
http://java.sun.com/javaee/6/docs/api/index.html

search for javax.persistence in the upperleft frame. click on it.

in the bottomleft frame search for entity annotation.

So You read the java ee 6 javadoc from sun.com site. As you can see Entity is supported too in the javax.persistence api.

Why you say that no longer exist?!
Perhaps you refer to "entity bean" instead of "entity class" terminology? If so, I correct the post replacing entity bean simply with entity!