Spring MVC新手教程(二)
第一篇文章宏观讲了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新手教程(二)的更多相关文章
- Spring MVC新手教程(一)
直接干货 model 考虑给用户展示什么.关注支撑业务的信息构成.构建成模型. control 调用业务逻辑产生合适的数据以及传递数据给视图用于呈献: view怎样对数据进行布局,以一种优美的方式展示 ...
- Spring MVC 入门教程示例 (一)
今天和大家分享下 Spring MVC 入门教程 首先还是从 HelloWorld web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- spring mvc入门教程 转载自【http://elf8848.iteye.com/blog/875830】
目录 一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...
- Spring Cloud 入门教程(二): 配置管理
使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring ...
- 160506、Spring mvc新手入门(11)-返回json 字符串的其他方式
Spring MVC返回 json字符串的方式有很多种方法,这里介绍最简单,也是最常使用的两种方式 一.使用 PrintWriter printWriter 直接输出字符串到返回结果中 不需 ...
- 手写Spring MVC框架(二) 实现访问拦截功能
前言 在上一篇文章中,我们手写了一个简单的mvc框架,今天我们要实现的功能点是:在Spring MVC框架基础上实现访问拦截功能. 先梳理一下需要实现的功能点: 搭建好Spring MVC基本框架: ...
- Spring MVC入门(二)—— URI Builder模式
URI Builder Spring MVC作为一个web层框架,避免不了处理URI.URL等和HTTP协议相关的元素,因此它提供了非常好用.功能强大的URI Builder模式来完成,这就是本文重点 ...
- spring MVC入门教程
写一个spring mvc后台传值到前台的一个小例子. 分为以下几个步骤: 1.创建web项目. 导入项目包.具体有如下: spring-aop-4.0.4.RELEASE.jar spring-be ...
随机推荐
- mac android apk反编译
在mac os系统上反编译android apk,首先需要准备好以下3个文件: 1.apktool:https://ibotpeaches.github.io/Apktool/install/ 2.d ...
- HTTP请求过程(http是一种无状态协议,即不建立持久的连接)
一.一个完整的HTTP请求,通常有7个步骤: 1.建立TCP连接: 2.web浏览器向web服务器发送请求命令: 3.浏览器发送请求头信息: 4.服务器应答: 5.服务器发送应答头信息: 6.服务器向 ...
- 《天书夜读:从汇编语言到windows内核编程》六 驱动、设备、与请求
1)跳入到基础篇的内核编程第7章,驱动入口函数DriverEnter的返回值决定驱动程序是否加载成功,当打算反汇编阅读驱动内核程序时,可寻找该位置. 2)DRIVER_OBJECT下的派遣函数(分发函 ...
- Spring 高级依赖注入方式
1.处理自动装配的歧义性 1.1 标记首选的bean 使用@Primary 来说明一个bean是首选的. @Component @Primary public class GuoRongCD im ...
- [转载] Java集合---HashMap源码剖析
转载自http://www.cnblogs.com/ITtangtang/p/3948406.html 一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射 ...
- markdown使用小结
初学时,对不太熟悉的markdown语法,有个简单记录 公式 公式一般用Latex书写,在线Latex编辑器可以使用,有以下几种方法供选择 有然后保存为图片gif格式,使用img标签进行引用. 使用G ...
- 准备冲锋 golang入坑系列
史前摘要: 本来想写读前必读,但连续几篇博文都写读前必读,感觉就没有了新意. 所以换成史前摘要,反正是一个意思. 此摘要的目的仍然是提醒点击而来的同学,本系列最新文章在这里.放到博客园的目的是为了方便 ...
- JAVA面试之集合框架(三)
21.ArrayList和Vector的区别 这两个类都实现了List接口(List接口继承了Collection接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种动态 ...
- jqGrid数据表格
方式一: <!DOCTYPE html><html><head><meta charset="utf-8" /><title& ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(88)-Excel导入和导出-自定义表模导出
前言 之前说了导入和导出,也提供了自定义的表模的导入,可见LinqToExcel可以做的事情不仅仅如此 这次我们来演示比较复杂的导出Excel,导出复杂的Excel与导入复杂的Excel原理基本是一样 ...