I hope developers working in web application development might hear about MVC architecture. Almost all technologies provide support for MVC based web application development, but the success is based on many factors like reusable of the code, maintenance of the code, future adaption of the code, etc..,
The success of the Spring MVC is “Open for extension and closed for modification” principle. Using Spring MVC the developers can easily develop MVC based web application. We don’t need any steep learning curve at the same time we need to know the basics of spring framework and MVC architecture. The Spring MVC consists of following important components.
1. Dispatcher servlet
2. Controller
3. View Resolver
4. Model
2. Controller
3. View Resolver
4. Model
Spring MVC - Overview
The overall architecture of Spring MVC is shown here.
1. When “Dispatcher Servlet” gets any request from client, it finds the corresponding mapped controller for the request and just dispatches the request to the corresponding controller. The requests and corresponding controller information are loaded into the context when application starts based on xml configuration or Meta data. We have many way for loading this information, we will discuss at this one at later point of time.
2. We know, the controllers are used for connecting business and service layer. The controller get inputs from request, process the inputs, finally it returns the view and model information.
3. The view resolver gets those information, find the corresponding view, it updates the view with model data and finally it send the response back to the “Dispatcher Servlet”.
4. Finally the “Dispatcher Servlet” sends this response back to the client.
Ok, let’s start developing our application. I have created the dynamic web project using STS and included all necessary jar files into class path of the application.
2. We know, the controllers are used for connecting business and service layer. The controller get inputs from request, process the inputs, finally it returns the view and model information.
3. The view resolver gets those information, find the corresponding view, it updates the view with model data and finally it send the response back to the “Dispatcher Servlet”.
4. Finally the “Dispatcher Servlet” sends this response back to the client.
Ok, let’s start developing our application. I have created the dynamic web project using STS and included all necessary jar files into class path of the application.
Step 1: Defining Dispatcher Servlet into web.xml file.
Here “DispatcherServlet” class is configured with name as “dispatcher” and the url pattern as “*.spring”.
Step 2: Creating login.html page.
Here I have created the html form with action as “Login.spring”. If user submits the page, the request is send to Login controller. Let’s create a login controller for handling the request.
Step 3: Creating Login Controller.
@Controller annotation is very important because when context loads, the spring will find all controller class using this annotation and it's maps to the corresponding view (URL).
@RequestMapping annotation is used for specifying action of the request.
So now the dispatcher servlet gets any request from “Login.spring” that automatically redirects the request to “LoginController”.
@RequestParam annotation is used for getting value from request parameters. It’s equivalent to request.getParameter() method in servlet.
In my login controller, the processCredentials method just gets the username, password details from parameters and check whether both are equal. If it’s equal, it just returns the object of “ModelAndView” class with next redirecting page as a parameter.
Hey In my login form, I have given action as “Login.spring” only. How that action automatically trigger my processCredentials method?
@RequestMapping annotation is used for specifying action of the request.
So now the dispatcher servlet gets any request from “Login.spring” that automatically redirects the request to “LoginController”.
@RequestParam annotation is used for getting value from request parameters. It’s equivalent to request.getParameter() method in servlet.
In my login controller, the processCredentials method just gets the username, password details from parameters and check whether both are equal. If it’s equal, it just returns the object of “ModelAndView” class with next redirecting page as a parameter.
Hey In my login form, I have given action as “Login.spring” only. How that action automatically trigger my processCredentials method?
Red indicated “results” is indicates redirecting page, this is resolved by “View resolver”, and another one is request attribute key and value pair. Now we need to configure our view resolver.
Step 4: Configuring view resolver.
For configuring view resolver, we need to create one more xml file into inside the WEB-INF folder. The name of the xml file is very important, it’s is based on our dispatcher servlet name configured in web.xml file. The name should be “dispatcher servlet name-servlet.xml”. In our case, the “dispatcher” is our servlet name, so the name of the file is “dispatcher-servlet.xml”.
Spring have lot of view resolver classes, for example ResourceBundleViewResolver, InternalResourceViewResolver, XsltViewResaolver, etc.., but by default spring use InternalResourceViewResolver class.
In this XML file I have configured InternalResoureViewResolver class properties. Here I have given prefix value as “/” and suffix value as “.jsp”. In this situation, the view resolver get the view name from ModelAndView object (“results”) and append the prefix and suffix value (/results.jsp). So in our login controller we have verified user name, password values and redirected our request to results.jsp page. So we need to create results.jsp page.
Before going to create results.jsp, I have to explain about the purpose of context:component-scan tag. This tag automatically scans all the controller class presents in base-package and registered into context. In other words, it’s just looks what are all the classes have @Controller annotation and map the controller into corresponding view. This information is maintained in context of the application.
Ok let’s create a results.jsp page.
In this XML file I have configured InternalResoureViewResolver class properties. Here I have given prefix value as “/” and suffix value as “.jsp”. In this situation, the view resolver get the view name from ModelAndView object (“results”) and append the prefix and suffix value (/results.jsp). So in our login controller we have verified user name, password values and redirected our request to results.jsp page. So we need to create results.jsp page.
Before going to create results.jsp, I have to explain about the purpose of context:component-scan tag. This tag automatically scans all the controller class presents in base-package and registered into context. In other words, it’s just looks what are all the classes have @Controller annotation and map the controller into corresponding view. This information is maintained in context of the application.
Ok let’s create a results.jsp page.
Step 5: Creating result.jsp file.
This is my result.jsp page; here I have just get the message value from request attributes and displayed the message.
Step 5: Creating applicationContext.xml file.
Spring MVC application needs applicationContext.xml file. Using this file we can configure our web related beans. As of now we don’t use any beans, so this file don’t have any bean definition.
Ok. Let’s deploy our application into web server and will check the result.
As of now, we didn’t integrate hibernate. In my next part, I will explain the stpes for integrating Hibernate ORM framework into our application. The part1 application source code is uploaded into my GitHub repository for your reference.
For integrating into hibernate, refer the below link.
http://www.pointerunits.com/2012/11/simple-login-application-using-spring_12.html
For integrating into hibernate, refer the below link.
http://www.pointerunits.com/2012/11/simple-login-application-using-spring_12.html
Comments
I'm following your tutorial and I've an issue that I can't resolve.
When I add the lines related to
org.springframework.web.context.ContextLoaderListener
to web.xml, my tomcat app crashes.
In stderr logs I've this generic error: "Grave: Error listenerStart" and I can't understand what is the problem.
thanks
Giacomo
I hope, you have some problem in your class path setting and included JARS. So remove your all unwanted JARS included in your class path and then try. Here I have included some more reference links.
http://forum.springsource.org/showthread.php?39809-Spring-app-deployment-problem-(SEVERE-Error-listenerStart)
-Gnanasuriyan.
i am new to spring, when ever i am running u r application getting bellow error.
HTTP Status 404 - /WebApplication1/
not displayed index.html page.
I created a sample application as said in the above tutorial and ran the same. but Am getting Null Pointer Exception at 'List userObjs = hibernateTemplate.find("from user_data u where u.userName=? and u.password=?",userName,password);". That is the HibernateTemplate is null. I don't know what mistake i have done.
Please help me in resolving it.
Thanks.
AWS Training in Chennai
Do you want to play in an online casino? Try with us, do not be shy. perfect internet casino gambling There is such a stream of money that even your grandchildren will be enough)
Uipath Training in Chennai
AWS Training in Chennai
Blue Prism Training in Chennai
imedicalassistants
Guest posting sites
mobile application development training online
mobile app development course
mobile application development course
learn mobile application development
mobile app development training
app development training
mobile application development training
mobile app development course online
online mobile application development
Good discussion.
German Language Classes in Chennai
German Courses in Chennai
german language classes in bangalore
german courses in bangalore
german language classes in coimbatore
german coaching classes in coimbatore
german language course in madurai
RPA course in Chennai
Robotics Courses in Bangalore
RPA Course in Bangalore
Robotics Courses in Coimbatore
RPA Training in Coimbatore
Web Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement | Web Designing and Development Course in Chennai | Web Designer Training Course in Chennai
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai
Selenium Training in chennai | Selenium Training in anna nagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery
Digital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
This is enough for me. I want to write software that anyone can use, and virtually everyone who has an internet connected device with a screen can use apps written in JavaScript. JavaScript Training in Chennai JavaScript was used for little more than mouse hover animations and little calculations to make static websites feel more interactive. Let’s assume 90% of all websites using JavaScript use it in a trivial way. That still leaves 150 million substantial JavaScript Training in Chennai JavaScript applications.
Peoplesoft Admin Training
Cloud Business Management Software Suite
Best RPA Training in Pune
AWS Training in Pune
Top Business Lawyers in Delhi
Information Technology Lawyers in Delhi
Best Corporate Lawyers in Delhi
Selenium Course in Chennai
Selenium Online Course
Selenium Course in Coimbatore
Visit-
Digital Marketing Course in Dubai
If you are interested in learning digital marketing, here is a complete list of the best online digital marketing courses with certifications. In this article, you will learn about digital marketing and its different strategies, the need for doing digital marketing, the scope of digital marketing, career opportunities after doing online digital marketing, and many more.
Visit-
Online Digital Marketing Courses
Visit- Digital Marketing Courses in Ahmedabad
Do check out into the Digital Marketing Courses in Delhi to know more about the topic.
Digital Marketing Courses in Delhi
A Guide with Tips and Steps you need before Starting your Freelancing Journey.
In our blog we shared a guide with Tips and Steps about What is Freelancing and How does it work? This will help people to take a good decision before starting a Freelancing Journey. Read now:
What is Freelancing
Visit- home tuitions in mumbai
Visit- Anchor ias coaching in Hyderabad
Digital marketing courses in Nagpur
Data Analytics Courses in Ghana
Courses after bcom
financial modelling course in kenya
financial modelling course in bangalore
Data Analytics Jobs
Data Analytics VS Data Science
CA Coaching in Mumbai