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. Nginx日志深入详解

    一.日志分类 Nginx日志主要分为两种:访问日志和错误日志.日志开关在Nginx配置文件(/etc/nginx/nginx.conf)中设置,两种日志都可以选择性关闭,默认都是打开的.1.访问日志 ...

  2. atitit.jquery tmpl模板总结 .doc

    atitit.jquery tmpl模板总结 .doc 1. atitit.动态模版解析1 1.1. Jquery.tmpl.js1 1.2. 比起anrular js方便啊.1 2. 动态模板引擎解 ...

  3. 完整学习使用CSS动画【翻译】

    注:原文有较大改动 使用keyframes, animation属性,例如timing,  delay, play state, animation-count, iteration count, d ...

  4. SVN提交项目时版本冲突解决方案

    版本冲突原因: 假设A.B两个用户都在版本号为7的时候,更新了index.jsp这个文件,A用户在修改完成之后提交index.jsp到服务器,这个时候提交成功,这个时候index.jsp文件的版本号已 ...

  5. python学习之time模块

    time.time() 将时间作为浮点数返回. 在Windows和大多数Unix系统上,时代是1970年1月1日00:00:00(UTC),并且闰秒不计入从时代开始的秒数. >>> ...

  6. Spark Standalone与Spark on YARN的几种提交方式

    不多说,直接上干货! Spark Standalone的几种提交方式 别忘了先启动spark集群!!! spark-shell用于调试,spark-submit用于生产. 1.spark-shell ...

  7. JQ 时间插件

    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/u ...

  8. C#反射Assembly 详细说明,有项目例子

    1.对C#反射机制的理解2.概念理解后,必须找到方法去完成,给出管理的主要语法3.最终给出实用的例子,反射出来dll中的方法 反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等 ...

  9. Linux的文件权限(简单易懂)

    学习这个章节,必须明白以下三个概念: 1.所有者 2.所属组 3.其他人 明白这三个概念后,接下来就学习文件的属性,那么文件的属性有什么呢?如何查看文件的属性? 在命令行下,执行 ls -l 可以得到 ...

  10. greenplum全量恢复gprecoverseg -F出现Unable to connect to database时的相关分析及解决方法

    之前有两位朋友碰到过在对greenplum的系统构架更改后,出现全量恢复gprecoverseg -F也无法正常执行的情况. 报错信息为Unable to connect to database. R ...