未找到WebApplicationContext:未注册ContextLoaderListener:java.lang.IllegalStateException

时间:2015-02-23 06:05:28

标签: java xml spring hibernate spring-mvc

我收到以下错误,但我无法解决此问题。

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84)
org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:81)
org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:219)
org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)
org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:76)
org.apache.jsp.student_jsp._jspx_meth_form_005fform_005f0(student_jsp.java:128)
org.apache.jsp.student_jsp._jspService(student_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

1.WEB.XML

<servlet>
    <servlet-name>jlcindia</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/jlcindia-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jlcindia</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

2.jlcindia-servlet.xml中

                               

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/ankur" />
    <property name="username" value="root" />
    <property name="password" value="admin" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.vivek.model.Student</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>                
        </props>
    </property>
</bean>

<bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

控制器类:

@Controller
public class StudentController {

@Autowired
private StudentService studentService;

public String setupForm(Map <String,Object> map){

    System.out.println("setupForm() -> StudentController Called");

    Student student=new Student();
    map.put("student",student);
    map.put("studentList",studentService.getAllstudent());
    return "student";

}

@RequestMapping(value="/student.do", method=RequestMethod.POST)
public String doActions(@ModelAttribute Student student,BindingResult result,@RequestParam String actions,Map <String,Object> map){

    System.out.println("doActions() -> StudentController Called");

    Student studentresult=new Student();
    switch (actions.toLowerCase()) {
    case "add":
        studentService.add(student);
        studentresult=student;

    case "edit":
        studentService.edit(student);
        studentresult=student;

    case "delete":
        studentService.delete(student.getStudentId());
        studentresult=new Student();
    case "search":

        Student searchStudent=studentService.getStudent(student.getStudentId());
        studentresult = searchStudent!=null ? searchStudent : new Student();
        break;
    default:
        break;
    }

    map.put("student",studentresult);
    map.put("student",studentService.getAllstudent());

    return "student";
        }
}

2 个答案:

答案 0 :(得分:0)

你可以删除第一个&#34; /&#34;从

<param-value>/WEB-INF/config/jlcindia-servlet.xml</param-value>

为了拥有:

<param-value>WEB-INF/config/jlcindia-servlet.xml</param-value>

答案 1 :(得分:0)

web.xml中使用的标记就是问题。

尝试添加context-param而不是servlet param

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> </param-value>
</context-param>