Spring中的WebAppRootListener

来源:互联网 发布:数组排序jquery 编辑:程序博客网 时间:2024/05/01 00:41

Spring中的WebAppRootListener


 这个listner的作用就是监听web.xml中的配置para-name为webAppRootKey的值,比如我的web应用为tsts,那么我配置

这样一个

 

  1. <context-param>
  2.         <param-name>webAppRootKey</param-name>
  3.         <param-value>tsts.root</param-value>
  4.     </context-param>

,然后再配置这样一个监听器:

 

  1. <listener>
  2.         <listener-class> 
  3.             org.springframework.web.util.WebAppRootListener
  4.         </listener-class>
  5.     </listener>

 

。这个监听器就会在web上下文初始化的时候,调用webUtil的对应方法,首先获取到param-name对应的param-value ,然后,根据传递进去的ServletContext对象得到web的物理路径:String root = servletContext.getRealPath("/");

 

接着把这个param-value作为key,root作为value放到system中System.setProperty(key, root);

 

然后再web中可以用 System.get.....就可以得到web的跟目录的物理路径了。

 

 

之前我的做法是用一个filter,在filter的init中调用String root = servletContext.getRealPath("/");,然后再去设置对应一个常量类文件的属性。做法差不多,但是spring的做法更可以借鉴!

0 0
原创粉丝点击