- 浏览: 186617 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
kjmmlzq19851226:
这个和排序米有关系吧
一个排好序的数组,找出两数之和为m的所有组合 -
ileson:
...
spring在web.xml中的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=http://java.sun.com/xml/ns/j2ee
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- spring 有三种启用模式 1.ContextLoaderServlet 2.ContextLoaderListener 3.ContextLoaderPlugIn-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml,/WEB-INF/struts-config-framework.xml
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<!-- Action -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 欢迎界面 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- Spring过滤中文字符集 -->
<filter>
<filter-name>SetCharacterEncoding</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>
</filter>
<!-- 要过滤的请求类型 -->
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 注册Spring的request作用域 -->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!--
request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,配置实例。request、session、global session使用的时候首先要在初始化web的web.xml中做如下配置:
如果你使用的是Servlet 2.4及以上的web容器,那么你仅需要在web应用的XML声明文件web.xml中增加下述ContextListener即可:
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
-->
<!-- OpenSessionInView -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--
Hibernate对延迟加载的实现原理是CGLIB动态字节码生成技术,即返回的实体并非真正的实体对象,而是经过CGLIB处理后的代理实体,当调用某一未经加载的属性时,代理实体就可以截获这一调用,然后由Hibernate实现动态加载。
如果要使用Hibernate的延迟加载特性,则渲染视图阶段不能关闭事务,因此,事务的范围变为整个HTTP请求的周期。
采用OpenSessionInView模式可以将事务范围界定在请求开始和渲染视图结束后,使得Hibernate的Session在视图渲染时仍有效。有两种方式实现OpenSessionInView模式,一种是使用Spring提供的OpenSessionInViewInterceptor,如果采用Spring MVC框架,可以将这个Interceptor加入到Controller的拦截器链中,事务在Controller处理前开始,在视图渲染后结束。
如果Web层没有采用Spring的MVC框架,而是使用Struts等其他MVC框架,甚至没有使用MVC框架,此时,就无法定义Interceptor,只能采用Filter来实现OpenSessionInView模式。
OpenSessionInViewFilter是Spring提供的一个Filter。在OpenSessionInViewFilter模式下,所有的HTTP请求都将被OpenSessionInViewFilter截获,事务在请求处理前开始,在请求处理完毕后结束,而不管采用何种MVC框架,甚至直接使用JSP。
两种方式各有优劣。OpenSessionInViewInterceptor只能用于Spring MVC,但是配置简单,无须过滤URL;OpenSessionInViewFilter适用范围更广,但是必须手动配置web.xml文件,并且必须正确过滤URL。
无论如何,采用以上两种方式的目的都是为了使用Hibernate的延迟加载特性。由于事务也是一种数据库资源,事务持续的时间越久,数据库资源被锁定也越久,应用程序的吞吐量就会降低。因此,要尽量将事务限定在最小的范围内
-->
<!-- session的过滤控制用户在登录时的权限限制-->
<filter>
<filter-name>authorizen</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--
以前学习框架经常做登录页面的demo,输入正确的id+pwd就返回成功了。。可是这种模式无法阻止通过URL直接访问其他的页面,在一个非玩具系统中,控制未登录用户的页面访问权限是一个基本功能。
从实现思路讲,验证一个用户的有效登录,大多采用的是登入时候向Session写一个User认证信息,然后在访问每个页面前来判断Session中是否有认证信息。
另外有很多网站提供记住登陆信息xx天,这种是结合了Cookie的认证信息存储。谈到这里,也可以仔细想想Cookie和Session的作用。比如卓越的购物车就是Cookie做的(因为关闭IE后再访问购物车还记得你的物品),而大多数群集Web服务器的信息也是采用Cookie(因为群集的Session同步开销很大),掌握了Cookie和Session的基本特性,这些都是理所当然的做法了。
-->
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>authorizen</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 设置监听,启动spring的一种模式,运行之后要去找上面的<context-param></context-param> -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--
今天有一个朋友问了我一个问题,他使用的是Struts+Spring+Hibernate架构,配置使用Spring的OpenSessionInView Filter,但是发现不生效,lazy的集合属性在页面访问的时候仍然报session已经关闭的错误。我和他一起检查了所有的配置和相关的代码,但是没有发现任何问题。经过调试发现,应用程序使用的Session和OpenSessionInView Filter打开的Session不是同一个,所以OpenSessionInView模式没有生效,但是为什么他们不使用同一个Session呢?
检查了一遍Spring的相关源代码,发现了问题的根源:
通常在Web应用中初始化Spring的配置,我们会在web.xml里面配置一个Listener,即ContextLoaderListener,
如果使用Struts,那么需要在Struts的配置文件struts-config.xml里面配置一个ContextLoaderPlugIn。
实际上ContextLoaderListener和ContextLoaderPlugIn的功能是重叠的,他们都是进行Spring配置的初始化工作的。因此,如果你不打算使用OpenSessionInView,那么你并不需要在web.xml里面配置ContextLoaderListener。
好了,但是你现在既需要Struts集成Spring,又需要OpenSessionInView模式,问题就来了!
由于ContextLoaderListener和ContextLoaderPlugIn功能重叠,都是初始化Spring,你不应该进行两次初始化,所以你不应该同时使用这两者,只能选择一个,因为你现在需要集成Struts,所以你只能使用ContextLoaderPlugIn。
但是令人困惑的是,ContextLoaderListener和ContextLoaderPlugIn有一个非常矛盾的地方!
ContextLoaderListener初始化spring配置,然后把它放在ServletContext对象里面保存:
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
保存的对象的key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE!
但是ContextLoaderPlugIn初始化spring配置,然后把它放在ServletContext对象里面保存:
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, WebApplicationContext);
这个attrName和WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE名字是不一样的!
如果仅仅是名字不一样,问题还不大,你仍然可以放心使用ContextLoaderPlugIn,但是当你使用OpenSessionInView的时候,OpenSessionInViewFilter是使用哪个key取得spring配置的呢?
WebApplicationContext wac =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
显然,OpenSessionInViewFilter是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个key去拿spring配置的!
我们整理一下思路:
ContextLoaderPlugIn保存spring配置的名字叫做attrName,ContextLoaderListener保存spring配置的名字叫做WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
而OpenSessionInView是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个名字去取得spring配置的!
而你的应用程序却是按照attrName去取得spring的配置的!
所以,OpenSessionInView模式失效!
解决办法:
修改ContextLoaderPlugIn代码,在getServletContext().setAttribute(attrName, wac);这个地方加上一行代码:
getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
或者修改OpenSessionInViewFilter,让它按照attrName去取得spring配置。
-->
发表评论
-
Struts2参数赋值的方式和原理
2018-03-15 14:06 551参数赋值方式 1.属性驱动 public cl ... -
Spring事务传播属性与事务隔离级别
2012-03-25 15:32 1164一、PROPAGATION(事务传 ... -
web项目中加载spring容器的方式
2012-02-26 17:14 12021、在Web.xml中,通过ContextLoaderList ... -
Spring加载配置文件的几种方式
2012-02-26 17:02 5098第一种:使用XmlBeanFactory+Resource ... -
Hibernate 与 Spring 多数据源的配置【转载】
2012-01-09 11:48 886Hibernate 与 Spring 多数据源的配置【转载】 ... -
SSH中操作Oracle Clob类型字段
2011-10-26 17:03 1506说明:使用以下方法时首先确认你已经配置好了 如果运行报:or ... -
Spring让LOB数据操作变得简单易行
2011-10-26 15:43 875本文讲解了在 Spring 中处理 LOB 数据 ... -
Spring源码学习
2011-09-22 12:01 869a) Spring 初始化的时候首先要运行的类 ... -
Spring Quartz 任务调度配置二
2011-05-10 15:04 8931、Spring 配置文件模板 <?xml ve ... -
Spring Quartz 任务调度配置一
2011-05-10 14:56 9301、Spring 配置文件模板 ... -
struts+spring+hibernate整合
2011-04-25 14:42 892本文基于Struts1.3.10+Spring2.5.6+Hi ... -
Hibernate的三种连接池设置C3P0、dbcp和Proxool
2011-04-13 16:17 797<!-- JDBC驱动程序 --> <p ... -
Java框架数据库连接池比较(c3p0,dbcp和proxool)
2011-04-13 16:07 911现在常用的开源数据连接池主要有c3p0,dbcp和proxoo ... -
Struts+Spring+JPA整合
2011-04-13 10:51 1414本文基于Struts1.3.10+Spring ... -
dwr配置文件
2011-04-12 13:15 855dwr.xml是DWR的配置文件。 ... -
struts2+spring+dwr+ibatis整合开发的最精简jar组合
2011-03-17 18:13 1239struts 2应用只需要下列几个必须的jar: common ... -
spring+dwr
2011-03-17 18:07 794首先开发环境是eclipse.这里需要hibernate3.2 ...
相关推荐
spring-mvc.xml spring-mybatis.xml web.xml log4j.properties,项目中需要用到的配置文件。直接可用。
spring无web.xml零配置,使用javaconfig来进行spring配置,本项目使用maven管理,
spring在web.xml中和在struts中的不同配置..pdf
加载spring 文件,在web.xml中的配置
本篇文章主要介绍了Spring mvc的web.xml配置说明,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring无web.xml的jdbctemplate配置
spring在web.xml中和在struts中的不同配置.[收集].pdf
主要介绍了Spring在web.xml中的配置详细介绍的相关资料,需要的朋友可以参考下
<param-value>/WEB-INF/spring-config.xml <!-- 备注:此所设定的参数,在JSP网页中可以使用下列方法来取得:${initParam.param_name} 若在Servlet可以使用下列方法来获得:String param_name=getServletContext...
SpringMVC基于代码的配置方式(零配置,无web.xml) 本资源博客地址:http://blog.csdn.net/u013816347/
这份文件 把javaEE 中web.xml中的基本配置都 写在里面了
在web.xml中配置spring容器的监听器。 2:项目集成springmvc 在web.xml中配置前端控制器 3:项目集成shiro 在web.xml中配置shiro过滤器 4:项目post乱码处理 在web.xml中配置字符过滤器 5:项目运行...
NULL 博文链接:https://200cc.iteye.com/blog/2192843
spring4的“所有jar包”+applicationContext.xml+web.xml====全套
绍spring在web.xml中和在struts中的不同配置
ssm一个简单项目所需的xml配置文件,spring+springmvc+mybatis框架中用到了三个XML配置文件:web.xml,spring-mvc.xml,spring-mybatis.xml.第一个不用说,每个web项目都会有的也是关联整个项目的配置.第二个文件spring-...
java servlet spring web filter listenr
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...
springMVC零配置,无web.xml,无spring配置,纯java
主要介绍了Spring手动生成web.xml配置文件过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下