Spring MVC Tutorials 05 – Creating first Spring MVC Web Application using Eclipse IDE (01)

http://www.gontu.org In this tutorial, we will learn how to create a simple web Application using Spring MVC framework. While developing this basic spring MVC web Application; I will explan everything right from front controller (Dispatcher Servlet), Handler Mapping, Controller till the View Resolver and the actual View.

I would recommend going through the Introduction to Spring MVC framework tutorial (02) before hitting this demo.

27 Comments on “Spring MVC Tutorials 05 – Creating first Spring MVC Web Application using Eclipse IDE (01)”

  1. Quick suggestion, when you are talking about a particular part of the code, it would be helpful if you zoom in into that part, I use glasses so sometimes it is hard to read the small letters.

  2. Just a suggestion

    It was important to show where that will get it <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean id = "HandlerMapping" class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> for those who learn this, so you can use any version

  3. Thank you your tutorial very helpful for me but i got an error when i run on server (Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]) dont know why this error comes please help

  4. @0:08 Review of previous lessons.
    @0:44 Declaration of what will come:
    "[…]create an ultra simple web application using Spring MVC"
    @0:54 Diagram of workflow that will be used for this tutorial.

    @1:46"Technically, I just need to follow…"
    MS#1. Modify web.xml file
    MS#2. Create spring-dispatcher-servlet.xml (Aka Front Controller)
    MS#3. Create the HelloController.Java class ( A controller)
    MS#4. Create the HelloPage.jsp file ( A View )

    @2:00 Points to Web.xml file that needs to be modified for STEP#1.
    @2:06 Auto-Generated code on screen.
    @2:07 WEB-INF / Web.xml (Where Web.xml should be)
    @2:072:28:If you want servlets, you MUST have Web.xml file here!
    @2:282:38 The main purpose of Web.xml is to:
    Map the input of your request to an appropriate servlet
    that can serve that request.
    @2:392:58"Why am I telling you all this?"
    Answer: Spring MVC framework uses servlet technology as it's base.
    @2:59 Another reminder: Put Web.xml in WEB-INF ALWAYS.
    @3:093:12 You will need to put up THIS code.
    (Pastes code into window of web.xml)
    PASTEBIN LINK to code transcribed off of screen:
    @3:163:27 Explanation of code:
    Tells servlet technology all your url requests to the web application
    should be mapped to the SpringMVC framework for further processing.
    @3:283:44  Extended Explanation of code:
    Map any URL request to your front controller named "DispatcherServlet"
    Mentions how "Front Controller" is synonymous with "Dispatcher Servlet"
    @3:453:56  Extended-Extended Explanation of code:
    Front controller will take complete responsibility of:
        1. Processing url request.
        2. Sending back response to the client browser.
    @3:564:02" With the help of… All it's helpers around it."
    Goes back to DIAGRAM first seen at 0:54

    @4:044:19  "In order to make front controller work…"
    "In order to make the front controller work and take charge"
    "of the entire application, we will need to provide all relevant information"
    "to it in a file called springDispatcherServlet.xml"
       @4:13"All relevant information to it"
       @4:154:19"A file called Spring Dispatcher Servlet.xml"
    @4:20 SpringDispatcherServlet.xml is ALSO in WEB-INF folder with Web.xml
    @4:424:32: Declares 4:20 to be MS#2 (Main Step #2 from my notes)
    @4:33 Right-Clicks on WEB-INF folder in project explorer.
    @4:354:38 New –> File (in the context menu made @4:33)
    @4:394:41  Name it: "spring-dispatcher-servlet.xml" then click "Finish"
    "I will quickly provide all relevant information in this file that the front controller"
    "would need to manage this application. And later I will explain the code."
    @4:575:00"You see here… these sections"
    @5:015:06"Each Section is conveying specific information to the front controller"

    @5:065:23     SECTION#1 START
                           Handler Mapping
    @5:065:12"This section tells the front controller what handler mapping to use."
    @5:125:14"hand? the input url" (hard time understanding a word)
    @5:15[email protected]5:22
    "and tell the front controller which data generated class to be called for further
    processing that request.

    @5:24 SECTION #3 START: (yes, 3 before 2)
                        View Resolver
    The view resolver tells us the exact location of the view to be used in the project.

    @5:333:42 SECTION #2 START:
    This is the controller of the data generated class that you are seeing
    in the diagram first seen at 0:54

    @5:41"Before we go further into explaining" this anymore, lets get onto steps 3&4
    @5:52 Main steps are shown back on screen.
    @5:53 nice arrows showing Controller + View location in project.
    In the next tutorial, we will go over what the code we just wrote does.
    Along with running the application on Apache Tomcat server.

  5. Thank you your tutorials are very helpful,
    Can you make tutorial on play MVC framework 

  6. Thank you so much sir. It gives me the clear basic idea of creating spring MVC in eclipse.

  7. Ok, can someone tell me how, and where exactly did we specify that all URL requests should be sent to get processed by spring-dispatcher-servlet.xml?
    I think this should be specified in web.xml but I don't know exactly in which line and how. 

  8. Hi Ankush,

    I am getting following warning. Any idea?
    INFO: Server startup in 2919 ms
    Feb 15, 2015 11:03:59 PM org.springframework.web.servlet.PageNotFound noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/Spring1MVCWeb/] in DispatcherServlet with name 'Spring1MVCWeb'

  9. Can some one tell me why I am getting "The hierarchy of the type HelloController is inconsistent" in hellocontroller while extending to abstractclass.

  10. Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException

  11. Feb 12, 2015 6:01:11 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    INFO: Refreshing org[email protected]77eec5fe: startup date [Thu Feb 12 18:01:11 IST 2015]; root of context hierarchy
    Feb 12, 2015 6:01:11 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [SpringConfig.xml]
    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [SpringConfig.xml]; nested exception is java.io.FileNotFoundException: class path resource [SpringConfig.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.spring.TestSpringProject.main(TestSpringProject.java:10)
    Caused by: java.io.FileNotFoundException: class path resource [SpringConfig.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
    … 13 more

Comments are closed.