Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2
Free Online Struts Tutorials,Struts Programming Tutorials,Struts notes
GeekMantra
Search Site

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2
J2SE [Java 2 Standard Edition]
Core Java Introduction
Exception Handling
Collection & Util Classes
Multithreading
Input & Output Streams
Applet
Adwance Windows Toolkit (AWT)
Swings (Adwance GUI)
Event Handling
Java Networking
Remote Method Invocation (RMI)
Java Database Connectivity (JDBC)
J2EE [Java 2 Enterprise Edition]
Java 2 Enterprise Edition Intoduction (J2EE)
Server Side Programming (Servlet)
Java Server Pages (JSP)
Enterprise Java Bean
MVC Framework (Struts)
Advance Framework (Springs)
Object-relational mapping (Hibernate)
eXtensible Markup Language (XML)
Asynchronous JavaScript and XML (AJAX)
Miscellneous
Monthly News Letter
Free SCJP Samples
Interviews Questions For Freshers
Interviews Questions For Experinced
Most Visited Links
Most Freaky Links
Advertisements

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2

Introduction to Model-View-Controller (MVC)

There is a big difference between a school page and a professionally developed Web site. The page designer (or
HTML developer) must understand colors, the customer, product flow, page layout, browser compatibility,
image creation, JavaScript, and more. Putting a great looking site together takes a lot of work, and most Java
developers are more interested in creating a great looking object interface than a user interface. Java Server
Pages (JSP) technology provides the glue between the page designer and the Java developer.
If you have worked on a large-scale Web application, you understand the term change. Model-View-Controller
(MVC) is an approach put together to help control change. MVC decouples interface from business logic and
data. Struts is an MVC2 implementation that uses Servlets and JSP tags, from the J2EE specifications, as part
of the implementation.

Model-View-Controller (MVC)
MVC helps resolve some of the issues with the single module approach by dividing the problem into three
categories:
Model
    The model contains the core of the application`s functionality. The model encapsulates the state of the application.
View
    The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing         about the controller. The view should be notified when changes to the model occur.
Controller
    The controller reacts to the user input. It creates and sets the model.

MVC Model 1

MVC1

The Model 1 architecture is very simple. A request is made to a JSP or servlet and then that JSP or servlet handles all responsibilities for the request, including processing the request, validating data, handling the business logic, and generating a response.

Consequences of the single-page approach


Heavy HTML and Java coupling
The coder of the JSP file must be both a page designer and a Java developer. The result is often either terrible Java code or an ugly page, or sometimes both.
Java and JavaScript blur
As the pages become larger, there can be a tendency to implement some JavaScript. When the JavaScript appears in a page, the script can get confused with the Java code. An example of a possible point of confusion is using client-side JavaScript to validate the email field.
Embedded flow logic
To understand the entire flow of the application, you have to navigate all of the pages. Imagine the spaghetti logic on a 100-page Web site.
Debugging difficulties
In addition to being ugly to look at, HTML tags, Java code, and JavaScript code all in one page makes it difficult to debug problems.
Tight coupling
Changes to business logic or data means possibly touching every page involved.
Aesthetics
Visually, in large pages, this type of coding looks messy.

JSP Tags: No more Java code in my HTML
A JSP tag is simply a way of abstracting out code from a JSP file.

Intro MVC struts

JSP tags solved only part of our above listed problems. We still have issues with validation, flow control, and updating the state of the application. This is where MVC2 comes to the rescue.

MVC Model 2


MVC 2

In this MVC architecture, a central servlet, known as the Controller, receives all requests for the application. The Controller then processes the request and works with the Model to prepare any data needed by the View (which is usually a JSP) and forwards the data to a JSP. The JSP then uses the data prepared by the Controller to generate a response to the browser. In this architecture, the business and presentation logic are separated from each other. Having the separation of business and presentation code accommodates multiple interfaces to the application, be they Web, wireless, or GUI (Swing). Additionally, this separation provides excellent reuse of code.

Note – Model 1 (single page approach) and Model 2 (true MVC approach), so don’t be confuse if you asked for MVC model 1, you need to explain single page approach.

Struts Framework

What a framework means?
• an extensible/generic/flexible architecture following set of standards and patterns
• Focuses on a specific domain (web / j2ee applications etc)
• Provides extension points
• Can be customized and used on projects
What is Struts Framework?
• Provide APIs those are used to create web applications
• Uses Java Beans, Java Servlets, and Java Server pages (JSP)
• Based on the MVC design pattern
• Rich set of tag libraries provided for presentation tier
• Extensible

Struts Framework

Struts API Zoo

Sturts API

• Most of the times we will extend ActionForm for making input page form beans and Action class for
putting the business logic like we did in servlet.
• We can customize controller parts also.
• Most of the API and tags works in internationalized fashion, so they will take data to display as a key
and we will define these key-values into properties file. So by default our application will be
multilingual.
• We can define our validation rules on user inputs into xml, means highly configurable and reusable
validation code.
• We can break our look and feel into tiles and each tile can be configured through xml, means making the
presentation configurable.
• We define the flow inside the struts-config.xml that makes our application flow highly configurable.
So waiting for what this is the right time to write HelloWorld…..

HelloWorld Setup

hello world

hello world

hello world

For accessing the application type following URL in your browser
http://localhost:8080/hello/welcome.do

1. Since the request ends at .do so the ActionServlet will get called. This servlet works as Front
Controller.

2. ActionServlet will take out /welcome from /welcome.do and look for mapping
3. it will found direct forward rule and will forward the request to /hello.jsp
4. when you submit the hello.jsp page in action it calls /hello.do
5. again step 1 and 2 will be repeated
6. Now this time it founds an action class and form associated with this action.
7. instantiate the form bean object, called the setter method for properties name matched and then called
the validate method which returns ActionErrors object
8. it check this object if empty then execution goes to HelloAction execute() method else it forward the
request to input="/hello.jsp"
9. in execute we cast the form, fetch the value and then added into request
request.setAttribute("message", "Your Message " + msg + " received!!!");
execute() returns ActionForward that hold the logical name of the page to be forwarded.
<forward name="success" path="/success.jsp"></forward>
And finally we see the success.jsp as a final output.

hello world