2011-10-15 154 views
1

我正在開發eclipse gallileo,struts和tomcat。 以下是我的web.xml文件服務器啓動時監聽器類不執行|| tomcat || eclipse

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>streetmall</display-name> 
<context-param> 
    <param-name>imagePath</param-name> 
    <param-value>D:\\OrderID_images</param-value> 
</context-param> 
<listener> 
    <listener-class>jaha.Customer.util.ApplicationScopeInit</listener-class> 
</listener> 
<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</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>1</load-on-startup> 
</servlet> 
<servlet> 
    <servlet-name>action_tmp</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</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> 
<servlet-mapping> 
    <servlet-name>action</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
    <welcome-file>welcome.jsp</welcome-file> 
</welcome-file-list> 

<login-config> 
    <auth-method>BASIC</auth-method> 
</login-config> 

    <description>MySQL Test App</description> 
    <resource-ref> 
     <description>DB Connection</description> 
     <res-ref-name>jdbc/TestDB</res-ref-name> 
     <res-type>javax.sql.DataSource</res-type> 
     <res-auth>Container</res-auth> 
    </resource-ref></web-app> 

當我startig我的Tomcat服務器,是沒有得到執行我的利斯特類我的問題。即使我已經推出了一些System.out.println()。這些消息也沒有在控制檯上..請幫助我如何擺脫這一點。

早些時候我正在上課沒有發現異常。但隨後我創建了新項目並進行部署。現在我在這個問題 感謝

public class ApplicationScopeInit implements ServletContextListener 
{ 

    public void contextInitialized(ServletContextEvent event) 
    { 
     try 
     { 
      ClassLoader loader = Thread.currentThread().getContextClassLoader(); 

      System.out.println("Use context classloader to read states.properties"); 
      InputStream iStream = loader.getResourceAsStream("jaha/Customer/states.properties"); 
      Properties props = new Properties(); 
      System.out.println("Load the stream into the properties object directly"); 
      props.load(iStream); 

      //Look up by key and load them into a ArrayList as NameValuePair collection 
      Enumeration keyEnum = props.propertyNames(); 
      // Use a Sorted Set to hold the state names and values 
      // Define an anonymous inner class to provide 
      // the comparison algorithm to the TreeSet 
      @SuppressWarnings("unchecked") 
      Set stateSet = new TreeSet(
        new Comparator() 
        { 
         public int compare(Object a, Object b) 
         { 
          LabelValueBean nvpA = (LabelValueBean) a; 
          LabelValueBean nvpB = (LabelValueBean) b; 

          String valA = nvpA.getLabel(); 
          String valB = nvpB.getLabel(); 
          return valA.compareTo(valB); 
         } 
        } 
      ); 

      LabelValueBean nvp = null; 
      String keyName = null; 
      String label = null; 
      while (keyEnum.hasMoreElements()) 
      { 
       keyName = (String) keyEnum.nextElement(); 
       label = props.getProperty(keyName); 
       nvp = new LabelValueBean(label, keyName); 
       stateSet.add(nvp); 
      } 

      System.out.println("Get ServletContext and set the properties as a application scope object"); 
      ServletContext context = event.getServletContext(); 
      context.setAttribute("STRUTS_EXAMPLE_STATES", stateSet); 


      //Load Carriers - FedEx, UPS etc.. 
      List carrierList = new ArrayList(); 
      carrierList.add(new LabelValueBean("UPS", "UPS")); 
      carrierList.add(new LabelValueBean("USPS", "USP")); 
      carrierList.add(new LabelValueBean("FedEx", "FDX")); 

      context.setAttribute("STRUTS_EXAMPLE_CARRIERS",carrierList); 
      //.setAttribute("STRUTS_EXAMPLE_CARRIERS",carrierList); 
     } 
     catch (IOException ioe) 
     { 
      ioe.printStackTrace();  
     } 
     try{ 
      System.out.println("Populating category on server startup"); 
      HashMap<String, ArrayList<CategorydetailObject>> hm= CategoryManager.PopulateCategory(); 
      ServletContext context = event.getServletContext(); 

      context.setAttribute("PRODUCTS", hm); 



     } 
     catch(Exception e) 
     { 
      System.out.println("$$$$$$$$$$$$$$$$$$$$$ PROBLEM IN INITIALIZATION ALL PARAMETERS"); 

     } 
    } 

    public void contextDestroyed(ServletContextEvent event) 
    { 
     event.getServletContext().removeAttribute("STRUTS_EXAMPLE_STATES"); 
     event.getServletContext().removeAttribute("STRUTS_EXAMPLE_CARRIERS"); 
     event.getServletContext().removeAttribute("PRODUCTS"); 

    } 

} 
+0

我們能否看到您的聽衆班? – jeff

+0

嗨,我已經添加了類plesase看。 – Pedantic

+0

我使用了您的設置,並且在服務器啓動時能夠看到我的消息打印。你確定服務器啓動正確嗎?沒有堆棧跟蹤? – jeff

回答

0

上maven的Web項目右鍵點擊然後在展開組件的目標文件夾中添加文件夾的偵聽器類。

例如,如果執行的ServletContextListener存在於包com.company.listeners,展開組件應具有以下文件夾映射:

target/<applicationname>/WEB-INF/classes/com/company/listeners 

應該被映射到

WEB-INF/classes/com/company/listeners 

我認爲這與tomcat中的類加載器層次結構以及用於加載監聽器的類加載器(以及用於該類加載器的類)有關。

相關問題