
上QQ阅读APP看书,第一时间看更新
1.3 快速搭建Spring 5项目
1.3.1 使用IntelliJ IDEA创建Spring 5+Spring MVC项目
本节将通过Spring 5+Spring MVC快速搭建一个Hello World Web程序。搭建本书项目全部是基于Spring 5.1开发。搭建分为以下步骤。
(1)在pom中引入Spring相关依赖。
一个最简单的Spring MVC项目只需要javax.servlet-api和spring-webmvc这两个jar包。在maven项目的pom.xml中加入这两个jar包的依赖,具体代码如下:

(2)创建HelloWorldController,输出文字Hello World。
Controller是Spring MVC控制层模块,创建一个简单Controller,提供“/hello”这个HTTP接口,然后使用浏览器访问该接口,即可输出文字“Hello World”。

(3)配置文件springmvc.xml。定义扫描包的路径和视图解析器。

(4)配置web.xml文件。指定Spring MVC核心Servlet和相关的配置文件即可:

1.3.2 测试部署
使用Intellij IDEA集成的Tomcat发布整个Spring MVC应用,在浏览器中访问http://localhost:8080/hello接口,效果如图1-8所示。

图1-8 Intellij IDEA集成Apache Tomcat启动窗口