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 閱讀(4654) 評論(1)  編輯  收藏 所屬分類: SpringFramework

          評論

          # re: 從servlet中獲取spring的WebApplicationContext[未登錄] 2010-03-18 11:12 wendy
          寫的不錯,

          http://shop60651194.taobao.com/ 呵呵。。  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 遵义市| 务川| 同德县| 建湖县| 宁城县| 古丈县| 西乡县| 阿瓦提县| 太湖县| 威海市| 惠州市| 丹巴县| 屏边| 阿瓦提县| 吉隆县| 兴业县| 浮梁县| 宿迁市| 轮台县| 霸州市| 揭西县| 建瓯市| 焉耆| 潞城市| 班玛县| 文昌市| 饶阳县| 寿光市| 望谟县| 海安县| 金沙县| 斗六市| 连城县| 小金县| 和政县| 武功县| 九龙城区| 手游| 三河市| 靖边县| 合川市|