当前位置: 首页 > 面试题库 >

找不到WebApplicationContext:没有注册ContextLoaderListener吗?

顾烨磊
2023-03-14
问题内容

我正在尝试创建一个简单的Spring 3应用程序并具有以下文件。请告诉我此错误的原因

下面是我的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_3_0.xsd"
        id="WebApp_ID" version="3.0">
    <display-name>Spring2</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

以下是我的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        Index Page<br/>
        <form:form commandName="loginBean" method="POST" action="login">
            <form:input path="userName" id="userName"/><br/>
            <form:input path="password" id="password"/><br/>
            <input type="submit" value="submit"/>   
        </form:form>
        <a href="register.jsp">Go to Registration Page</a>
    </body>
</html>

下面是我的dispatcher-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean> 
    <bean name="/login" class="com.infy.controller.LoginController"/>   
</beans>

这是LoginController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class LoginController {
    @RequestMapping(value="/login", method=RequestMethod.POST)
    public ModelAndView loginAction(@ModelAttribute("loginBean")LoginBean bean){
        return new ModelAndView("success", "success", "Successful Login");
    }
}

最后是我的LoginBean

public class LoginBean {
    private String userName;
    private String password;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

问题答案:

你必须在web.xml中有一个ContextLoaderListener-它会加载你的配置文件。

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

你需要了解Web应用程序上下文和根应用程序上下文之间的区别。

在Web MVC框架中,每个DispatcherServlet都有自己的WebApplicationContext,它继承了在根WebApplicationContext中已经定义的所有bean。可以在servlet特定的作用域中重写这些定义的继承Bean,并且可以在给定servlet实例本地定义新的特定于作用域的Bean。

调度程序Servlet的应用程序上下文是一个Web应用程序上下文,仅适用于Web类。你不能将这些用于中间层。这些需要使用ContextLoaderListener的全局应用程序上下文。



 类似资料:
  • 我正在建立一个注册页,当我提交时,我得到一个404没有找到。我正在使用react和express,我认为前端是好的。我缺少了一些后端,或者我的post请求的url错误:AXIOS.post('http://localhost:3000/auth'。如果有人能看到我的错误之处,我将不胜感激。 这是我的路由的registerPost函数。 这些是路线。

  • 根据Spring Doc API http://docs.Spring.io/Spring-framework/docs/current/javadoc-api/index.html?org/springframework/web/context/contextloaderlistener.html- 这个(ContextLoaderListener)监听器应该注册在web.xml中的Log4jC

  • 在Karaf+Felix+OpenJDK8环境中加载nashorn脚本引擎时遇到问题:在该环境中返回。我使用该代码的测试成功通过,不管是由maven执行(在tje Linux/Debian服务器上安装相同的JDK)还是在我的Windows机器上的IDE中执行。 以下是我检查的步骤: > Karaf运行时使用JDK8,引用了正确的ext目录: >ps aux grep karaf grep java

  • 我有一个使用的实现,但这与spring中的其他自动工作冲突,并覆盖了应用程序自身的一些逻辑。对于本应简单的东西来说,它也显得难以置信的沉重。我现在正在尝试: 但是,拦截器注册的实例似乎与应用程序实际用于web请求的实例完全不同。此外,当作为实现时,当我尝试时,在中获得

  • 我有一个查询,我需要过滤出结果。 这是我的查询 我得到一个错误,说注册[query]。我显然有一个筛选字段的查询。我遵循elasticsearch页面上筛选的查询文档中给出的格式。https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html

  • 我的log4j.properties: 编写代码如何使用日志