如何搭建SpringMVC的最小化工程(springmvc工程搭建步骤)
Spring是一个轻量级的Java开源框架,它是一个Full-Stack(全栈)的分层式的Java SE/EE框架。开发者可以很方便地使用Spring的IoC(反转控制)、AOP(面向切面)和MVC(模型-视图-控制器)等功能特性。本文会在Eclipse中搭建一个最小化的SpringMVC工程,以作为后续Spring学习的入门教程。
一、环境描述
操作系统:CentOS 6.6 x86_64 Desktop
JDK版本:build 1.8.0_51-b16
请按照《在CentOS上安装Java》安装。
Eclipse版本:Neon Release (4.6.0)
请按照《如何为官方版本的Eclipse安装Spring的STS插件》安装。
Tomcat版本:8.0.24
请按照《在CentOS上安装Tomcat》安装。
二、添加服务器
启动Eclipse,然后访问菜单Window → Preferences
,然后在打开的首选项窗口中,打开Server → Runtime Environments
,如下图所示:
点击上图中的Add
按钮,然后在打开的新建服务器运行时环境的窗口中选择Apache → Apache Tomcat v8.0
,然后点击Next
按钮,如下图所示:
在下一步的窗口中,填写Tomcat服务器的名称和安装目录。本文将服务器名称指定为“Apache Tomcat v8.0”,安装目录指定为“/usr/local/Tomcat”,最后点击Finish
按钮,如下图所示:
此时,会返回Eclipse的首选项窗口,点击OK
按钮便能成功添加Tomcat服务器了,如下图所示:
三、新建和配置Java工程
在Eclipse中访问菜单File → New → Java Project
,在新建Java工程的窗口中指定工程名为“SpringTest”,指定执行的JRE为“JavaSE-1.8”,然后点击Finish
按钮,如下图所示:
现在可以在Eclipse的Package Explorer
视图中看到“SpringTest”工程了。在工程名上点击鼠标右键,在右键菜单中选择Properties
(或者按下Alt+Enter
快捷键),打开工程的属性窗口,选择该窗口中的Project Facets
,如下图所示:
因为默认的Java工程并没有被配置为使用Project Facets,所以此处需要点击Convert to faceted form...
,添加对Project Facet的支持。然后,在Project Facet列表进行如下配置:
将Java的版本指定为1.8;
将Dynamic Web Module的版本指定为3.1;
将JavaScript的版本指定为1.0。
Project Facet的配置如下图所示,然后点击OK
按钮:
在Project Explorer
视图中,右键点击工程名称,然后在右键菜单中访问Configure → Convert to Maven Project
,如下图所示:
在打开的创建POM的窗口中,工程的Maven配置如下图所示:
点击上图中的Finish
按钮,工程的pom文件便创建完成了。
在Eclipse中打开和编辑工程的/SpringTest/pom.xml
文件,添加对spring-webmvc
和spring-context
的依赖关系,该文件内容最终如下所示:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringTest</groupId>
<artifactId>SpringTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- springframework begins -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
在Eclipse中打开工程的
目录,删除该目录下的
/SpringTest/WebContent/WEB-INFlib
子目录。然后,在WEB-INF
目录中新建web.xml
文件,该文件的内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
<!-- 默认是
/WEB-INF/applicationContext.xml --></context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
<!-- 默认是/WEB-INF/[servlet名字]-servlet.xml -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
web.xml
文件还指定了applicationContext.xml
文件和SpringMVC-servlet.xml
文件的路径。在WEB-INF
目录中新建SpringMVC-servlet.xml
文件,该文件的内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="hello" />
</beans>
SpringMVC-servlet.xml
文件将Spring注解组件的扫描根目录设置为hello
包。在WEB-INF
目录中新建applicationContext.xml
文件,该文件的内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
</beans>
因为这个最小化工程没有用到任何Bean,所以applicationContext.xml
文件中的配置是空的。
若工程提示xml文件有错误,则需要在Package Explorer
视图中右键点击工程名称,然后在右键菜单中访问Maven → Update Project
选项,更新工程的Maven依赖关系即可,如下图所示:
在工程的src
目录中新建一个名为“hello”的包,然后在这个包中再新建一个名为“HelloController”的类文件,该文件的内容如下所示:
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
private int count = 0;
@RequestMapping("hello")
public @ResponseBody String test() {
return String.format("hello, world! This comes from spring, visited %d times!", ++count);
}
}
至此,SpringMVC的最小化工程已经创建和配置完毕了。
四、启动应用
在Eclipse中访问菜单Window → Show View → Servers
,打开Servers
视图,如下图所示:
点击servers
视图中的No servers are available. Click this link to create a new server
,为工程新建的服务器配置如下图所示:
点击Next
按钮,然后将下一步窗口左侧中的“SpringTest”资源添加到窗口的右侧,如下图所示:
此时,便能在Servers
视图中看到这个工程使用的Tomcat服务器了,右键点击服务器名称,然后选择右键菜单的Debug
或Start
命令,即可调试或运行这个最小化工程了。
打开浏览器,然后在地址栏中输入以下URL:
http://localhost:8080/SpringTest/hello
打开的测试页面如下图所示:
至此,SpringMVC的最小化工程已经运行验证完毕了!