spring web mvc 处理流程

Architecture

web.xml (webapp必要配置)

作用:spring web mvc 使用dispatcherServlet 分发request,一般我们都需要一个web.xml 来定义这项工作。

servlet-mapping 中定义工作包括:

  • servlet-name :用于匹配名字到 [servlet-name]-servlet.xml (默认的规则:路径位于:/WEBContent/WEB-INF/[servlet-name]-servlet.xml)
  • url-pattern : 定义哪些url 需要servlet去处理
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  • 其他:

    • context-param 的 contextConfigLocation:如果不适用默认规则:自己指定路径来设置Spring容器加载配置文件路径, 如下例子使用applicationContext.xml 可以多个xml
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml,
    /WEB-INF/part2.xml</param-value>
    </init-param>
    • listener-class : 为了能在加载spring 的 bean 配置,设置 Listener

      • listen-class 为org.springframework.web.context.ContextLoaderListener
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>

servlet 定义的比较固定:

  • servlet-class : 定义哪种具体的servlet class(org.springframework.web.servlet.DispatcherServlet) 去做
  • servlet-name :与 web.xml 的servlet-mapping 对应的 servlet-name

dispatcher-servlet.xml / [servlet-name]-servlet.xml (webapp 非必要配置)

作用:初始化 bean

比较典型的例子:

  <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

applicationContext.xml (webapp 非必要配置)

bean 的class 是 org.springframework.web.servlet.view.InternalResourceViewResolver

如果需要初始化bean 对象的成员属性property,则定义其具体属性property name & value

dispatcher-servlet.xml 与 applicationContext.xml 的差别:

共同:两者对webapp 其实都是非必须

applicationContext.xml 定义bean 的 root webapp context

dispatcher-servlet.xml 定义的bean 给一个叫dispatcher(可以是其他名字)的 webapp context

据说 普通webapp context 可以引用 root webapp context

也即:dispatcher-servlet.xml 可以引用applicationContext.xml 定义 context。但反之不行

吐槽:

这么多xml 真的挺烦的,而且还有pom.xml。而且xml 都是比较冗长,使用习惯了其他如nodejs的动态语言,写起来会真的对这些冗长的配置很恼火。

对于pom.xml, gradle 可以稍微释放 xml

对于上述这么多xml,spring-boot 可以释放 这些这么多配置的问题。尤其在:我只不过是想写个简单的rest、单一职责的微服务而已。

另外一方面,这么多xml,IDE是有工具去做简化这些工作的。譬如:

  • pom.xml 的下载管理是可以通过IDE dependencies tab -> Add 去搜做的。
  • Spring Bean configuration file(beans.xml)是可以通过IDE beans tab -> New Bean 去配置的。

然而还是觉得比较麻烦。。。

参考:Spring - MVC Framework Tutorial(http://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm)

spring mvc architecture(http://www.slideshare.net/RaviKantSoni2/spring-framework-3-session1)

Spring的web.xml配置

(http://book.51cto.com/art/200909/151039.htm)

explain dispatcher-servlet.xml, applicationContext.xml, web.xml (https://www.quora.com/How-will-you-explain-dispatcher-servlet-xml-applicationContext-xml-web-xml-and-spring-servlet-xml-to-a-NOVICE-J2EE-Java-programmer-and-relationship-between-these-xmls-in-a-Spring-Web-App)

difference between ApplicationContext and WebApplicationContext (http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s)

spring mvc 为什么这么多xml的更多相关文章

  1. 如何用Java类配置Spring MVC(不通过web.xml和XML方式)

    DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...

  2. spring Mvc 执行原理 及 xml注解配置说明 (六)

    Spring MVC 执行原理 在 Spring Mvc 访问过程里,每个请求都首先经过 许多的过滤器,经 DispatcherServlet 处理; 一个Spring MVC工程里,可以配置多个的 ...

  3. spring mvc实现Restful返回xml格式数据

    最近,想在自己的小项目中搭建一个Restful风格的服务接口api,项目用的spring mvc 3,听说spring mvc本身就能十分方便的支持restful的实现,于是查询了下资料,果然非常强大 ...

  4. spring启动,spring mvc ,要不要xml配置,基于注解配置

    老项目是09-11年搞的,用的是spring+struts2,没有用注解,全xml配置.web.xml中也配置了一大堆. 现在启动新项目,在项目中用spring+springmvc ,主要用注解,也用 ...

  5. Spring mvc项目的web.xml以及注释

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp ...

  6. IntelliJ idea创建Spring MVC的Maven项目

    参考:http://my.oschina.net/gaussik/blog/385697?fromerr=Pie9IlFV 创建Maven Web项目 菜单File->New Project可进 ...

  7. spring 整合 spring mvc

    需要进行 Spring 整合 SpringMVC 吗 ? 还是否需要再加入 Spring 的 IOC 容器 ? 是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 Context ...

  8. spring mvc(1):基础入门

      依赖 pom.xml ( maven ) <properties>  <spring.version>3.0.5.RELEASE</spring.version> ...

  9. 【Spring】搭建最简单的Spring MVC项目

    每次需要Spring MVC的web项目测试一些东西时,都苦于手头上没有最简单的Spring MVC的web项目,现写一个. > 版本说明 首先要引入一些包,Spring的IOC.MVC包就不用 ...

随机推荐

  1. The Guideline of Setting Up Samba Server on linux(Ubuntu)

    The Guideline of Setting Up Samba Server on linux(Ubuntu) From terminate command window, install the ...

  2. [JavaScript] 邮箱验证

    1.JQuery $(function(){ $("#username").blur(function(){ var temp=$("#username"); ...

  3. 实现android"转盘抽奖"小项目后感想

    我这次做的小项目是android的转盘抽奖,因为这个小项目中有进程的调度,加锁等细节,而我们组的竞赛系统中也有这样的问题.通过这次的实践我发现了自己的好多问题也学到了很多. 个人问题: 1.自己的动手 ...

  4. 『TensorFlow』卷积层、池化层详解

    一.前向计算和反向传播数学过程讲解

  5. Regex-Golf

    A man, a plan: 判断回文,如果不确定串长度情况下可以考虑利用反向引用构造pattern,例如: (.)\1  , (.)(.)\2\1 , (.)(.)(.)\3\2\1 ...但这里只 ...

  6. Typescript中的装饰器原理

    Typescript中的装饰器原理 1.小原理 因为react中的高阶组件本质上是个高阶函数的调用, 所以高阶组件的使用,我们既可以使用函数式方法调用,也可以使用装饰器. 也就是说,装饰器的本质就是一 ...

  7. Awesome Tools

    Awesome R: https://awesome-r.com/ (Chinese translation: http://www.ppvke.com/Blog/archives/40981) Aw ...

  8. 不安全的HTTP方法(渗透实验)

    1.使用 curl -v -X OPTIONS url(含目录) 获取允许访问的http method 例如:curl -v -X OPTIONS http://192.168.1.123:12123 ...

  9. Python *Mix_w2

    1.循环: 执行流程: 1. 判断条件是否为真. 如果真. 执行代码块 2. 再次判断条件是否为真...... 3. 当条件为假.执行else 跳出循环. 循环结束. while 条件: 代码块(又叫 ...

  10. >HTML编辑笔记2

    1.列表 ①无序列表 <ul> <li>XXX</li> <li>XXX</li> </ul> ②有序列表 <ol> ...