caike

优秀是一种习惯,生命是一种过程,两点之间最短的距离并不一定是直线,只有知道如何停止的人才知道如何加快

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  3 随笔 :: 2 文章 :: 19 评论 :: 0 Trackbacks
需要做一个参数初始化类,当web应用被加载时从数据库里取出相关的参数设置

,并把这些参数放置到application里,jsp页面可以从中取出。

1.在web.xml中配置:
<servlet>
        
<servlet-name>Dispatcher</servlet-name>
        
<servlet-

class>org.springframework.web.servlet.DispatcherServlet</servlet-

class
>
        
<init-param>
            
<param-name>contextConfigLocation</param-name>
            
<param-value>/WEB-INF/Dispatcher-

servlet.xml,/WEB-INF/applicationContext.xml
</param-value>
        
</init-param>
        
<load-on-startup>1</load-on-startup>
    
</servlet>

    
<servlet>
        
<servlet-name>context</servlet-name>
        
<servlet-

class>org.springframework.web.context.ContextLoaderServlet</servlet-

class
>
        
<load-on-startup>2</load-on-startup>
    
</servlet>

    
<servlet>
        
<servlet-name>InitialServlet</servlet-name>
        
<servlet-

class>com.anylinks.billreturn.Web.InitialServlet</servlet-class>
        
<load-on-startup>3</load-on-startup>
    
</servlet>

2.servlet代码

package com.anylinks.billreturn.Web;

import java.util.Collection;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import

org.springframework.web.context.support.WebApplicationContextUtils;

import com.anylinks.billreturn.BO.SysParameter;
import com.anylinks.billreturn.Service.ISysParameterService;

/*
 * 初始化Servlet,从数据库中读取参数表,保存在application里
 * @author 蔡科
 * 创建日期:2006-1-9
 
*/
public class InitialServlet extends HttpServlet {

    
private Log log = LogFactory.getLog(this.getClass());

    
private ISysParameterService sysParameterService;

    
/**
     * 从数据库中读取参数表,保存在application里
     *
     * 
@throws ServletException
     *             if an error occure
     
*/
    
public void init() throws ServletException {

        log.debug(
"start to intitail ");
        
// 获取WebApplicationContext
        ServletContext application = getServletContext();
        WebApplicationContext wac 
= WebApplicationContextUtils
                .getWebApplicationContext

(application);

        
// 调用sysParameterService取出所有的系统参数
        sysParameterService = (ISysParameterService) wac
                .getBean(
"sysParameterService");

        Collection paras 
=

sysParameterService.findAllParameters();
        log.debug(
"sys parameters size:" + paras.size());

        
// 把参数加到application里去
        for (Iterator iter = paras.iterator(); iter.hasNext

();) {
            SysParameter para 
= (SysParameter) iter.next

();

            application.setAttribute(para.getParaName(),

para.getParaValue());

            log.debug(
"initial parameter: key=" +

para.getParaName()
                    
+ ", value=" +

para.getParaValue());

        }
    }

}



需要注意的地方:
1.仅仅配置一个DispatcherServlet是不够的,我开始就是这样,然后再servlet

里面怎么取都取不到WebApplicationContext 。配置上

org.springframework.web.context.ContextLoaderServlet之后才能取的到

WebApplicationContext 。
2.注意一下<load-on-startup>3</load-on-startup>,因为用到spring的

hibernateDaoSupport,所以必须在spring加载完之后再加载InitialServlet.

posted on 2006-01-09 16:05 caike 阅读(4634) 评论(1)  编辑  收藏 所属分类: SpringFramework

评论

# re: 从servlet中获取spring的WebApplicationContext[未登录] 2010-03-18 11:12 wendy
写的不错,

http://shop60651194.taobao.com/ 呵呵。。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: