1.创建Maven版的Web工程
BuildPath设置动态工程Dynamic Web Module
Build Path - >Add library->Server Runtime->Apache
2.pom.xml中的jar依赖配置
<dependencies>
<!-- context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
</dependencies>
3.在web.xml中配置DispatcherServle(调度工servlet)
需要配置的两个地方:
1 配置 SpringMVC配置文件的路径为:类路径下的classpath:springmvc.xml(一般名称为springmvc.xml)
2.配置映射的请求地址 为 /
(注意删除web-app 标签的最后的/ 换行 alt + /)
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 配置前端控制器:DispatcherServlet 快捷键 alt + / 倒数第二项 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置 SpringMVC配置文件的路径-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- 配置映射的请求地址 为 / -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.加入SpringMVC的配置文件springmvc.xml
4.1 增加命名空间
4.2 配置组件扫描 和视图解析器
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 设置自动扫描的包 -->
<context:component-scan base-package="afei.springmvc"></context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/views/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5.创建入口界面
在页面中编写超链接:
<a href="${pageContext.request.contextPath }/helloWorld">Hello SpringMVC </a>
6.编写helloWorld请求处理器
注意@Controller和@RequestMapping("/helloWorld")
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
/*
* 方法的返回值会被SpringMVC配置文件中配置的解析器InternalResourceViewResolver解析为真实的物理视图
* 然后进行请求的转发
* 真实的物理视图=前缀+返回值+后缀
* 即:/WEB-INF/views/success.jsp
*/
@RequestMapping("/helloWorld")
public String testHelloWorld() {
System.out.println("Hello Spring MVC");
return "success";
}
}
7. 编写视图页面
在/WEB-INF/views下新建success.jsp页面
8. 部署测试
9.流程解析
index.jsp中的helloWorld+web.xml中的 / = /helloWorld
即helloWorld中的@RequestMapping("/helloWorld")
返回值的success+前缀+后缀=/WEB-INF/views/success.jsp