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 ...
随机推荐
- windows系统扩展C盘的工具推荐(解决了C盘和压缩卷不相邻无法扩展C盘问题)
1.下载分区工具 “分区助手3.0中文版” 下载地址:http://www.33lc.com/soft/14880.html 2.下载下来是一个压缩包,解压后运行安装程序. 3.安装完成后按以下步骤执 ...
- JSP 学习笔记
JSP 全名为Java Server Pages,中文名叫java 服务器页面,它是在传统的 HTML 页面中插入 JAVA 代码片段和 JSP 标签形成的文件. 上一篇 Servlet 中只是讲解了 ...
- 【5】namenode启动过程
1.格式化空间(第一次启动的操作): 命令:bin/hadoop -format //用于格式化HDFS,如果不是首次格式化,需要删除下面配置的tmp目录后再进行core-site.xml的配置: / ...
- github设置
ssh-key: https://help.github.com/articles/generating-ssh-keys http://segmentfault.com/q/101000000013 ...
- 浅谈Maven
最近的有用到Maven 所以就大概跟大家谈一下Maven 1什么是Maven? Maven我个人的理解就是项目架包管理 通俗易懂的来说就是管理配置你项目中的所有jar包的 废话不多说 Maven去ht ...
- iOS获取所有机型
1.手机系统版本:9.1 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; 2.手机类型:iPhone 6 NSSt ...
- Science发表的超赞聚类算法
作者(Alex Rodriguez, Alessandro Laio)提出了一种很简洁优美的聚类算法, 可以识别各种形状的类簇, 并且其超参数很容易确定. 算法思想 该算法的假设是类簇的中心由一些局部 ...
- python利用for..in遍历,while循环嵌套编译九九乘法表的几种模式
运用for....in...遍历的四种方向的九九乘法表: 左下角: import sysfor i in range(1,10): for j in range(1,i+1): sys.stdout. ...
- Python——正则表达式
此篇文章结合小甲鱼的笔记和视频整理. 1 编译 Python 通过 re 模块为正则表达式引擎提供一个接口,同时允许你将正则表达式编译成模式对象,并用它们来进行匹配. 正则表达式被编译为模式对象,该对 ...
- sklearn 划分数据集。
1.sklearn.model_selection.train_test_split随机划分训练集和测试集 函数原型: X_train,X_test, y_train, y_test =cross_v ...