Java Spring Mvc Tutorial Example

Spring MVC tutorial - Crunchify Tutorial

Model-View-Controller (MVC) architecture provides the simplest way to develop flexible and loosely coupled web applications.

In this tutorial we will go over on how to create your 1st Spring MVC application with very detailed steps.

Do you have any of below questions?

  • Fundamentals of Spring MVC 5.2.3
  • Developing a Spring Framework MVC 5 application step-by-step..
  • java – Spring MVC tutorial from the scratch
  • Spring MVC Fast Tutorial
  • Spring MVC Framework Tutorial
  • First Spring MVC application tutorial
  • Spring 5.x MVC Tutorials, AJAX Demo, jQuery Demo, JavaScript Demo, Tips & Tricks Spring 5 MVC

Then you are at right place. Here I'll demonstrate simpleSpring MVC framework for building web applications.

First thing first – Let's Setup Environment

Tutorial last updated and verified: September 2020 with below Tool versions.

I'm using below tools which you may need to download if you don't have already.

  1. Tomcat 9.0.38 – Download latest Apache Tomcat from this link.
  2. Make sure you download Eclipse IDE forJava EE Developers (2020‑09 R) – Download link. (diagram below)
  3. Spring 5.2.9.RELEASE (No download required) – we will use Maven dependency.
  4. JDK 15 – Download link.
Simplest Spring MVC tutorial by Crunchify.com

Make sure you download latest Java EE Developer Kit:

Eclipse Photon IDE 4.8.0

Main Goal

Main goal for this tutorial to create Spring MVC Application in the simplest way.


Before we get started let's see what we will see at the end of this tutorial 🙂

This is how our application result will look like. This is a final result once you complete all below steps.

Here is a final result: Welcome page ==> index.jsp

CrunchifySpringMVCTutorial index.jsp page

Result returns from Controller Class 🙂

Message Coming from Controller

Now Let's get started on Tutorial

Step-1

  • Open Eclipse
  • Create New Eclipse Workspace – This is must to avoid any existing workspace config issue.
Create New Eclipse Workspace - Crunchify Tips

Step-2

  • Click on File
  • Click on New
  • Choose Dynamic Web Project
  • One popup window, Provide Project Name: CrunchifySpringMVCTutorial
  • Make sure you use Target Runtime as Apache Tomcat 9.0
    • If you don't see Target Runtime then follow these steps
  • Choose Configuration as Default Configuration
Create Crunchify Spring MVC Dynamic Web Project

Step-3

Convert Project to Maven Project to add all required Spring MVC dependencies to project.

Steps:

  • Right click on project
  • Configure
  • Convert to Maven project
Convert Dynamic Web Project to Maven Project
Convert CrunchifySpringMVCTutorial to Maven project

Step-4

Open pom.xml file and add below jar dependencies to project.

Crunchify Spring MVC pom.xml Dependencies

NOTE: Here is my pom.xml file. Make sure you update Java version to 13 if you haven't yet moved to JDK 13. We will keep updating this tutorial to latest Spring MVC version.

So below pom.xml file may have different (latest) version of Spring MVC dependencies than above image 🙂

Step-5

Create new Spring Configuration Bean file:/WebContent/WEB-INF/crunchify-servlet.xml

Create crunchify-servlet.xml file - Spring MVC project

crunchify-servlet.xml

In the above crunchify-servlet.xmlconfiguration file, we have defined a tag <context:component-scan> . This will allow Spring to load all the components from packagecom.crunchify.controller  and all its child packages.

This will load our CrunchifyHelloWorld.class . Also we have defined a bean viewResolver. This bean will resolve the view and add prefix string /WEB-INF/jsp/  and suffix .jsp to the view in ModelAndView.

Note that in our CrunchifyHelloWorld class, we have return a ModelAndView object with view name welcome.

This will be resolved to path /WEB-INF/jsp/welcome.jsp .

Step-6

