2014-04-13 28 views
1

M對JAVA來說很新穎。 嘗試使用spring mvc構建應用程序。 m只是試圖創建一個簡單的表單來註冊一個石英工作。找不到WebApplicationContext:沒有ContextLoaderListener註冊?一個

JobController.java

package com.shopzilla.monitor.controller; 

    import java.io.IOException; 
    import java.util.ArrayList; 
    import java.util.Date; 
    import java.util.Map; 
    import javax.servlet.http.HttpServletRequest; 
    import org.quartz.CronScheduleBuilder; 
    import org.quartz.CronTrigger; 
    import org.quartz.JobBuilder; 
    import org.quartz.JobDetail; 
    import org.quartz.JobKey; 
    import org.quartz.Scheduler; 
    import org.quartz.SchedulerFactory; 
    import org.quartz.TriggerBuilder; 
    import org.quartz.impl.StdSchedulerFactory; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.validation.BindingResult; 
    import org.springframework.web.bind.annotation.ModelAttribute; 
    import org.springframework.web.bind.annotation.PathVariable; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import com.shopzilla.monitor.form.QuartzJob; 
    import com.shopzilla.monitor.form.RequestJob; 
    import com.shopzilla.monitor.service.Alert_Generator; 
    import com.shopzilla.monitor.service.JobGen; 
    import com.shopzilla.monitor.service.JobService; 

    @Controller 
    public class JobController { 

@Autowired 
private JobService JobService; 

    @RequestMapping("/index") 
public String listContacts(Map<String, Object> map) { 
    System.out.println("Called"); 
    map.put("RequestJob", new RequestJob()); 
    map.put("Joblist", JobService.listJob()); 

return "Monitor"; 
} 



@RequestMapping(value = "/add", method = RequestMethod.POST) 
public String addContact(@ModelAttribute("RequestJob") 
RequestJob RequestJob, BindingResult result) { 
    String URL = RequestJob.getURL(); 
    String Locator = RequestJob.getLocator(); 
    String regex = RequestJob.getRegex(); 
    String Threshold = RequestJob.getThreshold(); 
    String recipient = RequestJob.getRecipient(); 
    String Frequency = RequestJob.getFrequency(); 
    String Alarmif = RequestJob.getAlarmif(); 


    String vlaue1=null; 
    String jobName = "Job_"+System.currentTimeMillis(); 
    String jobGroup = "Group1"; 
    Alert_Generator Alarm = new Alert_Generator(); 
    try { 
     vlaue1=Alarm.Monitor(URL, Locator, regex, Threshold, recipient,Alarmif,jobName); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    //configure job 


    JobKey jobkey = JobKey.jobKey(jobName, jobGroup); 

    JobDetail job = JobBuilder.newJob(JobGen.class) 
      .usingJobData("URL", URL) 
      .usingJobData("Locator", Locator) 
      .usingJobData("regex", regex) 
      .usingJobData("Threshold", Threshold) 
      .usingJobData("recipient", recipient) 
      .usingJobData("Alarmif", Alarmif) 
      .withIdentity(jobkey).storeDurably().build(); 


    Date startTime = new Date(); 
    CronTrigger trigger = TriggerBuilder.newTrigger() 
      .withIdentity(jobName) 
      .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) 
      .build(); 
    try { 
     //Schedule Job 
     SchedulerFactory schdFact = new StdSchedulerFactory("quartz.properties"); 
     Scheduler scheduler = schdFact.getScheduler(); 
     scheduler.start(); 
     scheduler.scheduleJob(job, trigger); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return "redirect:/index"; 
} 


@RequestMapping("/delete/{Job}") 
public String deleteContact(@PathVariable("contactId") 
String JobName,String JobGroup) { 

    JobService.removeJob(JobName, JobGroup); 

    return "redirect:/index"; 
} 

}

RequestJob.java

  package com.shopzilla.monitor.form; 

    public class RequestJob { 
    public String URL; 
    public String Locator; 
    public String regex; 
    public String Threshold; 
    public String recipient; 
    public String frequency; 
    public String Alarmif; 
    public String getAlarmif() { 
     return Alarmif; 
    } 
    public void setAlarmif(String alarmif) { 
     Alarmif = alarmif; 
    } 
    public String getURL() { 
     return URL; 
    } 
    public void setURL(String uRL) { 
     URL = uRL; 
    } 
    public String getLocator() { 
     return Locator; 
    } 
    public void setLocator(String locator) { 
     Locator = locator; 
    } 
    public String getRegex() { 
     return regex; 
    } 
    public void setRegex(String regex) { 
     this.regex = regex; 
    } 
    public String getThreshold() { 
     return Threshold; 
    } 
    public void setThreshold(String threshold) { 
     Threshold = threshold; 
    } 
    public String getRecipient() { 
     return recipient; 
    } 
    public void setRecipient(String recipient) { 
     this.recipient = recipient; 
    } 
    public String getFrequency() { 
     return frequency; 
    } 
    public void setFrequency(String frequency) { 
     this.frequency = frequency; 
    } 
    } 

的web.xml

  <?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>Monitoring Tool</display-name> 
     <welcome-file-list> 
      <welcome-file>index.html</welcome-file> 
     </welcome-file-list> 
     <servlet> 
      <servlet-name>spring</servlet-name> 
      <servlet-class> 
       org.springframework.web.servlet.DispatcherServlet 
      </servlet-class> 
      <load-on-startup>1</load-on-startup> 
     </servlet> 
     <servlet-mapping> 
      <servlet-name>spring</servlet-name> 
      <url-pattern>/</url-pattern> 
     </servlet-mapping> 
    <!-- <listener> --> 
    <!--  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> --> 
    <!-- </listener> --> 


    </web-app> 

Monitor.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
    <html> 
    <head> 
     <title>Monitoring Tool</title> 
     <style type="text/css"> 
      body { 
       font-family: sans-serif; 
      } 
      .data, .data td { 
       border-collapse: collapse; 
       width: 100%; 
       border: 1px solid #aaa; 
       margin: 2px; 
       padding: 2px; 
      } 
      .data th { 
       font-weight: bold; 
       background-color: #5C82FF; 
       color: white; 
      } 
     </style> 
    </head> 
    <body> 

    <h2>Job Manager</h2> 

    <form:form method="post" action="add.html" commandName="RequestJob"> 

     <table> 
     <tr> 
      <td><form:label path="URL"></form:label></td> 
      <td><form:input path="URL" /></td> 
     </tr> 
     <tr> 
      <td><form:label path="Locator"></form:label></td> 
      <td><form:input path="Locator" /></td> 
     </tr> 
     <tr> 
      <td><form:label path="regex"></form:label></td> 
      <td><form:input path="regex" /></td> 
     </tr> 
     <tr> 
      <td><form:label path="Threshold"></form:label></td> 
      <td><form:input path="Threshold" /></td> 
     </tr> 
     <tr> 
      <td><form:label path="recipient"></form:label></td> 
      <td><form:input path="recipient" /></td> 
     </tr> 
     <tr> 
      <td><form:label path="frequency"></form:label></td> 
      <td><form:input path="frequency" /></td> 
     </tr> 
     <tr> 
      <td colspan="2"> 
       <input type="submit" value="submit"/> 
      </td> 
     </tr> 
    </table>  
    </form:form> 


    <!-- <h3>Jobs</h3> --> 
    <%-- <c:if test="${!empty Joblist}"> --%> 
    <!-- <table class="data"> --> 
    <!-- <tr> --> 
    <!-- <th>JobName</th> --> 
    <!-- <th>jobGroup</th> --> 
    <!-- <th>strjobKey</th> --> 
    <!-- <th>nextFireTime</th> --> 
    <!-- <th>&nbsp;</th> --> 
    <!-- </tr> --> 
    <%-- <c:forEach items="${Joblist}" var="contact"> --%> 
    <!-- <tr> --> 
    <%--  <td>${Joblist.jobName} </td> --%> 
    <%--  <td>${Joblist.jobGroup}</td> --%> 
    <%--  <td>${Joblist.strjobKey}</td> --%> 
    <%--  <td>${Joblist.nextFireTime}</td> --%> 
    <%--  <td><a href="delete/${Joblist.jobName,Joblist.jobGroup}">delete</a></td> --%> 
    <!-- </tr> --> 
    <%-- </c:forEach> --%> 
    <!-- </table> --> 
    <%-- </c:if> --%> 


    </body> 
    </html> 

當我運行這個項目我得到

SEVERE: No WebApplicationContext found: no ContextLoaderListener registered? 
    java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? 
     at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84) 
     at org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:81) 
     at org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:219) 
     at org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74) 
     at org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48) 
     at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:76) 
     at org.apache.jsp.Monitor_jsp._jspx_meth_form_005fform_005f0(Monitor_jsp.java:163) 
     at org.apache.jsp.Monitor_jsp._jspService(Monitor_jsp.java:100) 
     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) 
     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) 
     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
     at java.lang.Thread.run(Thread.java:744) 

    Apr 13, 2014 10:11:44 PM org.apache.catalina.core.StandardWrapperValve invoke 
    SEVERE: Servlet.service() for servlet [jsp] in context with path [/MonitoringTool] threw exception [An exception occurred processing JSP page /Monitor.jsp at line 29 

    26: 
    27: <h2>Job Manager</h2> 
    28: 
    29: <form:form method="post" action="add.html" commandName="RequestJob"> 
    30: 
    31:  <table> 
    32:  <tr> 

請幫我解決這個問題。完全阻斷由於這個

+0

我做我的代碼一個微小的變化。 – user3529381

+0

在spring-servlet.xml中定義了哪些bean? JobService bean在哪裏定義?它的工作原理是 – Prasad

回答

1

如果你看到日誌: WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84) ... 在org.springframework.web.servlet.support.JspAwareRequestContext(JspAwareRequestContext.java:48 ) 在org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:76) ...

RequestContext的實例(即,JspAwareRequestContext)提供了訪問當前狀態像WebApplicationContext中。 RequestContextAwareTag是所有需要RequestContext的標籤的超類。 (Spring form()標籤是org.springframework.web.servlet.tags.RequestContextAwareTag的子類)。

WebApplicationContextUtils中的getRequiredWebApplicationContext方法始終爲此Web應用程序找到根WebApplicationContext,該Web應用程序通常通過ContextLoaderListener加載。每個DispatcherServlet都有自己的WebApplicationContext,它本身不是根WebApplicationContext,而是繼承了已經在根WebApplicationContext中定義的所有bean。

在您的方案中,您尚未定義根WebApplicationContext(即通過ContextLoaderListener),從而出現錯誤。 包括在web.xml中下面的代碼:

<!--Root web application context--> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

而作爲創建一個空的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    </beans> 
+0

Thanx .... – user3529381

相關問題