springMVC本身是属于spring的衍生框架,所以整合起来很简单,只要包jar依赖进来,然后增加springmvc的配置即可,前提是已经了解并合理使用spring容器
1、导入maven依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
2、配置web.xml
springMVC是一个基于servlet实现的框架,所以web就是一段servlet的配置
而在web项目中,我们需要吧创建spring容器的操作交给tomcat,所以需要配置自动spring容器的监听器,同时制定xml文件的位置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-core.xml</param-value>
</context-param>
<!-- spring监听器-用于创建spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring-mvc请求分派器 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
配置spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 使用注解方式完成映射 -->
<!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
<context:component-scan base-package="com.xingxue.controller"/>
<mvc:annotation-driven/><!-- 开启注解 -->
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/><!-- 前缀 -->
<property name="suffix" value=".jsp"></property> <!-- 后缀 -->
</bean>
</beans>
配置spring-core.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
<!-- spring开启注解 @-->
<context:component-scan base-package="com.xingxue" >
</context:component-scan>
</beans>
编写cotroller, @RequestMapping: 表示请求隐射地址
package com.xingxue.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/login.do")
public String login() {
System.out.println("11111111111");
return "OK";
}
@RequestMapping("/register.do")
public String register() {
return "OK";
}
public String add() {
return "LIST";
}
}
编写页面请求路径
<html>
<body>
<h2>Hello World!</h2>
<a href="<%=request.getContextPath()%>/user/login.do">spring MVC</a>
</body>
</html>
发布运行即可!
springMVC运行原理:
1、springMVC是基于servlet来进行实现的。
2、请求的流程,我们的http请求直接交到我们的DispatcherServlet里面,他通过读取配置文件,解析我们的请求路径找到对应controller来处理这个功能。当controller处理完成这个功能之后吧结果反馈给我们的disatcherServlet。dispacherServlet通过使用视图解析器组装数据,跳转到对应页面。
3、springmvc是单利模式,效率高, 参数传力利用的参数的方式,线程安全