Create new file web.xml if it's already not there.  Map Spring MVC in/WebContent/WEB-INF/web.xml file.

NOTE: if you don't see web.xml file in your "dynamic web project" then follow these steps.

web.xml

The above code in web.xml will map DispatcherServlet with url pattern /welcome.jsp. Also note that we have define index.jsp as welcome file.

One thing to note here is the name of servlet in <servlet-name> tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml  in WEB-INF folder of web application. In this example, the framework will look for file called crunchify-servlet.xml.

Step-7

Create Controller Class.

  • Right click on Java Resources -> src
  • Click New -> Class
  • Package: com.crunchify.controller
  • Filename: CrunchifyHelloWorld.java
Create Spring Controller Class CrunchifyHelloWorld

CrunchifyHelloWorld.java

Note that we have annotated the CrunchifyHelloWorld class with @Controller and @RequestMapping("/welcome"). When Spring scans our package, it will recognize this bean as being a Controller bean for processing requests. The @RequestMapping annotation tells Spring that this Controller should process all requests beginning with /welcome in the URL path. That includes /welcome/* and /welcome.html.

The helloWorld() method returns ModelAndView object. The ModelAndView object tries to resolve to a view named "welcome" and the data model is being passed back to the browser so we can access the data within the JSP. The logical view name will resolve to /WEB-INF/jsp/welcome.jsp . Logical name "welcome" which is return in ModelAndView object is mapped to path /WEB-INF/jsp/welcome.jsp.

The ModelAndView object also contains a message with key "message" and Detailed value. This is the data that we are passing to our view. Normally this will be a value object in form of java bean that will contain the data to be displayed on our view. Here we are simply passing a string.

Step-8

The View –Create new file/WebContent/index.jsp.

index.jsp

Create another file/WebContent/WEB-INF/jsp/welcome.jsp.

NOTE: Don't forget to create jsp folder and put welcome.jsp inside that 🙂

welcome.jsp

After everything this is how your workspace should look like.

Simplest Spring MVC Tutorial Project Structure - Crunchify

Step-9

Right Click on Project -> Run As -> Maven Build...

Maven Run-as - Maven Build in Eclipse IDE

Add Goals: clean install. Click Apply and Run.

Clean Install maven Spring project

You should see build success message:

Spring MVC Maven Clean Install Build Success Message

Where are all of my .jar files?

You will see all .jar files under /target folder. Screenshot.

Step-10

  • If you don't see Tomcat Server in Servers tab then follow steps to add Apache Tomcat to Eclipse.
  • Deploy project to Apache Tomcat
    • Right click
    • Add and Remove
    • Add Project to server (right side section).
  • Click on Start.
Start Apache Tomcat in Eclipse - Crunchify Tips

Make sure you see below logs. That means your application is successfully deployed on Tomcat Web Server.

Step-11. Done.

Visit: http://localhost:8080/CrunchifySpringMVCTutorial/ and you should be all set.

Hurray.. Now you know Hello World Spring MVC 5 Example. Let me know if you encounter any exception while running this. There are lot more example you can find here.

Do you want to include JS, CSS and images into JSP file? Follow this tutorial: Best way to Add/Integrate JS, CSS and images into JSP file using 'mvc:resources mapping'.


Having trouble? Any issue?

Triaging Step-1 – Having HTTP Status 404 error?

Make sure welcome.jsp file is inside jsp folder
Also, follow below tutorial:

Triaging step-2 – URL doesn't work? Tomcat error?

Make sure you add Apache Tomcat Server to Targeted Runtime. Which you may have selected in Step-1. Tomcat 7 or 8 any – server should work.

Spring MVC tutorial - Eclipse Target Runtime Setting

Triaging Step-3 – maven errors?

Make sure to update all maven dependencies.

Force Update dependencies - Spring MVC tutorial

Feel free to email or comment below if you have any problem running above tutorial.

Share:

corleypectill88.blogspot.com

Source: https://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/

0 Response to "Java Spring Mvc Tutorial Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel