|
|
MVC stands for Model-View-Controller. Spring MVC framework provides this architecture and components. Using this MVC framework, loosely coupled web applications are developed with ease. MVC pattern is designed such a way that it separates the input logic, business logic and UI logic.
All HTTP requests and responses are handled by DispatcherServlet which is designed by Spring MVC framework. Following diagram illustrates the request processing workflow of DispatcherServlet.
Below are the events in sequence which corresponds to an HTTP request to DispatcherServlet.
Requests are mapped using a URL mapping in the web.xml file which DispatcherServlet wants to handle. Below example shows the declaration and mapping for HelloWeb DispatcherServlet example:
WebContent/WEB-INF directory of the web application will have web.xml file. Framework will load the application context from file which has a name structure [servlet-name]-servlet.xml which is under WebContent/WEB-INF directory upon initialization of HelloWeb DispatcherServlet. In the example, file name will be HelloWeb-servlet.xml.
DispatcherServlet handles the URLs which are mentioned in <servlet-mapping> tag. In this example, all the HTTP requests which are ending with .jsp are handled by HelloWeb DispatcherServlet.
We can even customize the default file name and location which are [servlet-name]-servlet.xml and WebContent/WEB-INF respectively by adding servlet listener i.e., ContextLoaderListener in web.xml file as below-
Keeping the points together, let us see the required configuration for our example HelloWeb-servlet.xml file that is placed in web application’s WebContent/WEB-INF directory:
Below are the considerable points about HelloWeb-servlet.xml file -
DispatcherServlet assigns the incoming HTTP request to the controllers. The annotation @Controller specifies a particular class which fulfill the role of a controller. To map a URL to an entire class or a particular handler method, the annotation @RequestMapping is used.
The annotation @Controller specifies the class as a Spring MVC Controller. Next, the @RequestMapping mentions the all handling methods on this controller which are relative to /hello path. We declare printHello() method as default service method to handle next annotation @RequestMapping(method=RequestMethod.GET) HTTP GET request. We have the feasibility to define other method for handling any POST request to the same URL.
We can rewrite the above controller to add additional attributes in @RequestMapping as below-
From the above, the URL to which handler method is mapped is indicated by value attribute. The method attribute on the other hand specifies the method, which handle the HTTP GET request. Few important points regarding the controller defined in above example-
There are many types of views for presentation technologies by Spring MVC which are HTML, Excel worksheets, PDF, JSPs, XSLT, JSON, Velocity templates, Atom and RSS feeds etc. Most commonly used are JSP which are written with JSTL. Let us create a hello view in /WEB-INF/hello/hello.jsp:
In Controller, we have setup ${message} as the attribute. We can define many attributes inside our view to display.
|
|
Spring MVC Framework Related Tutorials |
|
---|---|
J2EE Tutorial | Hibernate Tutorial |
MVC Framework Tutorial | Framework7 Tutorial |
Maven Tutorial | JUnit Tutorial |
Log4j Tutorial |
Spring MVC Framework Related Practice Tests |
|
---|---|
J2EE Practice Tests | Hibernate Practice Tests |
Maven Practice Tests | JUnit Practice Tests |
Log4j Practice Tests | Spring MVC Framework Practice Tests |
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.