aspose file tools*
The moose likes JSP and the fly likes Need some general help regarding MVC design. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Need some general help regarding MVC design." Watch "Need some general help regarding MVC design." New topic
Author

Need some general help regarding MVC design.

Harish Moolchandani
Greenhorn

Joined: Mar 12, 2010
Posts: 23
I've to create a website and I'm trying to create it in MVC pattern.
Below are my doubts:

1. The flow of MVC application:
I've a little confusion here correct me if I'm wrong. The user interacts with the view. The view calls a controller which interacts with the javabean and redirects to other view. Right?

2. Why to use a controller?
I know controller act as a bridge between model and view. It creates a javabean and redirect to a view. But why do we need it? I can create a javabean object using <jsp:useBean> which will be easy to use.

3. Is it good to add update, delete and insert functionality in a javabean.?
I've seen some MVC examples and javabean has a getJavaBean method to select and initialize the bean. But if we want to insert, update or delete in database can we use similar methods in that bean?

4. Validation in an MVC application.
I will create a tag for login using a Tag file. User will press the submit button and a login controller will be called. Controller will check whether user exists, if exists it redirects to its appropriate profile. But if user doesn't exist then it shows the error in that Login tag (Just like today's web applications). Is validation to be done in Login tag or in the controller.
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

Harish Moolchandani wrote:1. The flow of MVC application:
I've a little confusion here correct me if I'm wrong. The user interacts with the view. The view calls a controller which interacts with the javabean and redirects to other view. Right?
Mostly correct: beware saying it "redirects" to the other view - you're generally not doing an HTTP redirect, but doing a RequestDispatcher.forward(...) within the servlet. Unlike a redirect, the forward is handled completely server-side, which means your View can access anything in the original Request object

Harish Moolchandani wrote:2. Why to use a controller?
I know controller act as a bridge between model and view. It creates a javabean and redirect to a view. But why do we need it? I can create a javabean object using <jsp:useBean> which will be easy to use.
The controller can determine the most correct View to use, and can deal with error-handling before determining what View to serve. In a JSP & Servlet environment, the Controller Servlet interacts with any other libraries required, leaving the JSP template to focus solely on the HTML (or XML) View.

Harish Moolchandani wrote:3. Is it good to add update, delete and insert functionality in a javabean.?
I've seen some MVC examples and javabean has a getJavaBean method to select and initialize the bean. But if we want to insert, update or delete in database can we use similar methods in that bean?
You probably want a Data Access Object (DAO) - http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html - rather than a simple bean. Again, a Controller Servlet is better able to deal with a DAO than a basic JSP template.

Harish Moolchandani wrote:4. Validation in an MVC application.
I will create a tag for login using a Tag file. User will press the submit button and a login controller will be called. Controller will check whether user exists, if exists it redirects to its appropriate profile. But if user doesn't exist then it shows the error in that Login tag (Just like today's web applications). Is validation to be done in Login tag or in the controller.
It's up to you, of course. My preference would be to make the Controller responsible for validation, since it could then forward me to the appropriate view - be it a login screen, an error page, or the data I am trying to view. You could probably do it with a tag as well, but I am not as familiar with this technique.

The biggest reason I favor the MVC design is it is more "OOP", it is easier to couple with additional libraries (like DAO or LDAP as two common examples), and it keeps my JSP pages cleaner and more focused.


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Please read this article.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Harish Moolchandani
Greenhorn

Joined: Mar 12, 2010
Posts: 23
Thanks for your replies :)

Pete's explanation was really good. So here's I've decided to do...
For example I've to insert a new User.

1. There will be a User class which will be a javabean and only have getters and setters for its properties.
2. A DB2DAOFactory class which has methods to connect to DB2 database and also has factory methods for different DAO objects. (I don' think that I need a DAOFactory because I'll be only connecting to DB2).
3. An UserDAO interface which has methods declarations for insert,update, etc.
4. A DB2UserDAO class which implements this interface and all its methods.
5. If controller wants to create a new user, then
-- 5.1 it will first create a User object, set the appropriate properties from the details provided by user in request object.
-- 5.2 then it will retrieve a DB2UserDAO object from DB2DAOFactory class.
-- 5.3 and atlast it will call insertUser by passing in it the newly created User Object.
-- 5.4 Based on the return value it will redirect to a view.

Am I going in right direction or not?

Also I've one more question if i'm following this strategy
Entity is an Abstract class.
Person, Group, Center and Agency are interfaces which will inherit entity.
User is an Abstract class which implements Person and Group
And GeneralUser, Consumer, Admin and Dealer are classes which extends User.
Now if I want to insert a new Person in the Person table, then
1. I've to create another DAO DB2PersonDAO.
2. Controller will create a GeneralUser object and send it to the insertUser() of DB2UserDAO class.
3. The insertUser() will use insertPerson() of DB2PersonDAO class by sending the same user object it is using.
4. The insertPerson() method will retrieve details of Person inside this User Object and insert into the database?

Or it should be done in different way?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need some general help regarding MVC design.