design principle in Spring Web MVC and in Spring in general is the “Open for extension, closed for modification” principle.
Please follow detailed tutorial get in-depth knowledge: Spring MVC Hello World Tutorial
Also if you have below questions then you are at right place:
- spring framework modules
- best way to download spring framework
- spring framework interview questions
- why use spring framework
- spring framework mvc
- spring framework in java
- advantages of spring framework
The Spring Web
model-view-controller (MVC) framework is designed around a
DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files. The default handler is based on the
@RequestMapping annotations, offering a wide range of flexible handling methods. With the introduction of Spring 3.0, the
@Controller mechanism also allows you to create
RESTful Web sites and applications, through the
@PathVariable annotation and other features.
In Spring Web MVC you can use any object as a command or form-backing object; you do not need to implement a framework-specific interface or base class. Spring’s data binding is highly flexible: for example, it treats type mismatches as validation errors that can be evaluated by the application, not as system errors.
Thus you need not duplicate your business objects’ properties as simple, untyped strings in your form objects simply to handle invalid submissions, or to convert the Strings properly. Instead, it is often preferable to bind directly to your business objects. Spring’s view resolution is extremely flexible.
Controller implementation can even write directly to the response stream. Typically, a
ModelAndView instance consists of a view name and a model
Map, which contains bean names and corresponding objects such as a command or form, which contain reference data. View name resolution is highly configurable, through bean names, a properties file, or your own
The model (the
M in MVC) is based on the
Map interface, which allows for the complete abstraction of the view technology. You can integrate directly JSP, Velocity, or any other rendering technology. The model
Map is simply transformed into an appropriate format, such as JSP request attributes or a Velocity template model.
To summarize, this is a simple flow:
- The client sends a request to web container in the form of http request.
- This incoming request is intercepted by
Front controller(DispatcherServlet) and it will then tries to find out appropriate
- With the help of Handler Mappings, the DispatcherServlet will dispatch the request to appropriate Controller.
- The Controller tries to process the request and returns the Model and View object in form of
ModelAndViewinstance to the Front Controller.
- The Front Controller then tries to resolve the View (which can be JSP, Freemarker, Velocity etc) by consulting the
- The selected view is then rendered back to client.
Configuring Spring 5.X MVC
As we saw in previous Spring MVC HelloWorld Example, DispatcherServlet is an entry point to MVC3. Here is a sample:
<servlet> <servlet-name>crunchify</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>crunchify</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
Once the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml in WEB-INF folder of web application. In above example, the framework will look for file called