spring MVC学习笔记
为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平。你的一个决定会影响团队未来的几年。要考虑方面太多:
1、简单易用,以提高开发效率。使小部分的精力在框架上,大部分的精力放在业务上。
2、性能优秀,这是一个最能吸引眼球的话题。
3、尽量使用大众的框架(避免使用小众的、私有的框架),新招聘来的开发人员有一些这方面技术积累,减低人员流动再适应的影响。
如果你还在为这件事件发愁,本文最适合你了。选择Spring MVC吧。
Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对Struts 2的超越。现在越来越多的开发团队选择了Spring MVC。
Struts2也是非常优秀的MVC构架,优点非常多比如良好的结构,拦截器的思想,丰富的功能。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降,应避免使用这些功能。而Struts2的多层拦截器、多实例action性能都很好。可以参考我写的一篇关于Spring MVC与Struts2与Servlet比较的文章《Struts2、SpringMVC、Servlet(Jsp)性能对比 测试》
Spring3 MVC的优点:
Spring3 MVC使用简单,学习成本低。学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。
Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)
Spring3 MVC的灵活是你无法想像的,Spring框架的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。
让我们能非常简单的设计出干净的Web层和薄薄的Web层;
进行更简洁的Web层的开发;
天生与Spring框架集成(如IoC容器、AOP等);
提供强大的约定大于配置的契约式编程支持;
能简单的进行Web层的单元测试;
支持灵活的URL到页面控制器的映射;
非常容易与其他视图技术集成,如Velocity、FreeMarker等等,因为模型数据不放在特定的API里,而是放在一个Model里(Map数据结构实现,因此很容易被其他框架使用)
非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的API;
提供一套强大的JSP标签库,简化JSP开发;
支持灵活的本地化、主题等解析;
更加简单的异常处理;
对静态资源的支持;
支持Restful风格。
1.web.xml文件的配置
典型配置
配置一
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
配置二
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件
</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
1.1.init-param
(1)、如果不指定spring配置文件路径,也就是不写init-param,那么应用约定大于配置原则,默认为web-inf/[servlet名称]-servlet.xml配置文件.
(2)、部署之后,classes目录就在web-inf目录下.<param-value>/WEB-INF/classes/springMVC.xml</param-value>
(3)、替代方法2的更好的方式<param-value>classpath:springMVC.xml</param-value>
(4)、多个值用逗号分隔
(5),支持模糊匹配,*代表任意文件,**代表可以跨越目录的通配符.
最佳实践是使用<init-param>,把配置文件放在classpath下,即<param-value>classpath:任意名称.xml</param-value>
<init-param>
<description>
加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件
</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
1.2.url-pattern
servlet路径常见有如下两种
(1)./拦截全部
(2).*.do 或者*.action或者*.htm等,后缀是任意的,只要与Controller的映射一致即可,例如写作/user/add.do,/user/add.htm.这是最传统的方式,最简单也最实用,不会导致静态文件(jpg,js,css)被拦截。
直接写'*'是会报错的,直接写/*也是错误的.Servlet跟Listener不一样,Servlet像文件一样,Listener像文件夹一样.所以'/*'不能作为Servlet的地址.Servlet的地址必须像一个文件一样.
最佳实践是使用'/'拦截全部.
1.3 ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
一个应用程序可以有多个ApplicationContext上下文,通过ContextLoader和DispatcherServlet两种配置可以得到两个ApplicationContext对象,这两个对象所在的位置不同,用途也不一样.其中DispatcherServlet的ApplicationContext是必需的.
ContextLoaderListener方式
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
DispatcherServlet方式
<servlet>
<servlet-name>mvcServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/config/applicationContextMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
ContextLoaderListener中加载的context成功后,spring 将 applicationContext存放在ServletContext中key值为"org.springframework.web.context.WebApplicationContext.ROOT"的attribute中.(servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context));可以通过WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)或WebApplicationContextUtils.getWebApplicationContext(servletContext)方法来获取对应的applicationContext。
网上许多地方写得很不清楚,具体怎么初始化的还需要研究研究.
1.4.编码过滤器
编码过滤器的作用是把请求的编码类型编程utf-8
<filter>
<filter-name>encodingFilter </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>
<init-param>
<param-name>forceEncoding </param-name>
<param-value>true </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter </filter-name>
<url-pattern>*.do </url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
2.spring第一步:从配置文件里读取spring配置
ApplicationContext context = new ClassPathXmlApplicationContext("SpringHelloWorld.xml");
会报错资源泄漏,因为ClassPathXmlApplicationContext这个类加载了文件资源,一直没有释放。ClassPathXmlApplicationContext这个类有close()方法,而ApplicationContext这个类却没有close()方法,ApplicationContext是ClassPathXmlApplicationContext的父类。
写法有如下三种:
写法一:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringHelloWorld.xml");
context.close();
写法二:
try(ApplicationContext context = new ClassPathXmlApplicationContext("SpringHelloWorld.xml")){
......
}
写法三:
把ApplicationContext context = new ClassPathXmlApplicationContext("SpringHelloWorld.xml");这个context变量设置为类的成员变量,这样就不需要管理啥时候释放了.
3.springmvc-servlet.xml的配置
3.1根节点引入命名空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 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">
根节点是beans,它默认的命名空间是beans命名空间,其他命名空间包括xsi,context,mvc,util,aop,tx等,其中xsi是XML标准中自带的,其余是spring的.通过xsi:schemaLocation来定义命名空间的定义位置.
如果schemaLocation不写版本号,那么默认就是最新的.所以最好不要写版本号.
3.2属性的两种写法(以视图解析器为例)
p:属性名和子标签效果是一样的.
<!-- scan the package and the sub package -->
<context:component-scan base-package="test.SpringMVC"/>
<!-- don't handle the static resource -->
<mvc:default-servlet-handler />
<!-- configure the InternalResourceViewResolver -->
视图解析器写法一
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
视图解析器写法二
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
3.3通过xml进行映射
注解方式比XML方式更好,但是需要了解XML方式
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<prop key="test.htm">testController</prop>
</props>
</property>
</bean>
<!-- 指定了表现层资源的前缀和后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<!--请求/处理单元关系映射-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="testController" class="com.zhkj.cms.controller.TestController"
p:commandClass="com.zhkj.cms.controller.TestFrom"
p:fail_view="fail"
p:success_view="success" />
3.4文件上传
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>
解析时有两种写法:方法一,手动解析
@RequestMapping(value="upload2")
public String upLoad2(HttpServletRequest request, HttpServletResponse response)
throws IllegalStateException, IOException{
//解析器解析request的上下文
CommonsMultipartResolver multipartResolver =
new CommonsMultipartResolver(request.getSession().getServletContext());
//先判断request中是否包涵multipart类型的数据,
if(multipartResolver.isMultipart(request)){
//再将request中的数据转化成multipart类型的数据
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator iter = multiRequest.getFileNames();
while(iter.hasNext()){
MultipartFile file = multiRequest.getFile((String)iter.next());
if(file != null){
String fileName = file.getOriginalFilename();
String path = "D:/" + fileName;
File localFile = new File(path);
//写文件到本地
file.transferTo(localFile);
}
}
}
return "/success";
}
方法二:
public String login(@Valid UserInfo userInfo, BindingResult result, @RequestParam MultipartFile[] files, Model model) {
......
}
3.5处理静态资源
不需要使用<mvc:resources>说明那些文件是资源
<mvc:default-servlet-handler />
spring MVC学习笔记的更多相关文章
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...
- Spring MVC 学习笔记11 —— 后端返回json格式数据
Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...
- Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息
</pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...
- Spring MVC 学习笔记9 —— 实现简单的用户管理(4)用户登录显示局部异常信息
Spring MVC 学习笔记9 -- 实现简单的用户管理(4.2)用户登录--显示局部异常信息 第二部分:显示局部异常信息,而不是500错误页 1. 写一个方法,把UserException传进来. ...
- Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录
Spring MVC 学习笔记8 -- 实现简单的用户管理(4)用户登录 增删改查,login 1. login.jsp,写在外面,及跟WEB-INF同一级目录,如:ls Webcontent; &g ...
- Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目
Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目 Spring Tool Suite 是一个带有全套的Spring相关支持功能的Eclipse插件包. ...
- Spring MVC 学习笔记1 - First Helloworld by Eclipse【& - java web 开发Tips集锦】
Spring MVC 学习笔记1 - First Helloworld by Eclipse reference:http://www.gontu.org 1. 下载 Spring freamwork ...
- Spring MVC 学习笔记(整理)
SpringMVC学习 1.概述 Spring MVC是一种基于Java实现MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行解耦,基于请求-响应模型帮助我们 ...
随机推荐
- linux cpu占有率居高不下 调试
今天调试程序,使用top命令后,发现程序的cpu占有率很高,一直在99,这很可怕,所以来调试. 使用top命令,得如下结果 PID USER PR NI VIRT RES SHR S %CPU %ME ...
- ElasticSearch 日期赋值
Nxlog date to elasticsearch elasticsearch会自动检测日期类型,"2016-03-31 22:09:42"会当作字符串,"2 ...
- Vim光标定位
*定位到指定行n: 输入"nG". 或输入"ngg". 或输入":n" 这里,n就是指定的行的行号.注意,有时候G=gg. *跳到屏幕顶部: ...
- android AsyncTask实例
.java package com.example.activitydemoay; import android.app.Activity; import android.content.Intent ...
- @Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml
项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置: <context:property-placeholder ignore ...
- 洛谷U4727小L的二叉树[树转序列 LIS]
题目背景 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣. 所以,小L当时卡在了二叉树. 题目描述 在计算机科学中,二叉树是每个结点最多有两个子结点的 ...
- tyvj[1089]smrtfun
描述 现有N个物品,第i个物品有两个属性A_i和B_i.在其中选取若干个物品,使得sum{A_i + B_i}最大,同时sum{A_i},sum{B_i}均非负(sum{}表示求和). 输入格式 ...
- 使用javascript对密码进行有密码强度提示的验证
好些网站的注册功能中,都有对密码进行验证并且还有强度提示.下面就来实现这种效果.密码强度说明:密码强度:弱——纯数字,纯字母,纯符号密码强度:中——数字,字母,符号任意两种的组合密码强度:强——数字, ...
- BZOJ 3295 【Cqoi2011】 动态逆序对
Description 对于序列\(A\),它的逆序对数定义为满足\(i<j\),且\(A_i>A_j\)的数对\((i,j)\)的个数.给\(1\)到\(n\)的一个排列,按照某种顺序依 ...
- FastReport 隐藏matrix的列如何实现
fastReport在处理 行列都是浮动的时候非常好用. 但是也有一些不太直观的东西.比如说要设置生成结果的某行某列. 修改字体颜色背景色,隐藏,设置行高,设置列宽等等. 可以在 matrix的aft ...