2011-10-15 115 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中的类加载器层次结构以及用于加载监听器的类加载器(以及用于该类加载器的类)有关。

相关问题