SpringMVC应用以及源码解析

一:SpringMVC是什么

  SpringMVC只是Spring的一个子框架,作用学过Struts2的应该很好理解,他们都是MVC的框架。学他就是用来代替Struts2的,那么为什么不用Struts2而选择SpringMVC呢!那就必须说说它相比与struts2的部分优点:

(2)整合:大部分企业都会使用Spring,而SpringMVC就是Spring的一个子框架,当然方便些。

(3)实现机制:Struts2是基于filter过滤器的,而SpringMVC是基于servlet,以前认为filter是servlet的一种特殊,但是servelet明显比filter快。而且struts2是多例的,而SpringMVC则是单例的

(4)参数封装上:

Struts基于属性进行封装。

Springmvc基于方法封装。颗粒更细

二:SpringMVC的执行流程

理论层面

(1)用户发送请求至前端控制器DispatcherServlet;

(2) DispatcherServlet收到请求后,调用HandlerMapping处理器映射器,请求获取Handle;

(3)处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet;

(4)DispatcherServlet 调用 HandlerAdapter处理器适配器;

(5)HandlerAdapter 经过适配调用 具体处理器(Handler,也叫后端控制器);

(6)Handler执行完成返回ModelAndView;

(7)HandlerAdapter将Handler执行结果ModelAndView返回给DispatcherServlet;

(8)DispatcherServlet将ModelAndView传给ViewResolver视图解析器进行解析;

(9)ViewResolver解析后返回具体View;

(10)DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)

(11)DispatcherServlet响应用户。

实现层面

  从1到14,是使用SpringMVC全部的代码执行过程,我们要做的就是

1)在Web.xml中配置前端控制器

2)在SpringMVC的配置文件(springMVC.xml)中配置处理器映射器

3)在SpringMVC的配置文件(Springmvc.xml)中配置处理器适配器(处理器映射器有三种,无论采取哪种都可以)

4)创建自定义Controller

5)配置自定义Controller的bean在Springmvc中

6)配置视图解析器 

三:注解开发

常用注解

1:@RequestMapping:用于处理请求 url 映射的注解,可用于类或方法上。

用于类上,则表示类中的所有响应请求的方法都是以该地址作为父路径。method=RequestMethod.GET。

2:@RequestBody:注解实现接收http请求的json数据,将json转换为java对象。

3:@ResponseBody:注解实现将conreoller方法返回对象转化为json对象响应给客户。

4:@Controller或@RestController,其中@RestController相当于@RequestMapping+@Controller

5:@SessionAttributes:把ModelMap里面的数据放入Session里面。可以在类上面加上@SessionAttributes注解,里面包含的字符串就是要放入session里面的key

使用注解开发配置:

SpringMVC.xml添加扫描注解

<context:component-scan base-package="com.xqc"></context:component-scan>

配置注解的处理器映射器

1 <!-- 配置注解处理器映射器
2           功能:寻找执行类Controller
3  -->
4 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
5 </bean>

配置注解的处理器适配器

1 <!-- 配置注解处理器适配器 
2     功能:调用controller方法,执行controller
3 -->
4 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
5 </bean>

视图解析器

<!-- 配置sprigmvc视图解析器:解析逻辑试图 
    后台返回逻辑试图:index
    视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsps/"></property>
    <property name="suffix" value=".jsp"></property>        
</bean>

四:SpringMVC整合Spring ,Mybatis

1:创建工程

2:jar包准备,使用Maven则pom.xml准备

3:配置web.xml文件

3.1)加载SpringMVC配置文件

 1 <filter>
 2   <filter-name>characterEncoding</filter-name>
 3   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 4   <init-param>
 5     <param-name>encoding</param-name>
 6     <param-value>UTF-8</param-value>
 7   </init-param>
 8 </filter>
 9 <filter-mapping>
