找不到WebApplicationContext:没有注册ContextLoaderListener?

时间:2014-04-13 16:34:20

标签: web-applications

M对JAVA来说很新。 尝试使用spring mvc构建应用程序。 我只想创建一个简单的表格来注册石英作业。

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>

请帮我解决这个问题。由于这个完全阻止

1 个答案:

答案 0 :(得分: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>