1. 运行流程

2. Spring 和 SpringMVC 整合

// 1. 导入 jar 包
// 2. 配置 web.xml <!-- 配置 Spring 的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置 Spring 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 配置SpringMVC配置文件路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param> <!-- 配置过滤器,解决 POST 乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置SpringMVC框架入口 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置SpringMVC 什么时候启动,参数必须为整数 -->
<!-- 如果为0 或者 大于0, 则SpringMVC随着容器启动而启动 -->
<!-- 如果小于0, 则在第一次请求进来的时候启动 -->
<load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!--
可用: *.xxx, /, /xxx/*
不可用: /*
-->
<url-pattern>/</url-pattern>
</servlet-mapping> // 3. 在 src 目录下创建 springmvc.xml <!-- 配置自动扫描包路径 -->
<context:component-scan base-package="cn.itcast.springmvc"></context:component-scan> <!-- 配置通用的视图解析器 -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 处理静态资源导入 -->
<mvc:default-servlet-handler/>
<!-- 如果只有 mvc:default-servlet-handler, 注解类失效, 还需要配置 annotation-driven -->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 对静态资源放行
mapping: 只页面中url路径中包含的, /** 表示可以包含多级目录
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/js/" mapping="/js/*"/>
<mvc:resources location="/fonts/" mapping="/fonts/*"/>
--> // 4. 在 src 目录下创建 applicationContext.xml
<!-- 配置自动扫描包路径 -->
<context:component-scan base-package="cn.itcast.springmvc"/> // cn.itcast.springmvc.HelloController.java
@Controller
public class HelloController{
// 无参构造函数
public HelloController(){
System.out.println("====HelloController");
}
} // cn.itcast.springmvc.UserService.java
@Service
public class UserService{
// 无参构造函数
public UserService(){
System.out.println("====UserService");
}
}

2.1 存在问题一: Bean 创建两次

// 改进方式:
// springmvc.xml
<context:component-scan base-package="cn.itcast.springmvc"> <!-- 只扫描 @Controller 和 @ControllerAdvice 注解标注的类 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan> // applicationContext.xml
<context:component-scan base-package="cn.itcast.springmvc"> <!-- 不扫描 @Controller 和 @ControllerAdvice 注解标注的类 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

2.2 存在问题二:

// 改进方式: use-default-filters="false"
// springmvc.xml
<context:component-scan base-package="cn.itcast.springmvc" use-default-filters="false"> <!-- 只扫描 @Controller 和 @ControllerAdvice 注解标注的类 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

2.3 最终效果:

3. SpringMVC 配置文件中引用业务层的 Bean

  • 多个 SpringIOC 容器之间可以设置为父子关系,以实现更好的解耦;
  • SpringMVC Web层容器可以作为"业务层"Spring容器的子容器:即 Web 层容器可以引用业务层容器的Bean,

    而业务层容器却访问不到Web层容器;

// Web 层
@Controller
public class CustomerHandler{
@Autowired
public CustomerService customerService; @RequestMapping(value="/test",method=RequestMethod.GET)
public String test(){
// 可以这样调用
System.out.println("=====test: "+customerService.getClass().getName());
return "ok";
}
} // 业务层
@Service
public class CustomerService{ // 业务层不可以调用 Web 层
// 这样,会出现异常
@Autowired
private CustomerHandler customerHandler; public CustomerService(){
System.out.println("====CustomerService:"+customerHandler.test());
}
}

3. SpringMVC 与 Struts2 比较

  • SpringMVC 的入口是 Servlet, Struts2 是 Filter;
  • SpringMVC 会稍微比 Struts2 快些, SpringMVC 是基于方法涉及的,而 Struts2 是基于类,每发一次请求

    都会实例一个Action;
  • SpringMVC 使用更加简洁,开发效率高, 支持JSR303, 处理 ajax 的请求更方便;
  • Struts2 的 OGNL 表达式使页面的开发效率相比 SpringMVC 更高些;

参考资料

SpringMVC 运行流程以及与Spring 整合的更多相关文章

  1. SpringMVC--从理解SpringMVC执行流程到SSM框架整合

    前言 SpringMVC框架是SSM框架中继Spring另一个重要的框架,那么什么是SpringMVC,如何用SpringMVC来整合SSM框架呢?下面让我们详细的了解一下. 注:在学习SpringM ...

  2. SpringMVC运行流程

    Spring工作流程描述       1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获:       2. DispatcherServl ...

  3. springmvc运行流程简单解释(源码解析,文末附自己画的流程图)

    首先看一下DispatcherServlet结构: 观察HandlerExecutionChain对象的创建与赋值,这个方法用来表示执行这个方法的整条链. 进入getHandler方法: 此时的变量h ...

  4. 【串线篇】SpringMVC运行流程

    1.所有请求,前端控制器(DispatcherServlet)收到请求,调用doDispatch进行处理 2.根据HandlerMapping中保存的请求映射信息找到,处理当前请求的,处理器执行链(包 ...

  5. SpringMVC听课笔记(十五:SpringMVC 运行流程)

    1. 图 一般的会按照红线标注的方向去行进,但是请求静态资源,或者出现异常等,会出现其他路径 2.

  6. springmvc 运行原理 Spring ioc的实现原理 Mybatis工作流程 spring AOP实现原理

    SpringMVC的工作原理图: SpringMVC流程 . 用户发送请求至前端控制器DispatcherServlet. . DispatcherServlet收到请求调用HandlerMappin ...

  7. spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist

    spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...

  8. springMVC框架介绍以及运行流程(图解)

    1 Springmvc是什么? spring web mvc和struts2都属于表现层的框架,spring web mvc是spring框架的一部分(所以spring mvc与spring之间不需要 ...

  9. java面试记录二:spring加载流程、springmvc请求流程、spring事务失效、synchronized和volatile、JMM和JVM模型、二分查找的实现、垃圾收集器、控制台顺序打印ABC的三种线程实现

    注:部分答案引用网络文章 简答题 1.Spring项目启动后的加载流程 (1)使用spring框架的web项目,在tomcat下,是根据web.xml来启动的.web.xml中负责配置启动spring ...

随机推荐

  1. [Yii Framework] Share the session with memcache in Yii

    When developing distributed applications with Yii, naturally, we will face that we have to share the ...

  2. 记一次netty版本冲突,报java.lang.NoSuchMethodError: io.netty.util.internal.ObjectUtil.checkPositive的问题

    elasticsearch 5.6中使用TransportClient初始化抛异常 在引入elasticsearch5.6的transportclient包中,会引入netty进行通信. <!- ...

  3. 手动安装minGW

    minGW是C语言编译包,将GCC编译器在Windows平台上编译软件提供支持. 手工安装minGW是一件很繁琐的事情,但是搞懂它很有用,因为C语言本身是一个很小的语法系统,全靠 各种库在支持,安装m ...

  4. JQ集合

    获取所有id以a开头的div$("div[id^='a']") $('div').last() $('#item1') <div id='select'>点击这里< ...

  5. HashMap 内部原理

    HashMap 内部实现 通过名字便可知道的是,HashMap 的原理就是散列.HashMap内部维护一个 Buckets 数组.每一个 Bucket 封装为一个 Entry<K, V> ...

  6. vi全文替换命令

    1,$s/str1/str2/g:从第一行到最后一行把str1替换成str2

  7. linux 文件夹的颜色代表什么意思

    linux 文件夹的颜色代表什么意思 绿色 蓝色 黑色代表什么意思 蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件: 灰色表示其它文件: 红色闪烁表示链接的文件有问题了: ...

  8. 清空oracle数据库

    在开发过程中,可能经常需要重新初始化数据库,在初始化之前,我们肯定希望不再有以前的老表.存储过程等用户对象,用下面的教本就可以做到这一点: BEGIN FOR rec IN (SELECT objec ...

  9. HLJU 1042 Fight (种类并查集)

    1042: Fight Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 26  Solved: 8 [Submit][Status][pid=1042& ...

  10. Tomcat7 自动加载类及检测文件变动原理

    在一般的web应用开发里通常会使用开发工具(如Eclipse.IntelJ)集成tomcat,这样可以将web工程项目直接发布到tomcat中,然后一键启动.经常遇到的一种情况是直接修改一个类的源文件 ...