Hello world with Spring MVC 3.2 – Spring framework tutorial

I have written hello world using Spring web app so many times that I think its about time I share it on my blog.

STEP 1: Create new webapp project, I will use maven for this. (Note: I am on my macbook with Maven 3 and Java 6 installed.)

mvn archetype:generate \
-DgroupId=com.manishchhabra.blog \
-DartifactId=HelloSpringMVC \
-DarchetypeArtifactId=maven-archetype-webapp \
-DinteractiveMode=false

You could create the maven project directly in your IDE as well. But I usually create it on the terminal and import it in eclipse by using the following command (Note: The following command is run within your newly created project directory, i.e. run -> cd HelloSpringMVC)

mvn eclipse:eclipse -Dwtpversion=2.0

(Note: If this is the first time you are creating a maven webapp project, maven will download a lot of jar files into your local maven repository).

STEP 2: Add Spring Framework 3.2 dependencies to your pom.xml

STEP 3: Update your web.xml (src/main/webapp/WEB-INF/web.xml) to use Spring’s DispatcherServlet

STEP 4: Create a spring controller (HelloSpringController.java) in the new source directory src/main/java

STEP 5: Create a JSP Page in the folder WEB-INF/jsp called output.jsp

STEP 6: Add your spring configuration dispatcher-servlet.xml into the WEB-INF folder (includes jsp page resolver and component-scan of your base package)

STEP 6: That’s it! Its time to run your project. You could either run directly from eclipse or you could run “mvn package” to build a war file and deploy it to your application server. I tested this on tomcat running on port 8080 (http://localhost:8080/HelloSpringMVC/hello), gave me a message from my Spring Controller. Working! yeah 🙂

You can view or download the full project code at https://github.com/manishchhabra/HelloSpringMVC

Cheers

  • Hi, i have basic knowledge of servlets , jsp and beans.
    I have developed few applications using core servlets and jsp.
    What i need to understand spring mvc. what are the pre-requirements for spring mvc? do i need to work on core spring or directly i can start with spring mvc module?? i have an interview within 4 days from now

    • Vijay Bhatt

      You can directly start Spring MVC.

  • Miki

    HI, what is the filename for step 4 and 6?

    • Manish

      Hi Miki, thanks for pointing that out, I have updated the blog post accordingly.

      File name in step 4 is HelloSpringController.java
      File name in step 6 is dispatcher-servlet.xml

      Also you can get the full source code at https://github.com/manishchhabra/HelloSpringMVC

  • Yoesoff

    the best spring mvc hello wolrd tutorial in the world 🙂

  • zauberberg51

    on Windows, the mvn archetype:generate command needs to be all on one line, so remove the slashes

    • Manish

      Thanks. I will update the post.

  • zauberberg51

    what version of the Spring Framework did you use for this? I am finding that Spring 3 is very brittle with respect to framework upgrades. I like your approach to the tutorial.

  • suganthan

    Thank you… I am working around lot of tutorials finally this is one helped me. Is a relief to me.

  • Thoms

    thank you, finally one that worked!! 🙂

  • suganthan

    On the other day It worked fine, but today it is showing this warning
    org.springframework.web.servlet.DispatcherServlet noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/HelloSpringMVC/hello] in DispatcherServlet with name ‘dispatcher’

    • suganthan

      As well I am using the same downloaded project without changing anything

      • Chris

        Doesn’t work for me, either.

  • Bubba

    This is really cool! So many spring mvc tutorials have too much unnecessary bloated code that makes is difficult to see the basics. This gets rid of the cruft, it will definitely be in my bookmarks!

    • Manish

      Thanks Bubba.

  • Madhu

    Very clean explanation!!

  • sillynutz

    found this gem while i was learning spring, at first it doesn’t make much sense, but after reading a lot more spring and revisited this for the 2nd time, it makes a lot more sense and the example really couldn’t be simpler. I also got this error when imported the project to eclipse luna, maven clean
    org.springframework.web.servlet.DispatcherServlet noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/HelloSpringMVC/hello] in DispatcherServlet with name ‘dispatcher’

    for whatever reason it is not importing correctly. i manually use maven to create an webapp, added the dependencies to pom, mod the build path to remove the missing src/main/java and readded it back to the project. it works right after that. hope this helps someone down the line

  • Rajapriya

    great and useful blog to all. after reading this blog i got some useful things from it

    best java training