第一篇文章宏观讲了Spring MVC概念,以及分享了一个高速入门的样例

这篇文章主要来谈谈Spring MVC的配置文件。



首先来谈谈web.xml: web项目启动时自己主动载入到内存中的信息,比方server配置參数,<listener>监听器。<filter>过滤器,<servlet>等。再如,假设在项目中使用了spring框架。则必须定义ContextLoaderListener。那么在启动Web容器时。会自己主动装配Spring
applicationContext.xml的配置信息。这里贴出一个典型的web.xml文件,凝视写的非常具体:

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?

>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC</display-name> <!-- 这里是指定 Spring配置文件:contextConfigLocation的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value>
</context-param> <!-- 启动Spring容器须要的类文件 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <!-- DispatcherServlet, Spring MVC的核心 -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet相应的上下文配置。 默觉得/WEB-INF/$servlet-name$-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定Spring MVC的配置文件的详细位置,这一參数能够不指定。那就是默认设置。上面有说 -->
<param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- "/"写法,mvc-dispatcher拦截全部的请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app></span>

再来说下Spring MVC配置文件mvc-dispatcher-servlet.xml:web项目启动时。在启动对应的servlet时会配置Spring
MVC。一个典型的Spring MVC的配置文件例如以下:

<pre name="code" class="html"><span style="font-size:14px;"><?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- DispatcherServlet使用, 提供其相关的Spring MVC配置 --> <!-- 启用Spring基于annotation的DI-->
<context:annotation-config /> <!-- DispatcherServlet上下文, 仅仅管理@Controller类型的bean, 忽略其它型的bean, 如@Service -->
<context:component-scan base-package="com.xidian.mvcdemo">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> <!-- HandlerMapping, 无需配置, Spring MVC能够默认启动。 DefaultAnnotationHandlerMapping
annotation-driven HandlerMapping --> <!-- 扩充了注解功能,能够将请求參数绑定到控制器參数,比方能够在类中的某个方法上加上。进一步指定url,极大提高开发效率 -->
<mvc:annotation-driven /> <!-- 静态资源处理, css, js。 imgs等。为web项目映射详细的资源路径-->
<mvc:resources mapping="/resources/**" location="/resources/" /> <!-- 配置ViewResolver。视图解析器,还能够有多个,仅仅要附上对应的order就可以 ,实际执行时会次序载入-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsps/" />
<property name="suffix" value=".jsp" />
</bean> <!-- Spring mvc 提供为了实现文件的上下传 -->
<!--200*1024*1024即200M resolveLazily属性启用是为了推迟文件解析。以便捕获文件大小异常 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean> </beans></span>

最后来说下Spring容器的配置文件applicationContext.xml:这里没太多涉及,详细的配置细节能够看《Spring新手教程(一)》。在后面的Mybatis系列文章中会结合来做一个简单的demo,Spring
MVC入门系列就是三篇文章。另一篇文章会详细说些开发中经常使用的标签的注意事项和一些Spring MVC开发思想。

<span style="font-size:14px;"><?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:context="http://www.springframewor k.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启用Spring基于annotation的DI-->
<context:annotation-config /> <context:component-scan base-package="com.xidian.mvcdemo">
<!-- 提示Spring不须要管理com.xidian.mvc.demo下Controller属性修饰的对象 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> </beans></span>

你看会了吗?欢迎讨论http://blog.csdn.net/code_7

Spring MVC新手教程(二)的更多相关文章

  1. Spring MVC新手教程(一)

    直接干货 model 考虑给用户展示什么.关注支撑业务的信息构成.构建成模型. control 调用业务逻辑产生合适的数据以及传递数据给视图用于呈献: view怎样对数据进行布局,以一种优美的方式展示 ...

  2. Spring MVC 入门教程示例 (一)

    今天和大家分享下  Spring MVC  入门教程 首先还是从 HelloWorld  web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...

  3. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  4. spring mvc入门教程 转载自【http://elf8848.iteye.com/blog/875830】

    目录  一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...

  5. Spring Cloud 入门教程(二): 配置管理

    使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring ...

  6. 160506、Spring mvc新手入门(11)-返回json 字符串的其他方式

    Spring MVC返回 json字符串的方式有很多种方法,这里介绍最简单,也是最常使用的两种方式 一.使用  PrintWriter printWriter  直接输出字符串到返回结果中    不需 ...

  7. 手写Spring MVC框架(二) 实现访问拦截功能

    前言 在上一篇文章中,我们手写了一个简单的mvc框架,今天我们要实现的功能点是:在Spring MVC框架基础上实现访问拦截功能. 先梳理一下需要实现的功能点: 搭建好Spring MVC基本框架: ...

  8. Spring MVC入门(二)—— URI Builder模式

    URI Builder Spring MVC作为一个web层框架,避免不了处理URI.URL等和HTTP协议相关的元素,因此它提供了非常好用.功能强大的URI Builder模式来完成,这就是本文重点 ...

  9. spring MVC入门教程

    写一个spring mvc后台传值到前台的一个小例子. 分为以下几个步骤: 1.创建web项目. 导入项目包.具体有如下: spring-aop-4.0.4.RELEASE.jar spring-be ...

随机推荐

  1. grid 布局 CSS3

    display:grid 是一种新的布局方式,旧的布局方式通常有副作用,例如float(需要额外修复浮动)或者inline-block(两个元素之间的空格问题)   把父元素定义为grid,就像表格一 ...

  2. :after伪类+content内容生成

    :after伪类+content 清除浮动的影响 浮动元素会让此div的高度塌陷.如下例子: .box{padding:10px; background:gray;} .l{float:left;} ...

  3. HTML学习笔记 cs2D3D展示基础 第十四节 (原创) 参考使用表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 微信小程序语音识别服务搭建全过程解析(https api开放,支持新接口mp3录音、老接口silk录音)

    silk v3(或新录音接口mp3)录音转olami语音识别和语义处理的api服务(ubuntu16.04服务器上实现) 重要的写在前面 重要事项一: 所有相关更新,我优先更新到我个人博客中,其它地方 ...

  5. OpenCV畸变校正源代码分析

    图像算法中会经常用到摄像机的畸变校正,有必要总结分析OpenCV中畸变校正方法,其中包过普通针孔相机模型和鱼眼相机模型fisheye两种畸变校正方法. 普通相机模型畸变校正函数针对OpenCV中的cv ...

  6. Python学习——列表

    定义:1.列表(list)是由一系列特定顺序排列的元素组成,可以包含字母,数字或者将任何东西加入列表. 2.列表的标识符号为 [ ],函数名称为list 3.列表是有序的 相关概念: 元素:列表中的值 ...

  7. 万年历java

    public void showTime(){/*万年历 :  1900年1月20号是星期几?1月1号是星期一1月8号是星期一1月15号是星期一1%7 = 18%7 = 115%7 = 1★: 1. ...

  8. Tornado异步

    http://www.tuicool.com/articles/36ZzA3 http://www.dongwm.com/archives/shi-yong-tornadorang-ni-de-qin ...

  9. 十六、Hadoop学习笔记————Zookeeper实战

    所有服务器都会先将自己的服务器信息注册到servers中,然后每台服务器都会尝试注册master,哪台注册成功,则哪台就是master服务器. 所有的服务器都会关注master节点的删除事件,这样通过 ...

  10. JAVA提高十八:Vector&Stack深入分析

    前面我们已经接触过几种数据结构了,有数组.链表.Hash表.红黑树(二叉查询树),今天再来看另外一种数据结构:栈. 什么是栈呢,我们先看一个例子:栈就相当于一个很窄的木桶,我们往木桶里放东西,往外拿东 ...