Spring Web MVC 随笔
1.ContextLoaderListener
对于使用Spring的Web应用,无需手动创建Spring容器,而是通过配置文件声明式地创建Spring容器。可以直接在web.xml文件中配置创建Spring容器。
Spring提供了一个ContextLoaderListener,该监听器类实现了ServletContextListener接口,它会在创建时自动查找WEB-INF/下的applicationContext.xml文件。因此,如果只有一个配置文件,且文件名为applicationContext.xml,则只需在web.xml文件中增加如下配置片段即可。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果有多个配置文件需要载入,则考虑使用<context-param…/>元素来确定配置文件的文件名。
ContextLoaderListener加载时会查找名为contextConfigLocation的初始化参数,如下:
<!--指定多个配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-context.xml,
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<!--使用ContextLoaderListener初始化Spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果没有使用contextConfigLocation指定配置文件,则Spring会自动查找WEB-INF/下的applicationContext.xml文件。
如果有contextConfigLocation,则使用该参数确定的配置文件。
如果无法找到合适的配置文件,Spring将无法正常初始化。
2.可能遇到的异常
在配置Spring MVC时,可能会遇到这样的错误:
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessioorg.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current threadnContext.java:134)
那么在web.xml文件中添加如下配置即可:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
OpenSessionInViewFilter的主要功能是用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。
Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。
Spring Web MVC 随笔的更多相关文章
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块
spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...
- 菜鸟学习Spring Web MVC之二
有文章从结构上详细讲解了Spring Web MVC,我个菜鸟就不引据来讲了.说说强悍的XP环境如何配置运行环境~~ 最后我配好的环境Tomcat.Spring Tool Suites.Maven目前 ...
- 4.Spring Web MVC处理请求的流程
- 1.Spring Web MVC有什么
Spring Web MVC使用了MVC架构模式的思想,将web层进行职责解耦. 同样也是基于请求驱动的,也就是使用请求-响应模型.它主要包含如下组件: DispatcherServlet :前端控制 ...
- Spring REST实践之Spring Web MVC
Spring概要 Spring Framework提供了依赖注入模型和面向切面编程,简化了基础型代码的编写工作以及更好的能够与其它框架和技术整合起来.Spring Framework由data acc ...
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...
- Features of Spring Web MVC
21.1.1 Features of Spring Web MVC Spring Web Flow Spring Web Flow (SWF) aims to be the best solution ...
- [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...
- Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
随机推荐
- 谈谈对XML的理解?说明Web应用中Web.xml文件的作用?
谈谈对XML的理解?说明Web应用中Web.xml文件的作用? 解答:XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard ...
- asp.net调用系统设置字体文本框的方法
本文实例展示了asp.net调用系统设置字体文本框的方法,是进行web开发中很实用的技巧.具体实现步骤如下: 一.调用系统字体文本框 首先在bin文件夹右击-->添加引用-->.net标签 ...
- 苹果手机输入中文不会触发onkeyup事件
今天同事的项目有这个问题,用我的安卓手机输入中文是ok的,但是苹果手机就不行 使用keyup事件检测文本框内容: $('#keyup_i').bind('keyup', function(){ ...
- python数字类型
Numbers >>> 5+4 9 >>> (6/2)+5 8.0 >>> >>> print(17/3) 5.66666666 ...
- 10013: An attempt was made to access a socket in a way forbidden by its access permissions
nginx的error.log日志报错: 2018/01/25 11:55:22 [emerg] 3380#15488: bind() to 0.0.0.0:20003 failed (10013: ...
- linux ftp 上传与下载命令解析
month=`date -d "last month" +"%Y%m"` year=`date +"%Y"` rm /home/yourDi ...
- cocos2d-X学习之主要类介绍:场景(CCScene)
场景(CCScene) 类结构: CCScene主要有以下两个函数: bool init () //初始化函数 static CCScene * node (void) //生CCScene 作为 ...
- Centos6.5 DNS配置
服务器端:192.168.186.130 1.安装 # yum -y install bind* 2.主要配置文件 [root@localhost named]# vim /etc/named.con ...
- 巨蟒python全栈开发-第16天 核能来袭-初识面向对象
一.今日内容总览(上帝视角,大象自己进冰箱,控制时机) #转换思想(从面向过程到面向对象) 1.初识面向对象 面向过程: 一切以事物的发展流程为中心. 面向对象: 一切以对象为中心,一切皆为对象,具体 ...
- C# WinForm 只运行一次的MDI子窗体
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void ToolColle ...