How MVC web application works in java? [closed]


<h3>As we define view,controller,service and Dao layer in application then how data flow in between them.</h3> e.g. suppose we add struts,spring,hibernate,etc. jars in project then how they work together?


The MVC pattern itself does not describe how should you implement your web application. What it describes is how your components should interact with each other in order to achieve a modular architecture with replaceable components.

The pattern is explained in details in Martin Fowler's <a href="http://rads.stackoverflow.com/amzn/click/0321127420" rel="nofollow" title="Patterns of Enterprise Application Architecture">POEAA</a> and in <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="nofollow" title="Wiki article on MVC">Wikipedia</a>. More info about MVC can be found in <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="nofollow" title="Wiki article on MVC">Wikipedia</a>

<strong>A simple example using Java, Spring and Hibernate</strong>

In this case Spring MVC provides a pluggable framework where you can define your models , controllers and views without coupling them together too tightly (this is achieved through <a href="http://en.wikipedia.org/wiki/Inversion_of_control" rel="nofollow" title="Wiki article on Inversion of Control">IOC</a>/<a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow" title="Wiki article on Dependency Injection">DI</a>).

The first thing to notice is the <a href="http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html" rel="nofollow" title="Spring Dispatcher Servlet API documentation">DispatcherServlet</a> which is a regular servlet that serves as an entry point by handling all the incoming HTTP requests and routing them to their respective <em>controllers</em>. The appropriate controller is looked up by their mappings, eg. via <a href="http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html" rel="nofollow" title="@RequestMapping annotation API documentation">@RequestMapping</a> annotations.

The controller's responsibility is to determine what actions should be performed as a response to the incoming request. This is usually done by checking headers, parameters, session info, path for information what the user wanted to do. Here is an extremely simple example:

if (session.getAttribute("authenticated") == false) { // we need to redirect to the login page } else { // everything was fine, so we do some business logic in the model importantService.doSomethingReallyImportant(productOrder) }

Then the control is passed to the <em>model</em> layer where business logic happens. This may include any operation that changes the model's state, like updating a password, registering a booking, clearing transactions, and so on. In web applications these operations often involve the use of a persistence API, eg. Hibernate.

public class ImportantService { public void doSomethingVeryImportant(final ProductOrder order) { // Here we define the business operation getCurrentBasket().add(order); // An additional pseudo-persistence operation getSession().update(order); } }

In practice when the model has finished then control is returned to the <em>controller</em> which decides how to update the <em>view</em> (eg. redirecting the browser or just simply display a result page) where the user sees the result of his/her action.


  • How to know the file formats supported by Databricks?
  • WordPress: Override parent theme class function in child theme
  • Custom Swing look and feel vs. custom components?
  • C++/CLI: inherit from one CLR class, multiple C++ classes
  • Which layer should create DataContext?
  • Why does a Binary Heap has to be a Complete Binary Tree?
  • REST Communication between microservices - Abstract away from host or port in REST call
  • Dependency injection, delayed injection praxis
  • So we've got MEF working with MVC4, how about the convention based model?
  • Unusual 'Headers already sent' error. No whitespace nor changing of the header
  • junit: how to avoid false positives when using forkMode=“once”?
  • How to change the scope of an existing binding in Ninject
  • Dependency Injection - Choose DLL and class implementation at runtime through configuration file
  • Help designing a order manager class
  • Unit test express route calls controller method?
  • Play 2.0.4 file upload. NullPointerException: null
  • Rails XML builder not rendering
  • Dependable views in Ember
  • Slicing an SPA into several components and use AngularJS
  • Should a web service response include empty values?
  • Making Google Visualization - Annotation Chart to work in GWT
  • Django model for a Postgres view
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • Combining two different ActiveRecord collections into one
  • how does System.Web.HttpRequest::PathInfo work?
  • Scala multiline string placeholder
  • Does Mobilefirst provide a provision to access web services directly?
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Typescript - Unable to get 'import' statement to function
  • How to do unit test for HttpContext.Current.Server.MapPath
  • How to rebase a series of branches?
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Traverse Array and Display in markup
  • KeystoneJS: Relationships in Admin UI not updating
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Why joiner is not used after Sequence generator or Update statergy