10   <filter-name>characterEncoding</filter-name>
11   <url-pattern>/*</url-pattern>
12 </filter-mapping>
13 <servlet>
14   <servlet-name>springmvc</servlet-name>
15   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16   <!-- 默认加载方式
17          默认加载必须规范:
18          * 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
19          * 路径规范:必须在WEB-INF目录下面
20    -->
21   <init-param>
22    <param-name>contextConfigLocation</param-name>
23    <param-value>classpath:springmvc.xml</param-value>   
24   </init-param>
25  </servlet>
26 <servlet-mapping>
27   <servlet-name>springmvc</servlet-name>
28   <url-pattern>*.do</url-pattern>
29 </servlet-mapping>

3.2)加载spring配置文件

1 <!-- 加载spring配置文件 -->
2 <listener>
3   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
4 </listener>
5 <context-param>
6   <param-name>contextConfigLocation</param-name>
7   <param-value>classpath:beans.xml</param-value>
8 </context-param>

4:配置Springmvc.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 8         http://www.springframework.org/schema/mvc 
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
10         http://www.springframework.org/schema/context 
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
14         http://www.springframework.org/schema/tx 
15         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
16 <!--注解开发,开启扫描-->
17     <context:component-scan base-package="cn.itcast"></context:component-scan>
18     
19     <!-- annotation-driven:默认创建了多个对象:RequestMappingHandlerMapping,RequestMappingHandlerAdapter
20         也就提供对json格式支持
21      -->
22     <mvc:annotation-driven/>
23     
24     
25     <!-- 配置sprigmvc视图解析器:解析逻辑试图 后台返回逻辑试图:index 视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp -->
26     <bean
27         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
28         <property name="prefix" value="/WEB-INF/jsps/"></property>
29         <property name="suffix" value=".jsp"></property>
30     </bean>    
31 </beans>
Springmvc.xml

5:配置beans.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 8         http://www.springframework.org/schema/mvc 
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
10         http://www.springframework.org/schema/context 
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
14         http://www.springframework.org/schema/tx 
15         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
16 <!--Spring注解开发,开启扫描-->        
17         <context:component-scan base-package="com.xqc"></context:component-scan>
18         
19     <!-- 第一步:配置数据源 -->
20     <context:property-placeholder location="classpath:jdbc.properties" />
21     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
22         <property name="jdbcUrl" value="${jdbc.url}"></property>
23         <property name="driverClass" value="${jdbc.driver}"></property>
24         <property name="user" value="${jdbc.username}"></property>
25         <property name="password" value="${jdbc.password}"></property>
26 
27     </bean>
28 
29     <!-- 第二步:创建sqlSessionFactory。生产sqlSession -->
30     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
31     <property name="dataSource" ref="dataSource"></property>
32     <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
33     </bean>
34     <!-- 配置mybatis接口代理开发
35 
36         * 接口类名和映射文件必须同名
37         * 接口类和映射文件必须在同一个目录下
38         *  映射文件namespace名字必须是接口的全类路径名
39         * 接口的方法名必须和映射Statement的id一致
40      -->
41      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
42      <property name="basePackage" value="com.xqc.dao"></property>
43      <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
44      </bean>
45     
46 
47     <!-- 第三步:事务 -->
48     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
49     <property name="dataSource" ref="dataSource"></property>
50     </bean>
51     
52     <!-- 配置通知 ,管理事务的策略-->
53     <tx:advice id="txAdvice" transaction-manager="transactionManager">
54     <tx:attributes>
55     <tx:method name="save*" propagation="REQUIRED" />
56     <tx:method name="update*" propagation="REQUIRED" />
57     <tx:method name="delete*" propagation="REQUIRED" />
58     <tx:method name="insert*" propagation="REQUIRED" />
59     <tx:method name="*" propagation="REQUIRED" />    
60     </tx:attributes>
61     
62     </tx:advice>
63     
64     <!-- 配置拦截service ,切面-->
65     <aop:config>
66     <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xqc.service.*.*(..))"/>
67     </aop:config>
68     
69 </beans>
beans.xml

6:配置  jdbc.properties 文件

1 jdbc.url = jdbc:mysql:///mybatismoder
2 jdbc.driver = com.mysql.jdbc.Driver
3 jdbc.username= root
4 jdbc.password= 1234
jdbc.properties

7:配置Mybatis配置文件  sqlMapConfig.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6 
7 </configuration>
sqlMapConfig.xml

 8:创建包结构,编写代码即可。

 四:SpringMVC常见问题

原文地址:https://www.cnblogs.com/nullering/p/9459755.html