Simplest Spring MVC Hello World Example / Tutorial – Spring Model – View – Controller Tips

 

Do you have any one of below question?

  • Developing a Spring Framework MVC 4 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 4 MVC Tutorials, AJAX DemojQuery DemoJavaScript Demo, Tips & Tricks Spring 4 MVC

Then you are at right place. Here I’ll demonstrate simple Spring MVC framework for building web applications.

First thing first – Let’s Setup Environment

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

  1. Tomcat 8.0.37 – Download latest Apache Tomcat from this link.
  2. Make sure you download Eclipse IDE for Java EE Developers (Neon v4.6) – Download link. (diagram below)
  3. Spring 4.3.1 (No download required) – we will use Maven dependency.
  4. JDK 1.8 – Download link.

Make sure you download Java EE:

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Main goal for this tutorial to create Spring MVC Application in the simplest way. 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

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Result returns from Controller Class ????

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

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.

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

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 8.0. If you don’t see Target Runtime then follow these steps.

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

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

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Step-4

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

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Here is my pom.xml file. Make sure you update Java version to 1.7 if you haven’t yet moved to JDK 1.8.

pom.xml
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
 
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
 
>
>
>
>
>
>
>

 

Step-5

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

/WEB-INF/crunchify-servlet.xml
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
>
 
>
 
>
>
>
>
>
 
>

In the above crunchify-servlet.xml  configuration file, we have defined a tag <context:component-scan> . This will allow Spring to load all the components from package com.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. 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-INF/web.xml
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
?>
>
>
>
>
>
  
>
>
>
DispatcherServlet
>
>
>
>
>
>
>
>
>
  
>

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

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

com.crunchify.controller.CrunchifyHelloWorld.java
Java
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
;
 
;
;
;
 
/*
* author: Crunchify.com
*
*/
 
@Controller
{
 
)
{
 
;
;
}
}

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
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>
>
>
{
;
}
</style>
>
>
>
>
>
>
>
>
to
)
>
>
>
>

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
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
>
>
MVC
>
{
;
}
</style>
>
}
 
>
>
>
 
.
a
a
>
>
>
>
>

After everything this is how your workspace should look like.

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Step-9

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

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Add Goalsclean install. Click Apply and Run.

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

You should see build success message:

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

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 and start tomcat.

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

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

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Step-11

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

Hurray.. Now you know Hello World Spring MVC 4 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?

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse
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.

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Triaging Step-3 – maven errors?

Make sure to update all maven dependencies.

http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/  非常棒的spring入门,maven,以及eclipse

Have a suggestion on article? Please chime in and share it as a comment.
 
 
注意 viewResolver 的配置以及welcome-fle-list 的配置
 
viewResolver的配置应该是spring的配置
<bean id="viewResolver"
>
>
>
>
>
 
welcome-file-list应该是tomcat的配置
<welcome-file-list>
>
>

相关文章:

  • 2021-05-09
  • 2021-12-18
  • 2021-11-06
  • 2022-12-23
  • 2022-01-18
  • 2021-12-03
猜你喜欢
  • 2021-10-07
  • 2022-12-23
  • 2021-05-21
  • 2021-06-02
  • 2021-09-26
相关资源
相似解决方案