Spring中的WebAppRootListener
Posted on: 2013-10-16, Last modified: 2015-07-31, View: 3123
Posted on: 2013-10-16, Last modified: 2015-07-31, View: 3123
这个listner的作用就是监听web.xml中的配置para-name为webAppRootKey的值,比如我的web应用为tsts,那么我配置这样一个
<context-param> <param-name>webAppRootKey</param-name> <param-value>tsts.root</param-value> </context-param>
然后再配置这样一个监听器:
<listener> <listener-class> org.springframework.web.util.WebAppRootListener </listener-class> </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的做法更可以借鉴!