2015 最简单的 Spring MVC 入门教程

简介: 2015 最简单的 Spring MVC 入门教程太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)本文遵循“署名-非商业用途-保持一致”创作公用协议转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。

2015 最简单的 Spring MVC 入门教程

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。



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

SHORT LINK: 

Last Updated on October 22nd, 2015 by  179 Comments

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 Demo, jQuery Demo, JavaScript 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. I’m using below tools which you may need to download if you don’t have already.

  1. Tomcat 7.0.65 – Download link.
  2. Eclipse IDE for Java EE Developers (Mars v4.5.1) – Download link.
  3. Spring 4.2.2 (No download required) – we will use Maven dependency.
  4. JDK 1.8 – Download link.

Main goal for this tutorial to create Spring MVC Application in the simplest way. This is how ourapplication result will look like. This is a final result once you complete all below steps.

Welcome page ==> index.jsp

CrunchifySpringMVCTutorial index.jsp page

Result returns from Controller Class :)

Message Coming from Controller

Now Let’s get started

Step-1

Open Eclipse and Create Dynamic Web Project CrunchifySpringMVCTutorial.

Create Dynamic Web Project in Eclipse

Crunchify Spring MVC Example

Step-2

Make sure you use Target Runtime as Apache Tomcat 7.0

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 CrunchifySpringMVCTutorial to Maven project

Step-4

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

Srping MCV Dependencies - Crunchify Spring Tutorial

Here is my pom.xml file.

Step-5

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

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

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.

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.

  • Package: com.crunchify.controller
  • Filename: CrunchifyHelloWorld.java

Create Spring Controller Class CrunchifyHelloWorld

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 /WebContent/index.jsp.

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

After everything this is how your workspace should look like.

CrunchifySpringMVCTutorial by Crunchify - Eclipse Package Structure

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:

Build Successful - Maven clean install task in Eclipse

Where are all of my .jar files?

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

Step-10

Deploy project to Apache Tomcat and start tomcat.

Start Tomcat Server

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

Application Successfully Deployed on Tomcat Server in Eclipse Mars IDE

Step-11

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

Hurrey.. 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

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.

Add Target Runtime on project

Triaging Step-2

Make sure to update all maven dependencies.

Force Update dependencies - Spring MVC tutorial

Have anything to add to this article? Please chime in and join the conversion.
SHARE ON

Filed Under: Apache Tomcat Examples, Eclipse, Java Collection, Spring MVC Tutorials

Enjoyed this post?

Be sure to subscribe to the Crunchify newsletter and get regular updates about awesome posts just like this one and more! Join more than 13000 subscribers!

Enter your email address...  

About Crunchify

Hello & Good Day from greater New York. Crunchify is founded by App Shah. He is a professional blogger & loves Web development hence Crunchify.com is his publication with more than 10 millions pageviews per month, that covers all aspects and tactics on Java, WordPress, J2EE (Enterprise Java), Spring MVC, Github, etc Technologies.




目录
相关文章
|
1月前
|
缓存 前端开发 Java
Spring MVC 面试题及答案整理,最新面试题
Spring MVC 面试题及答案整理,最新面试题
90 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
10 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
9 0
|
1月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
26 0
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
42 1
|
15天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
15天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
15天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
31 3
|
24天前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
1月前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
73 0