第一步:创建maven工程,加入SpringMVC的maven依赖:
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-webmvc</artifactId> 4 <version>4.3.0.RELEASE</version> 5 </dependency> 6 7 <dependency> 8 <groupId>javax.servlet</groupId> 9 <artifactId>jstl</artifactId> 10 <version>1.2</version> 11 </dependency> 12 13 <dependency> 14 <groupId>taglibs</groupId> 15 <artifactId>standard</artifactId> 16 <version>1.1.2</version> 17 </dependency>
第二步:在pom.xml文件中加入tomcat7的插件;
<pluginManagement> <plugins> <!-- 配置tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/tomcat-demo</path> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> <url>http://localhost:8080/manager/text</url> <server>tomcat7</server> </configuration> </plugin> </plugins> </pluginManagement>
第三步:配置SpringMVC配置文件;
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" > <!-- 默认的注解映射的支持 --> <mvc:annotation-driven/> <!-- 如果当前请求为“/”时,则转发到“/helloworld/index” --> <mvc:view-controller path="/" view-name="forward:/helloworld/index"/> <!-- 静态资源映射 --> <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> <mvc:resources mapping="/css/**" location="/WEB-INF/css/" /> <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" /> <mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" /> <mvc:resources mapping="images/**" location="/WEB-INF/images/" /> <!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 --> <mvc:default-servlet-handler/> <!-- 开启controller注解支持 --> <!-- use-default-filters="false" 只扫描指定的注解 --> <context:component-scan base-package="com.zte.ems.controllers" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置视图解析器,并指定视图所在的文件夹 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="contentType" value="text/html"/> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
第四步:配置web.xml文件;
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="taotao" version="2.5"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <servlet> <servlet-name>SpringMVCLesson</servlet-name> <!-- DispatcherServlet主要就是拦截请求,然后调用对应的Controller和Action --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- classpath:springservlet-config.xml指定具体的配置文件为springservlet-config.xml --> <param-value>classpath:springservlet-config.xml</param-value> </init-param> <!-- <load-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动,必须放在<servlet> 配置的最后。 --> <load-on-startup>1</load-on-startup><!-- load-on-startup必须放在最后 --> </servlet> <!-- Spring MVC配置文件结束 --> <servlet-mapping> <!-- 指定配置的是哪个servlet --> <servlet-name>SpringMVCLesson</servlet-name> <!-- 指定拦截哪些请求到该servlet,这里配置的是拦截全部请求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第五步:创建一个Contrller控制的Java类:
package com.zte.ems.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/user") public class FormController { @RequestMapping("/success") public String getSuccess() { //跳转页面,默认为转发 return "success"; } }
其中@Controller注解表示,该类是一个受Spring管理的类,一个类使用了@Controller进行标记的都是Controller。
@RequestMapper注解表示,url路径的映射,其实RequestMapping在Class上,可看做是父Request请求url,而RequestMapping在方法上的可看做是子Request请求url,父子请求url最终会拼起来与页面请求url进行匹配。
最后通过clean tomcat7:run 来启动tomcat,在浏览器输入响应的url即可访问。